简体   繁体   English

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

[英]Check if a vector is between two others

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): 首先,我写了像(伪)的simething:

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 如果你想知道,这是我正在使用的Vector和它的方法: http//www.jmonkeyengine.com/doc/com/jme/math/Vector3f.html

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: 一个更直观(但轻微慢)的变体是在每个轴上使用min / max:

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

Here is a general solution for a box that may not be even right angles, ie 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. 如果然后抛出要通过此变换测试的矢量,则只需要检查X,Y和Z是否都位于0和1之间。

Consider a corner point on your box to be your origin. 考虑一下你的盒子上的角点作为你的起源。 Let's call that K. Now construct your three principal axes PQR as the vectors that extend along the three edges that touch this point. 让我们称之为K.现在构造你的三个主轴PQR作为沿着接触这一点的三条边延伸的向量。

Now any point in three-dimensional space can be represented as K + aP + bQ + cR . 现在,三维空间中的任何点都可以表示为K + aP + bQ + cR Moreover, there is only one (a, b, c) that satisfies. 而且,只有一个(a,b,c)满足。

If you can determine (a, b, c) , you simply need to check that each is between 0 and 1. 如果你可以确定(a,b,c) ,你只需要检查每个是否在0和1之间。

If anyone is interested in the matrix math, give me a bell! 如果有人对矩阵数学感兴趣,请给我一个钟!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM