What's New
Q & A
Tip Jar
C# Helper...
Follow VBHelper on Twitter Follow VBHelper on Twitter
MSDN Visual Basic Community
TitleFind the area where two or more circles overlap in Visual Basic .NET
DescriptionThis example shows how to find the area where two or more circles overlap in Visual Basic .NET.
Keywordsgeometry, graphics, algorithms, circles, overlap, circles overlap, Visual Basic .NET, VB.NET
CategoriesAlgorithms, VB.NET

You can use the example Determine where two circles intersect in Visual Basic .NET 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 Function FindCircleIntersections(ByVal centers As _
    List(Of PointF), ByVal radii As List(Of Single)) As _
    If (centers.Count < 1) Then Return Nothing

    ' Make a region.
    Dim result_region As New Region()

    ' Intersect the region with the circles.
    For i As Integer = 0 To centers.Count - 1
        Using circle_path As New GraphicsPath()
            circle_path.AddEllipse( _
                centers(i).X - radii(i), centers(i).Y - _
                    radii(i), _
                2 * radii(i), 2 * radii(i))
        End Using
    Next i

    Return result_region
End Function
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 FindCircleIntersections and then fill the Region.

' Find the intersection of all circles.
Dim intersection As Region = _
    FindCircleIntersections(Centers, Radii)

' Draw the region.
If (intersection IsNot Nothing) Then
    e.Graphics.FillRegion(Brushes.LightGreen, intersection)
End If
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.