开发者

How to compute the cross-product?

开发者 https://www.devze.com 2022-12-24 04:40 出处:网络
I have the following piece of pseud开发者_开发技巧o-C/Java/C# code: int a[]= { 30, 20 }; int b[] = { 40, 50 };

I have the following piece of pseud开发者_开发技巧o-C/Java/C# code:

int a[]= { 30, 20 };
int b[] = { 40, 50 };
int c[] = {12, 12};

How do I compute the cross-product ABxAC?


The solution that was given to you in your last question basically adds a Z=0 for all your points. Over the so extended vectors you calculate your cross product. Geometrically the cross product produces a vector that is orthogonal to the two vectors used for the calculation, as both of your vectors lie in the XY plane the result will only have a Z component. The Sign of that z component denotes wether that vector is looking up or down on the XY plane. That sign is dependend on AB being in clockwise or counter clockwise order from each other. That in turn means that the sign of z component shows you if the point you are looking at lies to the left or the right of the line that is on AB.

So with the crossproduct of two vectors A and B being the vector

AxB = (AyBz − AzBy, AzBx − AxBz, AxBy − AyBx)

with Az and Bz being zero you are left with the third component of that vector

AxBy - AyBx

With A being the vector from point a to b, and B being the vector from point a to c means

Ax = (b[x]-a[x])
Ay = (b[y]-a[y])
Bx = (c[x]-a[x])
By = (c[y]-a[y])

giving

AxBy - AyBx = (b[x]-a[x])*(c[y]-a[y])-(b[y]-a[y])*(c[x]-a[x])

which is a scalar, the sign of that scalar will tell you wether point c lies to the left or right of vector ab

Aternatively you can look at stack overflow or gamedev


Assuming whether you're asking whether the angle between AB and AC is acute or obtuse, you want this:

int a[]= { 30, 20 };
int b[] = { 40, 50 };
int c[] = {12, 12};

int ab_x = b[0] - a[0];
int ab_y = b[1] - a[1];
int ac_x = c[0] - a[0];
int ac_x = c[1] - a[1];

int dot = ab_x*ac_x + ab_y*ac_y;
boolean signABxAC = dot > 0; // pick your preferred comparison here


The cross product is a vector, it doesn't have "a sign".

Do you mean the scalar (dot) product? If you do, then that is computed as for a pair of vectors [a,b,c]•[d,e,f] as ad + be + cf, so the sign of that expression is the sign of the dot product.

Figuring out the sign without doing the multiplications and adds is probably not faster than just doing them.


Since all three points have just two components, I'll assume that the z-component for all three is zero. That means that the vectors AB and BC are in the xy-plane, so the cross-product is a vector that points in the z-direction, with its x and y components equal to zero.

If by "sign" you mean whether it points in the positive or negative z-direction, the computation will tell you that.

In your case, the two vectors are AB = (10, 30, 0) and AC = (-18, -8, 0). If I take the cross-product of those two, I get vector AB X AC = (0, 0, 460). Do you mean to say that this has a positive sign because the z-component is positive? If yes, that's your answer.

UPDATE: If it's the scalar product you want, it's negative in this case:

AB dot AC = -180 -240 + 0 = -420.


From reading the question you linked, it seems you want the sign of the z-component of the cross-product (assuming 0 z-value for AB and AC); to indicate which side of the line AB the point C lies.
Assuming that's the case, all you need is the sign of the determinant of the matrix with AB and AC as its rows.

xAB = b[0] - a[0]
yAB = b[1] - a[1]
xAC = c[0] - a[0]
yAC = c[1] - a[1]
detABxAC = (xAB * yAC) - (yAB * xAC)
if (detABxAC < 0) 
  // sign is negative
elif (detABxAC > 0) 
  // sign is positive
else 
  // sign is 0, i.e. C is collinear with A, B
0

精彩评论

暂无评论...
验证码 换一张
取 消