Clip Triangle to Square approach

Started by
0 comments, last by Zakwayda 14 years, 7 months ago
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!
Advertisement
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).

This topic is closed to new replies.

Advertisement