Click here to Skip to main content
14,932,691 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have 200 Polygons.
Each polygon is made up of thousands of Points.(C#)

public Region GetGraphicsRegion(PointF offsetPoint, float ratioMicron, Graphics gfx = null)
Region region = null;

foreach (Polygon polygon in Polygons) // about 20000 Polygons
Region subRegion = polygon.GetGraphicsRegion(offsetPoint, ratioMicron, gfx); // polygon is made up of thousands of Points.

if (region == null)
region = subRegion;

if (polygon.isHole)


if (gfx != null)
if (isPolarity())
gfx.FillRegion(Brushes.Yellow, region); // It takes about 15hours
gfx.FillRegion(Brushes.DarkBlue, region);

return region;

Why is it taking so long?
Is there any way to improve it?

What I have tried:

Is it possible to modify the gfx.FillRegion to get the same result by moving it into the foreach statement?
Updated 25-Mar-21 22:44pm

1 solution

No, probably not.
Look at your data:
20,000 polygons.
Each polygon is "thousands of points".
And you are either excluding or including the region in a composite.

The resulting region is going to be highly complex, and require significant processing to decide if a point is inside or outside - the only way to speed it all up is to simplify the region.

I'd start by finding where the 15 hours is being taken: the Stopwatch class[^] will help you isolate that to either the "build a region" or "draw the region" sections. Personally, I'd guess it's the former rather than the later, but I haven't looked too closely at how Union and Exclude work - it's possible that they defer execution until the Region is used, which could explain why the draw is appearing to be slow.
BillWoodruff 27-Mar-21 20:17pm

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900