c++ - Reposition rectangle after zooming -
i have rectangle defined r1:x1,y1-x2,y2 , after applying zoom, rectangle r2:x1,y1-x2,y2.
+--------------+---+ | | | | r1 | | | | | +--------------+ | | r2 | +------------------+
as can see, r2 expanded based on r1's origin. not desired effect.
what want accomplish re-calculate origin based on mouse pointer when zooming operation performed.
for example:
+-----------------------+ | +-----------------+ | | | o | | | | r1 | | | | | | | +-----------------+ | | r2 | +-----------------------+
here, mouse pointer set on point "o", zoomed, resulting on rectangle r2. please note r2 not centered on r1, little displaced right , bottom.
how can re-position origin after zooming?.
this isn't programming specific question, math problem.
if mouse in center of screen, each side expands equally. if mouse way 1 side, rectangle expands in direction. need determine size increase ratio.
so, need setup few variables here: width_delta, height_delta, mouse_x_relative, mouse_y_relative, rect_width, rect_height.
- width delta new width minus old width.
- height delta new height minus old height.
- mouse x relative x-coordinate relative rect's left side
- mouse y relative y-coordinate relative rect's top side
with each delta, when mouse centered, can calculate change in rectangles sides delta - delta / 2, , delta / 2. results in half of delta going 1 side, other half other. instead of dividing 2, need find out how relates mouse position , size of rect.
easy enough: rect_width / mouse_x_relative. let's rect width 10, mouse in center @ 5, 10 / 5 2, causing delta distributed equally both sides of rect. need divide delta rect_width / mouse_x_relative.
left_delta = width_delta / rect_width / mouse_x_relative right_delta = width_delta - left_delta
but can clean be:
left_delta = width_delta * mouse_x_relative / rect_width right_delta = width_delta - left_delta
i believe that should work expected behavior, unlike last answer. when zoom in (shrink) rect closes in on mouse (centering), when zoom out moves away mouse (un-centering, if will), inverse of way moved in.
Comments
Post a Comment