@@ -29,6 +29,7 @@ void Viewport::set_range(double x_min, double x_max, double y_min, double y_max)
2929 }
3030}
3131
32+ // Pan: shift visible range by a fraction of the current range (dx, dy in [-1,1]).
3233void Viewport::pan (double dx, double dy) {
3334 double x_range = m_x_max - m_x_min;
3435 double y_range = m_y_max - m_y_min;
@@ -39,6 +40,7 @@ void Viewport::pan(double dx, double dy) {
3940 m_y_max += dy * y_range;
4041}
4142
43+ // Zoom: scale distance from (center_x, center_y). factor > 1 zooms in, < 1 zooms out.
4244void Viewport::zoom (double factor, double center_x, double center_y) {
4345 if (factor <= 0.0 ) return ;
4446
@@ -61,6 +63,7 @@ void Viewport::reset() {
6163 m_y_max = DEFAULT_Y_MAX;
6264}
6365
66+ // Map world (math) coords to pixel coords. Y is flipped so +y is up in world, down on screen.
6467void Viewport::world_to_screen (double world_x, double world_y,
6568 int screen_width, int screen_height,
6669 int & screen_x, int & screen_y) const {
@@ -74,12 +77,12 @@ void Viewport::world_to_screen(double world_x, double world_y,
7477 }
7578
7679 double normalized_x = (world_x - m_x_min) / x_range;
77- double normalized_y = 1.0 - (world_y - m_y_min) / y_range; // Flip Y axis
78-
80+ double normalized_y = 1.0 - (world_y - m_y_min) / y_range; // Flip Y: world +y = screen top
7981 screen_x = static_cast <int >(normalized_x * screen_width);
8082 screen_y = static_cast <int >(normalized_y * screen_height);
8183}
8284
85+ // Map pixel coords back to world coords (inverse of world_to_screen, same Y flip).
8386void Viewport::screen_to_world (int screen_x, int screen_y,
8487 int screen_width, int screen_height,
8588 double & world_x, double & world_y) const {
@@ -93,8 +96,7 @@ void Viewport::screen_to_world(int screen_x, int screen_y,
9396 }
9497
9598 double normalized_x = static_cast <double >(screen_x) / screen_width;
96- double normalized_y = 1.0 - static_cast <double >(screen_y) / screen_height; // Flip Y axis
97-
99+ double normalized_y = 1.0 - static_cast <double >(screen_y) / screen_height;
98100 world_x = m_x_min + normalized_x * x_range;
99101 world_y = m_y_min + normalized_y * y_range;
100102}
0 commit comments