Triangle math problem
Here is my problem. Say I have a function like this:
InTriangle( x1, y1, x2, y2, x3, y3, pointX, PointY );
What this function sould do is based on three X,Y coordinates and a third point, return true is the point is inside the triangle of the first 3 coords. So basicly the first three points make a triangle if graphed, and the last point is a free floating point. I''ve asked this question before, but all the answers I got didn''t quite work, but they were on the right track. It would be one huge ass help if someone could demonstrate how to do it.
-Dan
Test to see if it''s on the triangle plane. If it is, then test it against each of the lineas of the triangle to see which side it''s on (assuming proper vertex ordering, "inside" and "outside" will be indicated implicitly).
OK. I''m used to dealing with the 3D analogy to this, so I''ve
reduced the math to 2D to give the following:
Let the triangle have vertices ''a'' ''b'' ''c'' and your point
which you are testing is ''p''
define a function P( U,V ) which takes two vertices.
P( U, V ) = Ux * Vy - Vx * Uy
Compute the values: i = P( a - p, b - p )
j = P( b - p, c - p )
k = P( c - p, a - p )
( where a,b,c and p are 2D vectors )
if ''i'' ''j'' and ''k'' are all positive or all negative then the point ''p'' is inside the triangle otherwise it is outside.
NOTE: In 3D we replace ''P'' by the cross product to give:
i = ( a - p ) x ( b - p )
j = ( b - p ) x ( c - p )
k = ( c - p ) x ( a - p )
also note that if i,j or k is zero then this indicates the
point sits on the line. consider 0 either as positive or negative consistently as you wish.
Hope this helps.
reduced the math to 2D to give the following:
Let the triangle have vertices ''a'' ''b'' ''c'' and your point
which you are testing is ''p''
define a function P( U,V ) which takes two vertices.
P( U, V ) = Ux * Vy - Vx * Uy
Compute the values: i = P( a - p, b - p )
j = P( b - p, c - p )
k = P( c - p, a - p )
( where a,b,c and p are 2D vectors )
if ''i'' ''j'' and ''k'' are all positive or all negative then the point ''p'' is inside the triangle otherwise it is outside.
NOTE: In 3D we replace ''P'' by the cross product to give:
i = ( a - p ) x ( b - p )
j = ( b - p ) x ( c - p )
k = ( c - p ) x ( a - p )
also note that if i,j or k is zero then this indicates the
point sits on the line. consider 0 either as positive or negative consistently as you wish.
Hope this helps.
September 29, 2001 07:31 AM
My anonymous question:
How do you find the "center" of a triangle, a point from which the three other points are equally far from? (Codewise!)
You can do it by calculating the normals from the triangles lines, make them go through the middle of the lines, and then see where the normals intersect.
How would you code this?
How do you find the "center" of a triangle, a point from which the three other points are equally far from? (Codewise!)
You can do it by calculating the normals from the triangles lines, make them go through the middle of the lines, and then see where the normals intersect.
How would you code this?
Actually, you can find the center of the triangle by adding up the vectors and dividing by three.
center = (a + b + c)/3
center = (a + b + c)/3
Pretty cool discussion, me thinks. How about finding the center of a 4-sided polygon? Just add the 4 vertices and divide by 4? Would this work for non-rectangular polygons as well? And how about finding the center of a cube? Just add the 8 corner vertices of the cube and divide by 8?
It would be cool if that works... would make my life a much more easier. :-)
Bye,
Jayce
It would be cool if that works... would make my life a much more easier. :-)
Bye,
Jayce
I tried out the 4 sided polygon, and yes, all you do is add those up and divide by 4. My guess is that it would work on a cube too. Thanks people
-Dan
The formula will work for pretty much any polygon or 3d shape. But you have to understand when I say "center", I mean the center of mass. If you remember your physics, the center of mass is calculated by multipling each component position by the mass, then dividing by the total mass. However, in the case of polygons, you just assume each vertex "weighs" one unit. The equation then turns out to be just an average of the verticies.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement