Groups | Blog | Home
all groups > dotnet drawing api > march 2007 >

dotnet drawing api : Detect circle overlapping line


Nik Coughlin
3/13/2007 12:00:00 AM
Say I have drawn a number of lines on a form (forming a polygon), and I know
the coordinates for each point in the polygon. Now, I want to draw a circle
on the form, but I want to make sure that it doesn't overlap any of the
lines.

The approach I am thinking of goes something like this:

The center of my circle is at this point on the form. Are any of the pixels
that make up any of the lines at a distance from the center of circle that
is less than or equal to the radius of the circle? If so then the circle
overlaps a line.

Now, can GDI+ help me with this? I understand the problem but I'm having
trouble phrasing a Google search that finds me anything meaningful. What
keywords can I use to search for information, or what classes should I be
looking at?

Thanks heaps :)

Michael C
3/13/2007 12:00:00 AM
[quoted text, click to view]

I can see 2 approaches. The first would be to use a region object for the
circle and the polygon and check if they intersect (look up region in help).
This won't me mathematically accurate but should be accurate to the pixel.

The second would be to determine mathematically if the circle and line
intersect. I'm not sure how to do the maths but you can use the 2 equations
for the circle: x2+y2=r2 (where the 2 means squared)
for the line: y = ax + b
substitue the second into the first and solve for x. If you get an invalid
result (eg squareroot of a negative no) then they don't intersect. I'm a bit
rusty on the old maths sorry.

I should also mention that it's possible for the circle to be completely
inside the polygon but intersect no lines so you should check for that too.
That should be a bit easier if you divide the polygon into triangles and
check if the centre of the circle is in any of those triangles.

Michael

Nik Coughlin
3/13/2007 12:00:00 AM
[quoted text, click to view]

This won't tell me if the circle intersects one of the lines though will it,
only if it intersects the polygon as a whole? Which is actually something I
need to know as well, but I also need to know specifically if the circle
intersects the lines making up the polygon.

[quoted text, click to view]

I'll give it a shot, thanks.

[quoted text, click to view]

I don't mind the circle being inside the polygon (in fact, I want it to
be!), so long as it doesn't intersect any of the lines.

What I specifically trying to do is allow the circle to be moved around
within the polygon but not allow it to leave the interior of the polygon.

The polygon may be of any level of complexity and to further complicate
things, it may have holes in too.

Michael C
3/13/2007 12:00:00 AM
[quoted text, click to view]

That's true but you will be able to tell if the circle outside the polygon
by getting the regions that don't interset.

[quoted text, click to view]

I got

(a2 + 1)x2 + 2abx + b2 - r2 = 0

which can be considered as

dx2 + ex + f = 0
where
d= (a2 + 1)
e = 2ab
f = b2 - r2

you can then use a formula (which I have forgotten) to convert it to

(hx + i)(jx + k) = 0

if h, i, j or k are unreal numbers (roots of negative) then they don't
intersect.

[quoted text, click to view]


That shouldn't be too much of an issue, just treat the holes as seperate
polygons and do the opposite with them. The other problem you will have is
calculating the position the circle needs to be to just touch the edge. For
example if the circle is right at the centre of the screen and inside the
polygon and then it is moved suddenly to the very left of the screen and
outside the polygon, leaving it at the centre will look poor, you'll need to
determine the edge of the polygon and move it there. Does that make sense?

Michael

Nik Coughlin
3/13/2007 12:00:00 AM
[quoted text, click to view]

Ah, of course.

[quoted text, click to view]

Thankfully this won't be a problem as I'll only be moving the circle one
pixel at a time.

Thank you very much for your assistance!

Michael C
3/13/2007 12:00:00 AM
[quoted text, click to view]

One other idea would be to reduce the size of the polygon by the radius of
the circle. This new smaller polygon would be the area that the centre of
the circle is permitted to travel in.

Michael

Frank Hileman
3/13/2007 10:34:01 AM
If the center of your circle is always the center of the form, the radius of
the circle is the maximum distance from the center to any vertex on any
line. If instead you want the minimum enclosing circle with any center:

http://www.cs.brown.edu/people/tor/java/mec/
http://www.personal.kent.edu/~rmuhamma/Compgeometry/MyCG/CG-Applets/Center/centercli.htm
http://www.cs.mcgill.ca/~cs507/projects/1998/jacob/solutions.html

Regards,
Frank Hileman

check out VG.net: http://www.vgdotnet.com
Animated vector graphics system
Integrated Visual Studio graphics editor

[quoted text, click to view]

AddThis Social Bookmark Button