开发者

Cross product of 2 2D vectors

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;    
}


(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;


Mathworld Cross Product

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜