Advertisement

Calculating if a point is within a triangle?

Started by October 03, 2002 01:04 PM
30 comments, last by DevLiquidKnight 22 years, 4 months ago
Do any of you have a basic c++ function(perferrably) or even any functions in any languages that calculate dot product efficently yet optimal. I don't know dot prodcut and it just confuses me to look ill look around to see what i can find but if you do it will be helpful. Or even just a function to test if an object is in a triangle.

[edited by - DevLiquidKnight on October 4, 2002 11:37:33 PM]
It looks as if dot product is used for 3D applications let me say that this is not in a 3D envoirment i want to check for this using 2D
Advertisement
Dot products can be calculated for 2D-lines.

p . q = (px * qx) + (py * qy)

So, in C/C++ code it becomes something along the lines of this:
float dot_product(float x1, float y1, float x2, float y2){    return x1*x2 + y1*y2;    // (x1,y1) . (x2,y2)} 


For the 3D case it''s similar, just throw the Z-coordinates in there as well.
Eh still confused on how to do this exactly lol
Exactly how to do what? He gave you the formula for the dot product (code even), and I gave you the formula for finding what side the point is on.

Do you need to know the side normal? Since this is 2D, we can actually fabricate a vector that is used in the cross product to find the normal. So let''s say you had two points, (1,1) and (10,4). This vector is then (9,3). However, since the cross product is a 3D operation, we add a Z coordinate, 0 -> (9,3,0). But now we need another vector. This is the vector that can be fabricated. We create a vector that has the same X and Y, BUT we give it a Z value to make it perpendicular to the 2D plane, and thus the first vector -> (9,3,Z), where Z is any non-zero number. We can then take the cross product between the two vectors to get the perpendicular vector. We normalize it to get the normal. However, be wary. The cross product returns a resultant vector using the right hand rule, and combined with the fact that your fake Z can either be positive or negative, you have to make sure you know which way the normal will face.
EH its just im confused i never done this before and i have no clue what any of these terms mean i have only tooken highshcool classes on math and not very high classes only algebra 1 2 and geometry lol..
Advertisement
solution:

structure triangle
{
CVector Points[3];
CVector Edge[3];
}

Triangle:ointIsIn(CVector CheckPoint)
{
Edge[0] = Point[1]-Point[0];
Edge[1] = Point[2]-Point[1];
Edge[2] = Point[0]-Point[2];

DistanceFromEdge[0]=Point[1]-(CheckPoint-DotProduct(Edge[0],Point[1]-CheckPoint));

//same ... for the 2 edges left.

if(DistanceFromEdge[0] < 0 && DistanceFromEdge[1] < 0 && DistanceFromEdge[2] < 0)
return 1;
else
return 0;
}

hey guys... check if there are an error in the DistanceFromEdge Formula... I wrote this witout test it, but I know the principe is correct.
hmmm.... I found that on the net and I tested it.

D3DVECTOR closestPointOnLine(D3DVECTOR a, D3DVECTOR b, D3DVECTOR p)
{
// Determine t (the length of the vector from ‘a’ to ‘p’)

D3DVECTOR c = p - a;
double d = Magnitude(b-a);
D3DVECTOR V = Normalize(b - a);
double t = DotProduct(V,c);

// Check to see if ‘t’ is beyond the extents of the line segment

//you dont need these 2 lines for DistanceFromEdge
if (t < 0) return a;
if (t > d) return b;

// Return the VECTOR between the nearest Point on the line and ''p''

V*=t;
return (a + V)-p; // nearest Point = a + V
}
what is CVector Edge[3]; for?
CVector Edge is a usefull data.. instead of Point[1]-Point[0], write Edge[0]...

I use that in
DistanceFromEdge[0]=Point[1]-(CheckPoint-DotProduct(Edge[0],Point[1]-CheckPoint));

Do you know how to use the very useful DotProduct in geometry?

This topic is closed to new replies.

Advertisement