9

Can anyone provide an example of a function that returns the cross product of TWO 2d vectors? I am trying to implement this algorithm.

C code would be great. Thanks.


EDIT: found another way todo it that works for 2D and is dead easy.

bool tri2d::inTriangle(vec2d pt) {
    float AB = (pt.y-p1.y)*(p2.x-p1.x) - (pt.x-p1.x)*(p2.y-p1.y);
    float CA = (pt.y-p3.y)*(p1.x-p3.x) - (pt.x-p3.x)*(p1.y-p3.y);
    float BC = (pt.y-p2.y)*(p3.x-p2.x) - (pt.x-p2.x)*(p3.y-p2.y);

    if (AB*BC>0.f && BC*CA>0.f)
        return true;
    return false;    
}
  • Is this for work or home work? – legends2k Feb 25 '10 at 10:39
  • This is for personal enjoyment. Why? –  Feb 25 '10 at 10:46
  • dup http://stackoverflow.com/questions/243945/calculating-a-2d-vectors-cross-product – jk. Feb 25 '10 at 10:47
  • 4
    @tm1rbt -- Because SOers like to know when we are doing or helping with homework so that we can claim the credit transfers to our own academic transcripts. – High Performance Mark Feb 25 '10 at 10:47
  • don't be so rude and try reading it again "Given that the definition requires at least three dimensions, how does one calculate the cross product of two 2d vectors?" – jk. Feb 25 '10 at 11:04
  • @tm1rbrt: Just to know; because I know people who don't do either, elude their work and get it done thru' SO. Usually SO is for asking doubts; helping is something, but doing someone else's work is totally something else. Btw, don't use strong words like _moron_. – legends2k Feb 25 '10 at 11:07
  • 8
    How on earth can you ask for a function to calculate a cross-product, accept an answer that's incorrect, and then post a function that returns a boolean? I'm voting this down and voting to close. – duffymo Feb 27 '10 at 05:33
  • Agree with @duffymo. I think the topic creator wanted to calculate a cross product for his algorithm, but ended up using a different method that doesn't need vector algebra at all. But I'm not voting this question down. – Max Apr 02 '13 at 06:20

2 Answers2

21

(Note: The cross-product of 2 vectors is only defined in 3D and 7D spaces.)

The code computes the z-component of 2 vectors lying on the xy-plane:

vec2D a, b;
...
double z = a.x * b.y - b.x * a.y;
return z;
kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
  • Wow. Would like to give you an extra +1 for that link! – AakashM Feb 25 '10 at 10:51
  • 2
    @tm1rbrt: That `CrossProduct` should be a full 3D cross-product. You can always add back the two 0 components. – kennytm Feb 25 '10 at 11:44
  • 1
    The cross product of two vectors in 3D space is a 3D vector, yet your code only returns a double. What good is one component? – duffymo Feb 26 '10 at 02:41
  • 2
    The 3-D cross product of two vectors in the x/y plane is always along the z axis, so there's no point in providing two additional numbers known to be zero. Another way to look at it: the closest 2-D equivalent to a 3-D cross product is an operation (the one above) that returns a scalar. – comingstorm Feb 26 '10 at 05:47
  • Also, I want to assure you that the above operation will in fact work correctly with the algorithm you link to. If you work it out, the dot product of the resulting cross product vectors simplifies to a simple product of the resulting scalars. Your updated version above could be considered a simplified (and nicely symmetric!) version of this: by reducing the problem to comparing the signs of the resulting scalars, you can determine that the orientation of the original triangle is redundant. – comingstorm Feb 26 '10 at 06:23
  • @comingstorm - 2D doesn't necessarily mean in the xy-plane. I understand vectors well enough to know what special case you're talking about, but there's nothing in the original question that spells out that restriction. The updated answer that's provided is the correct, general answer. In spite of the fact that your answer was accepted it's not correct for the general case. – duffymo Feb 27 '10 at 05:32
  • Hey, first off, that's not *my* answer; second, it is, in fact, correct. The OQ was specifically about 2D vectors: an answer phrased in terms of 3D vectors would be missing the point, and therefore incorrect. Also, if you take a look at the link in the OQ, you will notice that his update, which you slagged for returning a boolean, is a (correct) implementation of his declared objective -- a point-in-triangle algorithm implemented for 2D vectors. – comingstorm Mar 02 '10 at 07:23
0

Mathworld Cross Product

High Performance Mark
  • 77,191
  • 7
  • 105
  • 161