BLOG.CSHARPHELPER.COM: Find the area where two or more circles overlap in C#
Find the area where two or more circles overlap in C#
You can use the example Determine where two circles intersect in C# to find the region where two circles overlap. Finding the area where more circles overlap this region is just as easy in principle: use similar code to see where the new circles intersect the previous region. Unfortunately the details are a bit involved. (This is similar to the way you would see where a group of polygons overlap except the "sides" are circular arcs instead of line segments.)
Fortunately the .NET Framework provides Regions that make this much easier.
The FindCircleIntersection method uses the following code to build a Region representing the area where a list of circles overlap.
// Find the intersection of all of the circles. private Region FindCircleIntersections(Listcenters, Listradii) { if (centers.Count < 1) return null;
// Make a region. Region result_region = new Region();
// Intersect the region with the circles. for (int i = 0; i < centers.Count; i++) { using (GraphicsPath circle_path = new GraphicsPath()) { circle_path.AddEllipse( centers[i].X - radii[i], centers[i].Y - radii[i], 2 * radii[i], 2 * radii[i]); result_region.Intersect(circle_path); } }
return result_region; }
The code creates a Region. It then loops through the circles. For each circle, the program makes a new GraphicsPath object, adds the circle to it, and calls the Region's Intersect method to intersect the Region with the circle. The result is a new Region that includes the area where the old Region and the circle overlap.
The form's Paint event handler uses the following code to call FindCircleIntersection and then fill the Region.
// Find the intersection of all circles. Region intersection = FindCircleIntersections(Centers, Radii);
// Draw the region. if (intersection != null) { e.Graphics.FillRegion(Brushes.LightGreen, intersection); }
8/26/2010 8:00 AM
Ate Peeringa wrote:
Hello this is a great app but if there is no intersection with the first two circles it displays none if there is a intersecten with the second and 3rd and so on. Reply to this
8/26/2010 8:53 AMRod Stephens wrote:
That's true. If there is no intersection, then there's nothing to show. If you want it to do something else, like color different areas depending on how many circles they are in, then you'll have to write some code to do that. (Although that sounds like fun so maybe I'll give it a try.)
If you had something else in mind, let me know. Reply to this
Hello this is a great app but if there is no intersection with the first two circles it displays none if there is a intersecten with the second and 3rd and so on.
Reply to this
That's true. If there is no intersection, then there's nothing to show. If you want it to do something else, like color different areas depending on how many circles they are in, then you'll have to write some code to do that. (Although that sounds like fun so maybe I'll give it a try.)
If you had something else in mind, let me know.
Reply to this
Take a look at the new example
Partition an area with circles and draw each region's count in C#
Reply to this