Snapping point to path
-
I'm in need of a method that takes a point and a path and returns the point on the pathclosest to the specified point (snapping). The method prototype would look like this:
QPointF snapPointToPath(const QPointF& point, const QPainterPath& path) const;
I did implement this one for boxes/rectangles which is rather easy. However, now I would like to extend the functionality so it works with any shape/path/line.
Could anyone tell me what's the fastest/easiest way of doing this? I assume that I should callQPainterPath::createStroke()
to get just the outline. But from there I'm lost on how to get the point on the path closest to the specified one.Context: I am working with the graphics framework. A user should be able to drag a point around the outline of a
QGraphicsItem
. The method I want to implement now would take the current mouse position and theQGraphicsItem::shape()
as parameters. -
@Joel-Bodenmann Two points: 1. You can decompose the path to get each element and work on those independently. 2. You must contend with lines, polylines, ellipses and Bezier splines.
Closes point on a line is simply a vector projection. Bezier splines are more complex. For an approximation you can get polyline approximation to the remainder using parametric equations, then use the same technique for lines. I am writing a book on this subject.
By the way, I recall seeing an article on the method you propose.