Tuesday, July 28, 2009

What's the algorithm to determine a collision between a static line segment and a static polygon(triangle)?

please help... if possible a referal to a website or post your pseudo code.. i'm working with C++.. thank you very much

What's the algorithm to determine a collision between a static line segment and a static polygon(triangle)?
This is the algorithm:


we note the static line as:(x1,y1) (x2,y2)


we note the n th vertex of the polygon as (An,Bn):








for (i=0 ; i%26lt; n ; i ++){ //for each edge


if ( (x1,y1) is on one side of the line (Ai,Bi) (A[i+1],B[i+1])


and (x2,y2) is on the other side of the same line)


AND ( (Ai,Bi) is on one side of the line (x1,y1) (x2,y2)


and (A[i+1],B[i+1]) is on the other side of the same line )


then there is intersection between those lines


other wise check the next line (continue)


}





to know whether a point is on the left or on the right of some line


use this algorithm:


point (x,y) line (a,b) - (c,d)


calculate the determinant of the following matrix:


|a b 1|


|c d 1|


|x y 1|


if it is positive - the point is on the right


if it is negative - the point is on the left


if it is zero - it is on the line.





I know you need some background in math for this - but this is


the best way to do it.


No comments:

Post a Comment