Repository: SerenityOS/serenity
File: Userland/Libraries/LibMaps/MapWidget.cpp (C++)

Code before:

            return *this;
        }
    };

    constexpr Iterator begin() { return Iterator { m_width, m_height }; }
    constexpr Iterator end() { return begin().end(); }

private:
    T m_width;
    T m_height;
};

void MapWidget::paint_map(GUI::Painter& painter)
{
    int center_tile_x = longitude_to_tile_x(m_center.longitude, m_zoom);
    int center_tile_y = latitude_to_tile_y(m_center.latitude, m_zoom);
    double offset_x = (longitude_to_tile_x(m_center.longitude, m_zoom) - center_tile_x) * TILE_SIZE;
    double offset_y = (latitude_to_tile_y(m_center.latitude, m_zoom) - center_tile_y) * TILE_SIZE;

    // Draw grid around center tile; always pad the dimensions with 2 tiles for left/right and top/bottom edges
    // plus one additional tile to account for the width() / 2 in CenterOutwardsIterable.
    int grid_width = width() / TILE_SIZE + 3;
    int grid_height = height() / TILE_SIZE + 3;

    i32 number_of_tile_per_axes = pow(2, m_zoom);

    for (auto const delta : CenterOutwardsIterable { grid_width, grid_height }) {
        int tile_x = center_tile_x + delta.x();
        int tile_y = center_tile_y + delta.y();

        // Only draw tiles that exist
        if (tile_y < 0 || tile_y >= number_of_tile_per_axes)
            continue;

        auto tile_rect = Gfx::IntRect {
            static_cast<int>(width() / 2 + delta.x() * TILE_SIZE - offset_x),
            static_cast<int>(height() / 2 + delta.y() * TILE_SIZE - offset_y),
            TILE_SIZE,
            TILE_SIZE,
        };

        // Make the tiles wrap horizontally.
        tile_x = tile_x % number_of_tile_per_axes;
        if (tile_x < 0)
            tile_x += number_of_tile_per_axes;

        if (!tile_rect.intersects(frame_inner_rect()))
            continue;

        // Get tile, when it has a loaded image draw it at the right position

Review comment (suggestion):
We also have a `exp2` in IntegralMath.h (or just use a shift)

Code after:
            : width(move(width))
            , height(move(height))
        {
        }

        constexpr Iterator end() const
        {
            auto end = *this;
            end.index = AK::max(width, height) * AK::max(width, height);
            return end;
        }
        constexpr bool is_end() const { return *this == end(); }

        constexpr bool operator==(Iterator const& other) const { return index == other.index; }
        constexpr bool operator!=(Iterator const& other) const { return index != other.index; }

        constexpr Gfx::Point<T> operator*() const { return position; }

        constexpr Iterator operator++()
        {
            while (!is_end()) {
                // Figure out in which of the four squares we are.
                if (AK::abs(position.x()) <= AK::abs(position.y()) && (position.x() != position.y() || position.x() >= 0))
                    position.translate_by(position.y() >= 0 ? 1 : -1, 0);
                else
                    position.translate_by(0, position.x() >= 0 ? -1 : 1);
                ++index;

                // Translating the coordinates makes the range check simpler:
                T xp = position.x() + width / 2;
                T yp = position.y() + height / 2;
                if (xp >= 0 && xp <= width && yp >= 0 && yp <= height) {
                    break;
                }
            }

            return *this;
        }
    };

    constexpr Iterator begin() { return Iterator { m_width, m_height }; }
    constexpr Iterator end() { return begin().end(); }

private:
    T m_width;
    T m_height;
};

void MapWidget::paint_map(GUI::Painter& painter)
{
    int center_tile_x = longitude_to_tile_x(m_center.longitude, m_zoom);