This example shows how you can find the area where circles overlap in a .NET program. The example Determine where two circles intersect in C# shows how to find the points where two circles intersect. 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 a `Region` class that makes this much easier.

The `FindCircleIntersection` method uses the following code to build a `Region` representing the area where the circles in a list overlap.

// Find the intersection of all of the circles. private Region FindCircleIntersections(List<PointF>centers, List<float>radii) { 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` object. It then loops through the list of circles. For each circle, the program makes a new `GraphicsPath` object, adds the circle to it, and calls the `Region` object’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 display the circles and their intersection.

// Draw the circles. private void Form1_Paint(object sender, PaintEventArgs e) { e.Graphics.SmoothingMode = SmoothingMode.AntiAlias; // Find the intersection of all circles. Region intersection = FindCircleIntersections(Centers, Radii); // Draw the region. if (intersection != null) { e.Graphics.FillRegion(Brushes.LightGreen, intersection); } // Draw the circles. for (int i = 0; i < Centers.Count; i++) { e.Graphics.DrawEllipse(Pens.Blue, Centers[i].X - Radii[i], Centers[i].Y - Radii[i], 2 * Radii[i], 2 * Radii[i]); } }

This code calls the `FindCircleIntersections` method to find the region where the circles intersect. If the `Region` isn’t `null`, the code uses the `e.Graphics` object’s `FillRegion` to fill the region with light green.

The method finishes by drawing the circles in the list.

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.

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.

Take a look at the new example

Partition an area with circles and draw each region’s count in C#

Pingback: Find a Region centroid in C# - C# HelperC# Helper

Pingback: Partition an area with circles and draw each region's countC# Helper