Jump to content
  • Advertisement
Sign in to follow this  
goon15

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.

If you intended to correct an error in the post then please contact us.

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
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).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!