Sign in to follow this  
goon15

Clip Triangle to Square approach

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 this post


Link to post
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).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this