2 个 2D 向量的叉积

发布于 2024-08-22 11:58:12 字数 575 浏览 3 评论 0原文

谁能提供一个返回两个二维向量叉积的函数示例?我正在尝试实现此算法

C 代码会很棒。谢谢。


编辑:找到了另一种适用于 2D 并且非常简单的方法。

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜心小果奶 2024-08-29 11:58:12

(注意: 2 个向量的叉积仅在 3D 和 中定义7D 空间。)

该代码计算位于 xy 平面上的 2 个向量的 z 分量:

vec2D a, b;
...
double z = a.x * b.y - b.x * a.y;
return z;

(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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文