In my program, users can draw shapes atop a matplotlib plot and do various actions with those shapes. I'm currently trying to implement scaling of these shapes to maintain their location in reference to the main plot, even when zoomed in.
My objects are represented by a list of vertices,
obj = [(1, 1), (2, 1), (1, 0), (2, 0)] # represents a 1 unit square
of course this is a simplified breakdown of how my polygons are represented, but the only useful attribute of them in this case is the vertices.
The User can select a bounding box of where he would like to zoom in, shown below
Once the mouse is released the application will zoom to that location, the only problem is my polygons do not zoom with this. While the canvas is zoomed, the polygons will remain in their exact location and now represent a whole different area than they did before. This is because the zoom is handled by matplotlib, which is a backend to the actual application. what I would like to do is along the lines of what the picture shows below if the user were to zoom in on the selected location above:
So what I do know is
- The list of vertices of the object
[(1,2),(1,0)....]
- A list of handles to all objects contained inside the bounded zoom
targets = [itemHandle1, itemHandle2....]
- The location of the bounded zoom box via the topleft and bottom right coordinate e.g.
zoomboundedbox = [(162, 62), (937, 560)]
I believe I know all the required data about my objects to scale these objects correctly, but I don't know the algorithm that will allow me to accomplish this ...
def receive(self, lim):
'''
Calculate new coordinates of polygons visible to screen, this function
is called when the user releases the mouse button on the zoom function,
the parameters of the bounding box are internally stored as (x,y) tuples
in xyf (x,y first) and xyl (x,y last)
'''
# Grab all item handles to polygons that intersect with the zoom
# Stored in `targets`
for shape in self.activePolygonList: # loop through active polygons on screen
if shape.handle() in targets: # if the polygon is a target to be scaled
print "scaling...."
# ?
shape.redrawShape()