8

I have the following piece of pseudo-C/Java/C# code:

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

How do I compute the cross-product ABxAC?

SyntaxT3rr0r
  • 27,745
  • 21
  • 87
  • 120
  • 1
    From your previous question - "How do I programmatically compute the dot-product/cross-product?" Presumably the sign is the sign of whatever the result of that computation is, so I would say this is adupe. –  Mar 28 '10 at 13:20
  • Why the title is dot-product but the tag is cross-product? – kennytm Mar 28 '10 at 13:42
  • @KennyTM: Fixed. That's because he first thought cross and dot product are the same. – Georg Schölly Mar 28 '10 at 13:45
  • Looking at the answer to the previous question, which is actually correct, he does need the cross product. But the calculation is reduced as the Z component of all the points is considered to be 0. See my answer below. (hope i got all the math right) – Harald Scheirich Mar 28 '10 at 14:14
  • 1
    Everyone: OK now this is a real case where the StackOverflow / "all editable Wiki" is really showing its limit. I made several errors and tried to "edit" them, then meanwhile there have been great answers and comments but some appeared to be "wrong" because of the edits. In such a case a classical discussion thread would have been just *so* much better. Because at least you can't edit / have answers not matching the question anymore etc. (my fault and not my fault, it's a problem when trying to have a "discussion" on a Wiki site). This is a *major* SO usability issue in my opinion. – SyntaxT3rr0r Mar 28 '10 at 15:04
  • Everyone: thanks a lot that said, I gave +1 to everyone. I just screwed up the terminology and the edits. I *hate* it that questions are editable on SO and I just showed myself, by mistake, what is one of the major pitfall of using a Wiki for programming related questions. Because questions sometimes can be non-subjective yet need discussion and Wiki... And tiny comments aren't the right place to have a discussion. Can't wait for the next expert/Usenet/SO thing. It's just going to be so much better :) – SyntaxT3rr0r Mar 28 '10 at 15:08

5 Answers5

7

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

Community
  • 1
  • 1
Harald Scheirich
  • 9,676
  • 29
  • 53
  • @Harald Scheirich: thanks for detailed explanation and great links. You meant *"with Az and Bz being zero..."* ? (I don't dare to edit your answer) – SyntaxT3rr0r Mar 28 '10 at 14:26
  • @Harald Scheirich: isn't this exactly the same code as the one tzaman gave? (I don't know who voted him down) – SyntaxT3rr0r Mar 28 '10 at 14:30
  • @Wizard Yes that is the same calculation, the voting down might have started with the title and subject of this question being changed (dunoo who did that) – Harald Scheirich Mar 28 '10 at 14:35
4

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
Donal Fellows
  • 133,037
  • 18
  • 149
  • 215
2

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.

unwind
  • 391,730
  • 64
  • 469
  • 606
  • @unwind: My bad, I thought dot "product" and cross-product were identical but, yes, indeed I'm after the "dot product", which is also the "scalar product": http://en.wikipedia.org/wiki/Dot_product – SyntaxT3rr0r Mar 28 '10 at 13:26
  • No you were right in the first place, you are not looking for the dot product, it will not give you whether your point is to the left or the right of your line. – Harald Scheirich Mar 28 '10 at 13:48
2

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.

duffymo
  • 305,152
  • 44
  • 369
  • 561
1

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
tzaman
  • 46,925
  • 11
  • 90
  • 115
  • @Donal: nope, that's the case for a dot-product (which this isn't). @WizardOfOdds: You're welcome. Note that if this is what you want, it isn't actually the dot-product you're after, but the sign of one component of the cross-product. – tzaman Mar 28 '10 at 13:46
  • I misread what you wrote and assumed that it was an answer for the question. Turned out the questioner didn't originally know the difference between dot and cross products, which is somewhat important, but by the time I read the question it had already been corrected/clarified. It was all just a mixup. – Donal Fellows Mar 28 '10 at 14:14
  • @Donal: I figured as much; no worries. :) – tzaman Mar 28 '10 at 14:44