PDA

View Full Version : How to find the nearest point on a QPainterPath



Markus
9th June 2010, 00:07
Hello all,

I need to find a point on a QPainterPath that is nearest to an arbitrary point outside the QPainterPath.
For example: I have a QPainterPath that is a circle and a point outside the circle. Now I want to draw the shortest possible line between the circle and the point.

If I use connectPath() it always connects to the beginning of the path (circle) which is never the nearest point.

How can I do that?


Markus

SixDegrees
9th June 2010, 00:18
There are mathematical techniques (http://algorithmist.wordpress.com/2008/08/01/closest-point-on-bezier-preview/) for determing such points, but they require access to the mathematical definition of the curve involved. Qt doesn't make this information available, and doesn't provide such a function as part of QPainterPath.

If the paths involved are always simple (circles, for instance, or straight line segments) you should be able to come up with a solution independently. If the curve is arbitrary, however, you are probably out of luck short of a brute-force approach of some sort.