Advertisement

2D Normals

Started by December 12, 2002 02:28 PM
4 comments, last by jonbell 22 years, 2 months ago
If i have 3 2d points which define a triangle, how do i calculate 3 perpendicular vectors (one for each edge) that each points towards the inside of the triangle?
Just like 3D you have to mind your winding(sp?). Your points have to either go consistantly clockwise or counter-clockwise around the triangle. Then you can use the displacement vector from one point to the next. A vector perpendicular and to the right of the vector (x,y) is (y,-x), i.e. (1,0) is (0,-1). To the left is (-y,x), i.e. (1,0) is (0,1). If you wind clockwise you want right, if you wind counter-clockwise you want left.
Keys to success: Ability, ambition and opportunity.
Advertisement
<this is off the top of my head>

If you''re in 2d, you can calc the slope of the edge, then invert and negate it. The angle is atan( -1.0/slope ), or atan2( -dx,dy ). You could also calc the angle of the edge and add or subtract PI/2 (90 degrees).

The problem with this is that the angle produced will be perpindicular to the triangle, but not necessiarly point towards the interior.

Two ways to do that may be:
1) calc the angle each side makes with the x-axis, and decide if it''s normal should have a a greater angle or a lesser angle.

2) calc the angle''s of all 3 edges as a preprocess, compare the angle of the current edge with the angle of the next edge. If the angle of the angle of the current edge is less than the next edge, it will be the angle of the current edge + 90, otherwise it''s - 90.

3) as an optimization of 2 that i just thougth of, find the winding of the triangle: clockwise, each perp angle is the edge angle - 90, counterclockwise, each perp is angle + 90.

some pseudocode:
assume the points are (x0,y0), (x1,y1), and (x2,y2)


  // calc all 3 anglesint dx10 = x1 - x0;int dy10 = y1 - y0;int dx21 = x2 - x1;int dy21 = y2 - y1;int dx02 = x0 - x2;int dy02 = y0 - y2;double a0 = atan2( dy10,dx10 );double a1 = atan2( dy21,dx21 );double a2 = atan2( dy02,dx02 );bool WindsCW = <arrgh>double p0;double p1;double p2;if( WindsCW ){    p0 = a0 - PI_OVER_2;    p1 = a1 - PI_OVER_2;    p2 = a2 - PI_OVER_2;}else{    p0 = a0 + PI_OVER_2;    p1 = a1 + PI_OVER_2;    p2 = a2 + PI_OVER_2;}  


Deciding if it winds clockwise is the hard part, and though I know some algorigthms for it, I don''t off the top of my head.

Look that up and you should be able to pull it off.
-- Succinct(Don't listen to me)
Oops - little buddy wizer beat me to it!

Yup - that''s a beautiful, elegant solution.
-- Succinct(Don't listen to me)
i''m still having a few problems implementing this. Maybe a little more info will help.

The triangle was 3D but i have collapsed it into 2D. I have a point within the triangles 2d plane that i now want to check to see if it is inside this 2d triangle.

Any thoughts?
This is the classic point in polygon problem. Check the Forum FAQ, at the bottom. Else, use Google.

This topic is closed to new replies.

Advertisement