Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compute union of two arbitrary shapes

I'm working on an application, I need to be able to combine two overlapping arbitrary shapes as drawn by the user. This would be a Union operation on the two shapes. The resultant shape would be the silhouette of the two overlapping shapes.

The shapes are stored as a sequence of points in a clockwise manner.

Ideally I'd like an algorithm which will take two arrays of Points (x,y) and return a single array of the resultant shape.

I've been reading Wikipedia on Boolean operations on polygons which mentions the Sweep line algorithm but I can't make the link between this and my goal, alas I'm not a Mathematician.

I'm developing the application in ActionScript 3 but I'm familiar with C#, Java and I can pick my way through C and C++.

like image 472
Greg B Avatar asked Jan 26 '10 14:01

Greg B


4 Answers

Implementing boolean operations correctly is not trivial; fortunately, there are libraries that already implement this functionality.

What language are you using? If it's C++, take a look at CGAL, the Computational Geometry Algorithms Library.

like image 179
Martin B Avatar answered Nov 20 '22 08:11

Martin B


Given two lists of points (A and B)
- [ 1 ] for each line in A does it intersect a line in B
-.- [2] if no (more) lines intersect, there is no overlap
-.- [3] if a line in (A) intersects a line in B then
-.-.- [4] add the point of intersection into output
-.-.- [5] does the next line from A intersect B
-.-.-.- [6] if not, add this to output (it's inside B) goto 5
-.-.-.- [7] if so, add the intersect to output and switch lists A & B goto 2

Also see Intersection Point Of Two Lines. I'm not gonna write the code sorry :)

like image 42
Dead account Avatar answered Nov 20 '22 10:11

Dead account


See also GPC.

like image 3
lhf Avatar answered Nov 20 '22 08:11

lhf


Would this algorithm work for you?

like image 2
Jon Cage Avatar answered Nov 20 '22 09:11

Jon Cage