# Clip Triangle to Square approach

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

## Recommended Posts

I need to solve the issue of clipping a triangle to a rectangle, which will create polygons. Im just posting this to see if my approach is the best one, or if anyone else has solved this problem and had a better one. Basically what I have is this: http://img30.imageshack.us/img30/728/67775227.jpg Where the results would be the polygon in green would be the new set of triangles that would be displayed and the red would be clipped. My basic approach would be to check the intersection of the lines of the square with the lines of the triangle, starting at the top and going clockwise. If 0 or 1 lines of the square intersect, then the triangle is out of the square. If 2 or more intersect then find the intersection points and create a polygon out of the triangle, send that polygon to the tesselator and render it. There seems to be a bunch of corner cases with this, like if the triangle encompasses the rect, if one triangle edge intersects several lines of the rect, etc. Does anyone have any tips for this or a simpler way? Thanks!

##### Share on other sites
The standard solution to this problem is the Sutherland-Hodgman clipping algorithm. The algorithm itself is fairly straightforward, but care must be taken when implementing the algorithm using floating-point types (due to precision issues).

1. 1
2. 2
Rutin
16
3. 3
4. 4
5. 5

• 26
• 11
• 9
• 9
• 11
• ### Forum Statistics

• Total Topics
633701
• Total Posts
3013450
×