检查一个向量是否在另外两个向量之间

发布于 2024-10-01 04:22:24 字数 527 浏览 6 评论 0原文

我有一个由两个三维向量组成的“盒子”。一个用于前左下角,一个用于后右上角。

有没有简单的方法来检查第三个三维向量是否在这个“盒子”内的任何地方?

首先,我写了类似 (psuedo):

p = pointToCompare;
a = frontLowerLeft;
b = backUpperRight;

if(p.x >= a.x && p.x <= b.x && p.y >= a.y ...

但这仅在所有坐标均为正数时才有效,而它们并不总是如此。我应该做类似上面的事情,还是有更好/更简单的方法来进行这个计算?

如果您想知道,这是 Vector 及其我正在使用的方法: http://www.jmonkeyengine.com/doc/com/jme/math/Vector3f.html

I have a "box" made out of two three-dimensional vectors. One for the front-lower-left corner and one for the back-upper-right corner.

Are there any simple way to check if a third three-dimensional vector is anywhere inside this "box"?

First i wrote simething like (psuedo):

p = pointToCompare;
a = frontLowerLeft;
b = backUpperRight;

if(p.x >= a.x && p.x <= b.x && p.y >= a.y ...

But that does only work if all coordinates are positive, which they won't always be. Should i do something like the above, or are there any better/simpler way to do this calculation?

If you would like to know, this is the Vector and it's method i'm using: http://www.jmonkeyengine.com/doc/com/jme/math/Vector3f.html

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

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

发布评论

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

评论(2

忆梦 2024-10-08 04:22:24

如果你想让它更健壮一点,你可以使它对角的位置不变:

if (a.x <= p.x && p.x <= b.x || b.x <= p.x && p.x <= a.x) {
  // similar to the y- and z-axes.
}

一个更直观(但稍微慢一些)的变体是在每个轴上使用最小值/最大值:

if (Math.min(a.x, b.x) <= p.x && p.x <= Math.max(a.x, b.x)) {
  // ...
}

If you want to make it a little more robust, you could make it invariant to the position of the corners:

if (a.x <= p.x && p.x <= b.x || b.x <= p.x && p.x <= a.x) {
  // similar to the y- and z-axes.
}

A more intutive (but slightliy slower) variant would be to use min/max on each axis:

if (Math.min(a.x, b.x) <= p.x && p.x <= Math.max(a.x, b.x)) {
  // ...
}
梦亿 2024-10-08 04:22:24

这是针对可能不是直角的盒子(即通用平行六面体)的通用解决方案。

这里的技巧是找到将盒子转换为单位立方体的转换。如果您随后将要测试的向量放入此转换中,则只需检查 X、Y 和 Z 是否都位于 0 和 1 之间。

考虑盒子上的一个角点作为原点。我们称之为 K。现在将三个主轴 PQR 构造为沿着接触该点的三个边延伸的向量。

现在三维空间中的任何点都可以表示为K + aP + bQ + cR。而且,只有一个(a, b, c)满足。

如果你能确定(a, b, c),你只需要检查每个值是否在0和1之间。

如果有人对矩阵数学感兴趣,请给我打电话!

Here is a general solution for a box that may not be even right angles, i.e. a generic parallelepiped.

The trick here is to find the transformation that transforms your box into the unit cube. If you then throw your vector that you want to test through this transformation, you would just need to check that X, Y and Z all lie between zero and one.

Consider a corner point on your box to be your origin. Let's call that K. Now construct your three principal axes P Q R as the vectors that extend along the three edges that touch this point.

Now any point in three-dimensional space can be represented as K + aP + bQ + cR. Moreover, there is only one (a, b, c) that satisfies.

If you can determine (a, b, c), you simply need to check that each is between 0 and 1.

If anyone is interested in the matrix math, give me a bell!

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