I have a complicated polygon in the 2d plane, that is an RegionIntersection
of RegionUnion
of triangles.
Finally, I want to calculate it's Area. Unfortunatly, the Area
is really slow. I am not sure why, but presumably because it calculates the result analytically.
I have an example that represents the shapes I am dealing with:
TotalTime = 0;
For[cc = 1, cc <= 50, cc++,
(* Reconstructing roughly the shapes I am using *)
BoundIntersect = RegionIntersection[
RegionDifference[Triangle[RandomReal[{-0.2, 0.2}, {3, 2}]],
Polygon[RandomReal[{-0.2, 0.2}, {3, 2}]]],
RegionUnion[Triangle[RandomReal[{-0.2, 0.2}, {3, 2}]],
Triangle[RandomReal[{-0.2, 0.2}, {3, 2}]],
Triangle[RandomReal[{-0.2, 0.2}, {3, 2}]],
Triangle[RandomReal[{-0.2, 0.2}, {3, 2}]],
Triangle[RandomReal[{-0.2, 0.2}, {3, 2}]]]];
CurrentTime = AbsoluteTime[];
BoundaryIntersectRatio = Area[BoundIntersect];
TotalTime += (AbsoluteTime[] - CurrentTime);
];
Print["Total Time: " <> ToString[TotalTime] <> " sec."];
Can you speed it up? In particular, error-ratios in the order of $10^{-4}$ are tolerable.
Update (25.12.2019):
A fair comparison (100 iterations with same polygons for each method):
- Henrik Schumacher's method using undocumented Graphics`PolygonUtils function: 0.75005 sec.
- Alx's suggestion using
BoundaryDiscretizeRegion:
5.03149 sec. - My original method: 24.50163 sec.
Result: Speedup of >factor 30! And learned about undocumented function that i can use all over my code. Fantastic, thank you!
BoundaryDiscretizeRegion
:Area@RegionIntersection[BoundaryDiscretizeRegion@RegionDifference[...],BoundaryDiscretizeRegion@RegionUnion[...]]
. $\endgroup$ – Alx Dec 24 '19 at 2:32