简体   繁体   English

我如何检查Vector3(x,y,z)是否在Java中的其他2个Vector3之间?

[英]How can i check if a Vector3 (x,y,z) is between 2 other Vector3's in Java?

I want to make a protection plugin for a Minecraft server software and I want to work with Vector3. 我想为Minecraft服务器软件制作一个保护插件,并且希望与Vector3一起使用。 I want to check if a Vector3 is between 2 Positions (Vector3). 我想检查Vector3是否在2个位置之间(Vector3)。

The Vector3 has got the values: x, y, and z. Vector3的值是:x,y和z。 How can I check now if a Vector is between 2 others? 现在如何检查Vector是否在2个之间?

Vector3 pos1 = new Vector3(100, 10, 100);
Vector3 pos2 = new Vector3(10, 100, 43);
Vector3 vector3tocheck = new Vector3(60, 23, 1); // it should be between the 2 positions

public boolean isInTwoVectors(Vector3 pos1, Vector3 pos2, Vector3 vector3tocheck) {
// Here idk how to continue.
}

I expect the result if it is in the two positions or not. 我期望结果是否在两个位置。

public boolean isInTwoVectors(Vector3 pos1, Vector3 pos2, Vector3 check) {
    int minX = Math.min(pos1.x, pos2.x);
    int maxX = Math.max(pos1.x, pos2.x);
    int minY = Math.min(pos1.y, pos2.y);
    int maxY = Math.max(pos1.y, pos2.y);
    int minZ = Math.min(pos1.z, pos2.z);
    int maxZ = Math.max(pos1.z, pos2.z);
    return check.x >= minX && check.x <= maxX && check.y >= minY && check.y <= maxY
        && check.z >= minZ && check.z <= maxZ;
}

Simply, check all the x, y, and z bounds for whether the vector lies within. 只需检查所有x,y和z边界,以了解向量是否位于其中。 For the record, in your example, the given vector would not be within the bounds, because its z-value is out of bounds (outside of [43,100]). 作为记录,在您的示例中,给定的矢量将不在范围内,因为其z值超出范围(在[43,100]之外)。 In this case (not caring about z-value), you'd only check x- and y-values, as so: 在这种情况下(不关心z值),您只需检查x和y值,如下所示:

public boolean isInTwoVectorsXY(Vector3 pos1, Vector3 pos2, Vector3 check) {
    int minX = Math.min(pos1.x, pos2.x);
    int maxX = Math.max(pos1.x, pos2.x);
    int minY = Math.min(pos1.y, pos2.y);
    int maxY = Math.max(pos1.y, pos2.y);
    return check.x >= minX && check.x <= maxX && check.y >= minY && check.y <= maxY;
}

Alternatively, maybe you actually mean something like this or this ? 另外,也许您实际上是说像这个这个

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

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