python - Scale polygons by a ratio using only a list of their vertices -
in program, users can draw shapes atop matplotlib plot , various actions shapes. i'm trying implement scaling of these shapes maintain location in reference main plot, when zoomed in.
my objects represented list of vertices,
obj = [(1, 1), (2, 1), (1, 0), (2, 0)] # represents 1 unit square
of course simplified breakdown of how polygons represented, useful attribute of them in case vertices.
the user can select bounding box of zoom in, shown below
once mouse released application zoom location, problem polygons not zoom this. while canvas zoomed, polygons remain in exact location , represent whole different area did before. because zoom handled matplotlib, backend actual application. like along lines of picture shows below if user zoom in on selected location above:
so know is
- the list of vertices of object
[(1,2),(1,0)....]
- a list of handles objects contained inside bounded zoom
targets = [itemhandle1, itemhandle2....]
- the location of bounded zoom box via topleft , bottom right coordinate e.g.
zoomboundedbox = [(162, 62), (937, 560)]
i believe know required data objects scale these objects correctly, don't know algorithm allow me accomplish ...
def receive(self, lim): ''' calculate new coordinates of polygons visible screen, function called when user releases mouse button on zoom function, parameters of bounding box internally stored (x,y) tuples in xyf (x,y first) , xyl (x,y last) ''' # grab item handles polygons intersect zoom # stored in `targets` shape in self.activepolygonlist: # loop through active polygons on screen if shape.handle() in targets: # if polygon target scaled print "scaling...." # ? shape.redrawshape()
as said in comment, think can use similar did in answer rotate line around center point given 2 vertices.
the difference mathematical formulas scale point (x,y) factor of s
relative point (cx, cy) are:
x_new = ( s * (x - cx) ) + cx y_new = ( s * (y - cy) ) + cy
and these determines how computation of points (p1x, p1y) , (p2x, p2y) (x1, y1) , (x2, y2) done in inner loop.
another difference might want scale polygons relative center of user's bounding box rather center of each polygon. mean wouldn't have compute cx
, cy
each polygon (which make faster).
Comments
Post a Comment