2二维向量积 [英] Cross product of 2 2D vectors

查看:208
本文介绍了2二维向量积的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以提供一个函数,该函数返回的两个 2D向量的积的例子吗?我想实现这个算法

C code会很大。谢谢你。


编辑:找到另一种方式待办事项它是适用于2D和死容易

 布尔tri2d :: inTriangle(vec2d PT){
    浮动AB =(pt.y-p1.y)*(p2.x-p1.x) - (pt.x-p1.x)*(p2.y-p1.y);
    浮动的CA =(pt.y-p3.y)*(p1.x-p3.x) - (pt.x-p3.x)*(p1.y-p3.y);
    浮BC =(pt.y-p2​​.y)*(p3.x-p2.x) - (pt.x-p2.x)*(p3.y-p2​​.y);

    如果(AB * BC> 0.f和放大器;与BC * CA> 0.f)
        返回true;
    返回false;
}
 

解决方案

(注:的2个向量跨产品只被定义在3D和的 7D空间。)

在code计算的以Z 的2载体躺在-component的 XY 的 - 平面:

  vec2D A,B;
...
双Z = a.x * b.y  -  b.x * a.y;
返回Z者除外;
 

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;

这篇关于2二维向量积的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆