【问题标题】:How can i check if a Vector3 (x,y,z) is between 2 other Vector3's in Java?如何检查 Vector3 (x,y,z) 是否在 Java 中的其他 2 个 Vector3 之间?
【发布时间】:2019-12-21 09:41:48
【问题描述】:

我想为 Minecraft 服务器软件制作一个保护插件,并且我想使用 Vector3。我想检查一个 Vector3 是否在 2 个位置 (Vector3) 之间。

Vector3 有以下值:x、y 和 z。 我现在如何检查一个向量是否在其他 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.
}

我期待结果是否在这两个位置。

【问题讨论】:

  • 在 3D 空间中“介于”是什么意思?共面并在较小的角度内?

标签: java position xyz


【解决方案1】:
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;
}

只需检查所有 x、y 和 z 边界,以确定向量是否位于其中。作为记录,在您的示例中,给定的向量将不在范围内,因为它的 z 值超出范围(在 [43,100] 之外)。在这种情况下(不关心 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;
}

或者,也许您实际上是指thisthis 之类的东西?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多