# Subtracting Triangles / Triangle "Cutting"

This topic is 4893 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi I'm trying to create a new triangle mesh by "cutting" an area out of one triangle using the shape of another triangle (see image below), the thing is I have no idea what is the best way to accomplish this, I've tried googeling a bit but haven't come up with anything useful so far (might be using the wrong keywords though). Does anyone here have any experience doing something similar or know of any resources that might be worth while looking at. Thanks, Anders

##### Share on other sites
I'd probably recommend an open source library such as http://gts.sourceforge.net/

##### Share on other sites
Hi ander, there are two approaches you might take to this. If you have two triangulated polygons and you're trying to do a subtraction, you would use a sweep-line algorithm for polygon subtraction and to triangulate the result simultaneously (this is relatively straight-forward).

If you're ONLY subtracting one triangle from another here is a sensible approach:

Let's call the first triangle A, and the second triangle B.

We're subtracting A from B.

First determine how many of B's vertices are INSIDE A (note that even if none of them are, you may still have intersections). Second, find every intersection point, and reorder the edges of A such that intersections are either only on the first edge, the first and second, or all three (ie, not only on the second or similar).

You'll be able to construct a case analysis based on how many vertices of B are inside A and where the intersections happen. To illustrate using your pictures:

The first picture is 1 of B's verts inside A, and two intersections on the first edge of A. The second picture is all three vertices of B inside A, and the third picture is two vertices of B inside A, and two intersections on the first edge.

I've done this before (a while ago), iirc the result is about 30 cases.

Keep in mind, if this isn't a single triangle operation, you should look up sweep-line algorithms and use them.

##### Share on other sites
David Eberly has free source code to do this using BSP trees. It is under "Boolean operations on 2D polygons" under Applications/Miscellaneous Samples. See his web page here:

Geometric Tools: Applications

If you wish to do it yourself, BSP trees are not the optimum way to do things. I would recomment reading the document "Efficient Clipping of Arbitrary Polygons" by Gunther Greiner and Kai Hormann. Some hints. It is best to use the Bentley-Ottmann algorithm (this is the classic sweep-line algorithm that George suggested) to efficient locate intersection points. Also, if one polygon is contained entirely inside the other, e.g., you're cutting a clean hole out of the middle and you intend to ultimately triangulate this for display, you will need to subdivide the outer polygon before proceeding. You can triangulate the outer polygon, but this might not be sufficient. A single resulting triangle might still totally surround the hole, and so you'd still need to subdivide the triangle containing the hole shape. One more comment. If you will have possibly degenerate cases (coincident vertices, colinear edges, etc.) then you might want to look into John Hobby's paper on snap rounding, which can avoid a whole slew of computational geometry difficulties with computing the intersection points. I only provide all these ideas, since it is nontrivial to build a completely Boolean code, even in 2D.

##### Share on other sites
Thanks for the help guys, much appreciated, now I just have to figure out which of these is the least amount of work for doing what I want to do =)

/ Anders

1. 1
2. 2
Rutin
20
3. 3
khawk
16
4. 4
A4L
14
5. 5

• 11
• 16
• 26
• 10
• 11
• ### Forum Statistics

• Total Topics
633755
• Total Posts
3013706
×