Don't worry, I'm not selling anything. Just giving a shoutout to one of my open-source side projects: I created a programming language for beginners and am looking for people to test it out. If you have a spare moment (particularly if you haven't programmed before) and you're interested in making 2D games, please check it out. | ## Determining if a point is in a triangleSuppose you have 3 points in 2D space that represent a triangle. Now suppose you have another point and you want to know if that point is inside the triangle. How do you do that algorithmically without plotting it and doing a visual confirmation?This method is called walking-around-the-edge. Pretend you are walking around the edges of the triangle. Let's call our 3 corners A, B, and C. Let's call the point you're trying to figure out if it's inside or outside of the triangle P. - Start at A.
- Walk to B.
- Then Walk to C.
- Then walk back to A.
- If P was to your left the entire time, then it must have been inside the triangle and you must have been walking counter-clockwise.
- If P was to your right the entire time, then it also must have been inside the triangle and you must have been walking clockwise.
- If P switched from your left to right or vice versa, then it couldn't have been inside the triangle.
So now, given a vector, how do you determine if a point is to the left or the right? Cross product. ## Cross Product RefresherPoint Q = (q_{x}, q_{y})Point R = (r _{x}, r_{y})Point S = (s _{x}, s_{y})Point T = (t _{x}, t_{y})Vector QR = (r _{x} - q_{x}), (r_{y} - q_{y})Vector ST = (t _{x} - s_{x}), (t_{y} - s_{y})QR × ST = (r _{x} - q_{x}) * (t_{y} - s_{y}) - (t_{x} - s_{x}) * (r_{y} - q_{y})If AP × AB, BP × BC, and CP × CA all have the same sign, then P is inside triangle ABC. If not, then P is outside triangle ABC. If 2 of the cross products are 0 then P is at a corner. If 1 of the cross products is 0 then P is inside ABC if the other two cross products have the same sign. If 1 cross product is 0, another one is positive, and another is negative, then P is outside ABC. The beauty of this method is that it requires no complex math to implement. Everything you see is multiplication and subtraction. Also, if your original points are integers, then all your math will be in integers also. That makes computational time very fast when you do this on a computer making this ideal for use inside 3D graphics engines. Here is an example. See if the point is inside or outside the triangle. If you get stuck, raise your hand. A: (1, 2) B: (3, 5) C: (8, 3) P: (5, 4) |

You are visitor #

Get notified about new posts by following the newfangled twitter account.

© 1999 Nerd Paradise