Point into a ClosedCurve

  • Thread starter Thread starter Sergio
  • Start date Start date
S

Sergio

Hello,

I'm developing an application that manages some kind of shapes.
One of those shapes is an drawn using Graphics.DrawClosedCurve() function.
I want to know if the user has selected a coordinate inside this draw or
not, but I don't know how to do it. Any idea?

Thank you
 
Sergio,

Take a look at the GraphicPath and Region classes. You can add closed
curves to an instance of the GraphicPath and then add the GraphicPath to an
instance of a Region class. Afterwards, you can use the region's IsVisible
method to find a point within the it.

I don't use this objects, so I may be a little off on their use; however, I
don't think I am.

If you looking for points in a polygon, I translated a class from C++ to C#
to do that; however, you are using curves so it won't help you there.

At any rate, I hope I have helped.
Dave
 
GraphicsPath is enough for hittesting. It contains method IsVisible and
IsOutlineVisible. The latter is to check only on the path outline. Graphics
path is very useful type. I won't suggest using Regions though. Besides they
are resource expensive the type also has bugs that cause memory leaks IFAIK
(at least in .NET 1.x)
 
Stoitcho said:
GraphicsPath is enough for hittesting. It contains method IsVisible and
IsOutlineVisible. The latter is to check only on the path outline. Graphics
path is very useful type. I won't suggest using Regions though. Besides they
are resource expensive the type also has bugs that cause memory leaks IFAIK
(at least in .NET 1.x)

Thank you to both. I've solved my problem using GraphicsPath and
IsVisible().
 
IsVisible and IsOutlineVisible are both also broken when using float
types. If you are working in integer pixels, you should be ok.

Broken? Yeah...

GraphicsPath path = new GraphicsPath();
path.AddRectangle(new RectangleF(0.0f, 0.0f, 0.0625f, 0.0625f));

// defintely outside the path
if (path.IsVisible(-0.01f, -0.01f))
{
throw new ApplicationException("This is crazy");
}
}

Microsoft claims this is "as designed" with vauge reference to
GraphicsPath rasterizing before testing.

If you want try testing with floats you have to write your own code to
make use of PathData. If you are working with curves, you canto
flatten before working with PathData.

Generally speaking to test "am i inside" a closed path, project a
vector from testing point to somewhere outside of the path (anywhere).
Then count intersections between test vector and the line segments that
comprise the flattened path. If count is odd, the point is inside the
closed path. If the count is even, the point is outside the closed
path.

To test "am I on the path", compute the length of the normal for each
line segment in the flattened path and then test if the length of the
normal is within some tolerance (such as pen width).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top