Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boolean operations on rectangle polygons

Avast there fellow programmers!

I have the following problem:

I have two rectangles overlapping like shown on the picture below.

alt text

I want to figure out the polygon consisting of point ABCDEF.

Alternate christmas description: The red cookie cutter is cutting away a bit of the black cookie. I want to calculate the black cookie.

Each rectangle is a data structure with 4 2d-vertices.

What is the best algorithm to achieve this?

like image 850
Nailer Avatar asked Dec 22 '08 15:12

Nailer


People also ask

How many types of Booleans are there in Polygon?

Booleans (Mesh > Booleans) let you model with polygonal objects. Three boolean operations let you combine objects to make shapes that would otherwise be difficult to model using other techniques. You can add, subtract, or intersect objects to create a new, complex shape.

What are the Boolean operations used in solid modeling?

What are the three Boolean operations? Union, subtract, and intersect.

What are the 3 types of Boolean operations and how do they work?

Some Types of Boolean Operators Logic operators or conditionals could be: Denial: It is NOT fulfilled. Conjugation: One thing AND another is fulfilled. Disjunction: One thing OR another is fulfilled.

What are Boolean operations in 3D modeling?

In 3D Modeling, by Boolean operations we mean creating intersections and unions of objects, as well as subtracting objects from each other. All these are set operations that students know from Venn diagrams.


3 Answers

This is a special case of general 2D polygon clipping. A good place to start is the Weiler-Atherton algorithm. Wikipedia has a summary and links to the original paper. The algorithm seems to match the data structure you've described pretty well.

Note that it's quite possible you'll end up with a rectangle with a hole in it (if the red one is entirely inside the black one) or even two rectangles (eg if the red is taller and skinnier than the black). If you're certain there is only one corner of the red rectangle inside the black one then the solution should be much simpler.

like image 58
jblocksom Avatar answered Oct 05 '22 04:10

jblocksom


constructive solid geometry

like image 39
Steven A. Lowe Avatar answered Oct 05 '22 04:10

Steven A. Lowe


How precise are the coordinates? If the rectangles are fairly small, the easiest approach might be to just paint them on a canvas, black rectangle first, followed by red. The remaining black pixels on the canvas are the polygon that's left.

Another approach is to split the coordinate grid into a bunch of rectangles based on all of the sides of the rectangles (not counting unbounded rectangles, you have up to 9 rectangles generated if you have two original rectangles). Then just test a representative point from each of these rectangles for membership in the particular polygons to determine which rectangles are in and which are out.

like image 33
Yuliy Avatar answered Oct 05 '22 04:10

Yuliy