问题
I am creating a simple GUI to record points which are clicked on the image, using wxpython. However, i would like the click to be snapped to certain points on the image. Please suggest methods.
回答1:
The easiest to snap to a fixed scale grid is with something like:
snap_x = scale * round(x / scale)
snap_y = scale * round(y / scale)
where scale is the size of your grid, eg 10 pixels.
If the points are nonuniformly distributed, then find the closest based on the distance (or the square of the distance for efficiency reasons).
回答2:
You might have a look at wxPython's Object Graphics Library. The documentation's a little sparse but the Diagram class has direct support for snapping to grids; take a look at the wxPython Demo's Miscellaneous->OGL section for some ideas to get started. I'm in the middle of a project w. OGL right now - it's great for what it does but like I said I find the documentation a little thin.
来源:https://stackoverflow.com/questions/7794496/snapping-to-pixels-in-wxpython