Click to See Complete Forum and Search --> : Using GraphicsPath's IsOutlineVisible() method to find intersection points...


ahmadka
June 25th, 2008, 11:55 PM
EDIT: Pictures are not being displayed for some reason, so I have provided links for each image instead...

Hi everyone! Well I have recently started using the GraphicsPath object in C#, and I had a question concerning one of its methods, name IsOutLineVisible() .

I am currently developing this small application, in which I need to find the points on a GraphicsPath where a given line intersects it. This is pretty simple in basic mathematics. Also, my problem is only for 2D, for those wondering. I am using this IsOutLineVisible() method to determine whether a given point lies on the GraphicsPath or not.

The problem I am having is that the intersection points that I get, are not accurate. That is, the point is slightly 'off' from where it really should be.

I will show this error using some pictures. Have a look at the following linked picture:

http://img66.imageshack.us/img66/284/zoom11mv2.png

In the above picture, a rectangular GraphicsPath is shown in orange colour. The light blue colored line is the line which is intersecting the GraphicsPath. The tiny red and green circles repersent the intersection points. At this zoom level, it seems that the intersection points are spot-on, but actually they are not!

Let me zoom in, to give you a closer view:

http://img70.imageshack.us/img70/7352/zoom13vj5.png

In the above picture, you can see that the red circle is not really on top of the orange GraphicsPath, where it should be.

http://img66.imageshack.us/img66/5893/zoom16rz4.png

In the above picture, we see that the red marker is actually not on top of the GraphicsPath after all !!

http://img70.imageshack.us/img70/1638/zoom16bvo5.png

Finally, in the above picture, we again see the same thing, but here I have indicated where the red circle should have been. Also indicated in the picture is the real world distance between where the red circle is, and where it should have been. This distance is currently 27 km, whereas it should ideally be zero!

The offset makes my application inaccurate.

These are the steps I follow in order to check whether a given point lies on the outline of a GraphicsPath or not:

1) I define a temporary GraphicsPath object called tmpGp.
2) I add the GraphicsPath object that I need to check line intersection with, to tmpGp.
3) I then form the line with I need to check intersection with, by calculating its gradient and Y-intercept.
4) I then have iterate a while loop, in which the intersection is checked. The iteration variable is the x-coordinate. For each coordinate, I calculate a y-coordinate value. These coordinates are then fed into the intersection checking statement, to see if the point lies on the GraphicsPath outline or not.


Here is the while loop code:

while (xWorldValue <= 180 && yWorldValue >= -90 && yWorldValue <= 90)
{

SharpMap.Geometries.Point testPoint = new SharpMap.Geometries.Point(xWorldValue, yWorldValue); //The point which needs to be tested if its intersects the GraphicsPath's outline or not.
Pen drawingPen = new Pen(Color.Green, 0.01f); //The Pen with which the GraphicsPath is drawn.

if (tmpGp.IsOutlineVisible(sharpMap.WorldToImage(testPoint).X, sharpMap.WorldToImage(testPoint).Y, drawingPen))
{
//An intersection point has been found!
//My internal point processing code comes here, which is irrelevant to this problem.
}
xWorldValue += 0.05f; //incrementing the x-coordinate iteration variable
yWorldValue = (pathGradient * xWorldValue) + yIntercept; //recalculating the corresponding y-coordinate value
}


As you can see, the iteration variable increments by just 0.05f, which is very small !! But still, the values I get are not accurate !

A possible reason as to why I think this is happening, is explained below now:

The IsOutlineVisible() method is dependent on the Pen which is used to draw the outline. This means, that the Pen's width needs to be thick enough to allow iteration variable (xWorldValue) to land into it.

Even though the width of the drawing Pen is set to 0.01f in the above code, its still too thick, making the intersection Point inaccurate!

The points which are stored in a GraphicsPath are just individual points. How can I make sure that the Pen's width is only 1 pixel wide ?? If I can somehow make the Pen's 'width' exactly 1 pixel wide, the Pen will only draw exactly where the points in the GraphicsPath are.

Please help me with this problem, as its confusing me a lot.

Regards.