Reputation: 1453
In clipping algorithms there are many techniques such as Cohen–Sutherland,Cyrus–Beck algorithms for 2D line clipping also, there are many others for Circle and polygon. but I'm looking for such a way to clip line to a triangle window in c++ like as explained in image bellow :
So I have three cases first one is that the line is inside the triangle which must be drawn, second case is out side the triangle which must not be drawn, third case the line has an end point inside the triangle and second outside which needs to be clipped to the triangle border, So what is a best way to do this with respect to performance of processing?!
Upvotes: 1
Views: 1912
Reputation:
The basic principles you need here are calculating the intersection points of each side of the triangle and working out whether the intersection point is inside the edge or beyond the corners (which the intersection algorithm should give you).
Basically, the intersection of line segment A-B against triangle side C-D will give you an intersection time where A and C represent time = 0, and B and D represent a time of 1. Any values between 0 and 1 for both line segments mean they intersect, and you'll you need to modify the line segment you're testing such that the point outside the triangle is placed on the intersection point. Any value outside that range means you can ignore that side of the triangle. (Either the line segment is completely outside the triangle, or you'll clip it with the other two sides.)
You just do that for each side in turn.
Upvotes: 3