BLOG.CSHARPHELPER.COM: Partition an area with circles and draw each region's count in C#
Partition an area with circles and draw each region's count in C#
The example Find a Region's centroid in C# explains how to find the area where two or more circles overlap. This example extends this idea farther by using circles to partition an area. In other words, it uses circles to divide the area into non-overlapping pieces. It then labels each area with the number of circles that contain it. (The inspiration for this example comes from a collection of cellphone towers. The circles represent their areas of coverage and the labels tell how many towers are within range at every point.)
The RegionInfo class holds information about one of the partitions. This class has a Region property holding a Region that represents the partition, and a Count property indicating the number of circles that cover the region.
The high-level idea behind the program is fairly simple, although the details are rather involved so I won't cover them in detail here. Download the example and look at the code to learn the details.
When the form receives a Paint event, the program builds a List named regioninfos that holds RegionInfo objects representing the partitions.
To build this list, the program considers each circle in turn. It builds a RegionInfo object to represent the circle and then calls the RegionInfo object's MakeIntersections method.
The MakeIntersections method loops through the RegionInfo objects that are already in the list. For each object "other" in the list, it considers how "other" and the current object "this" partition their areas. If the two objects overlap, then they divide their are into three pieces:
this intersect other
this - other
other - this
If the regions overlap, the code:
Replaces "other" with "other" - "this"
Replaces "this" with "this" - "other"
Adds "this" intersect "other" to a new list of RegionInfo objects
After it has compared "this" to all of the RegionInfos in the old list, it adds any intersection objects in the new list to the old list.
After it has finished comparing every circle to the list, the partition is complete.
After building the partition, the Paint event handler loops through the RegionInfo objects calling their Draw methods. The Draw method fills a partition with a color that depends on the partition's count (so partitions with larger counts get brighter colors). It then uses techniques described in the example Find a Region's centroid in C# to find the partition's centroid and draws the count there.
(Note that you can use this technique to partition an area with regions of any shape, not just circles.)
I am almost there with your examples but how do i get only the highest centroid. Thanx Reply to this
8/28/2010 7:15 AMRod Stephens wrote:
I'm not sure I understand. Do you mean only the region with the largest count? Just look through the list of RegionInfo objects to find the one with the largest Count value. Reply to this
Hello,
I am almost there with your examples but how do i get only the highest centroid. Thanx
Reply to this
I'm not sure I understand. Do you mean only the region with the largest count? Just look through the list of RegionInfo objects to find the one with the largest Count value.
Reply to this