16,004,399 members
5.00/5 (1 vote)
See more:
I draw a curve using GraphicsPath:

```GraphicsPath gp = new GraphicsPath();
gp.AddCurve(new Point[]{ new Point(10,30), new Point(50,60), new Point(100,30)
});
...```

I know IsOutlineVisible method to determine whether or not a point lies on the curve. But the problem is I want to know exactly which part of curve the point lies on. For example, from point (10,30) to (50,60) or from (50,60) to (100,30).

Does anyone have an idea or two to help me wrap my brain around this one?

Thanks!
Posted
Updated 7-Dec-09 2:29am
v2

## Solution 1

try using:

```Rectnagle RectangleFromPoints(Point A, Point B)//method 1
{
//Finish this method...
}

int[] PointOnCurveBetween(Point P, GraphicsPath gp)//Method 2
{
for(int i=1;i<gp.pointlist.Length; i++)
{
if(RectangleFromPoints(
gp.Points[i-1],gp.Points[i]).Contains(P))
{ return new int[]{i,i-1};}

}
}```

At least that is the first way that comes to mind.. would work best if you knew the direction of the the curve(or you could determine the derivative of each sequence in method 1, and use switch/if statements)...

this will only work reliably when the function is 1-1 and somewhat well behaved.

alternativly if you "know" that they are clicking on the curve itself(no tollerance for user error ) you can do a least squares regression to each line segment, and then sort for the segment with the highest R value. -- this is more rigiourus, however will be slower then Method 1..(you could acomplist this also by using plane geometry/trigonometry, however that may be significantly slower due to the Math.(blah) functions which are "slow"..

--Incidently if you do use the least squares approach you wouldn't need to take the sqrt, just leave the numbers "big" and compare them the answer will still be the same.--Sometimes you don't need to be precise to do math.

-- I did something similar and you may want to try a "Node" approach, where you seperate the line segment into 2 ends and a center, each with a circle arround them equal to .25 the distance of the segment, this way you use a tree and get"closer" answers fast and then use more rigourus methods to determine the"correct" answer.

-good luck.

v2