【问题标题】:Lines Intersect method not working correctly线相交方法无法正常工作
【发布时间】:2014-01-27 13:32:45
【问题描述】:

我正在研究一些向量数学,我需要计算多边形的法线向量。 我的代码:

    //p is a parameter, is a Vec2, second point on first line
    double[][] vert = getVerticies(); //[any length, # of verticies][2]
    for(int i = 0; i < vert.length; i++) {
        Vec2 cm = Vec2.ZERO_VEC;//first point on first line, always is <0, 0> as it is the origin
        Vec2 rcm = getCM(); // just used to get relative positions.
        Vec2 v1 = cm.sub(new Vec2(vert[i])); //the first point in one of all edges of the shape, second line
        Vec2 v2 = cm.sub(new Vec2(i == vert.length - 1 ? vert[0] : vert[i + 1])); // the second point on the second  line.
        double den = (v2.getY() - v1.getY()) * (p.getX() - cm.getX()) - (v2.getX() - v1.getX()) * (p.getY() - cm.getY());
        if(den == 0D) {
            continue;
        }
        double a = ((v2.getX() - v1.getX()) * (cm.getY() - v1.getY()) - (v2.getY() - v1.getY()) * (cm.getX() - v1.getX())) / den;
        double b = ((p.getX() - cm.getX()) * (cm.getY() - v1.getY()) - (p.getY() - cm.getY()) * (cm.getX() - v1.getX())) / den;
        if(a >= 0D && a <= 1D && b >= 0D && b <= 1D) {
            Vec2 mid = v2.add(v2.sub(v1).scale(0.5D)); //this is just normal vector calculation stuff, I know the error isn't here, as if it was, it would return a non-unit-scale vector.
            return mid.uscale(); //hats the vector, returns
        }
    }
    return p; // return the parameter, second point on first line, used as a contingency, should never actually run, as the first line is fully contained in the lines were testing against

我已经进行了一些调试,但我只是不知道发生了什么。谁能告诉我我的数学有什么问题?它似乎流动得很好,但数学似乎不正确。我使用此代码的目标是确定我的线相交的两个顶点的索引。

【问题讨论】:

  • 输入?预期结果?实际结果?这将大大受益于被分解成更小的部分(方法)。 (顺便说一句,我很确定这里没有微积分,只有向量代数。)
  • 我想我明白了我的问题,你知道当你试图解释一个问题时你是如何解决的吗?那是我在测试线段,当我需要测试矢量“光线”和/或使用光线追踪时。
  • 等等。它是二维平面中的一个表面。法线不总是沿着 z 轴吗?
  • @jpmc26 你对扭矩的想法,在二维的情况下,它通常只表示为一个由扭矩大小组成的+/-值。法线向量在物理学中用于碰撞响应。
  • A normal vector 是垂直于对象的向量。由于您的对象是位于 xy 平面中的多边形(二维),因此垂直向量必须指向远离 xy 平面的方向。不?也许如果你不是取多边形的法线而是直线,那么它可能是 2d,但我相信表面的法线必须是 3d。所以我遗漏了一些东西,你没有计算多边形的法线,或者法线会沿着 z 轴。 (此外,扭矩大致相当于力的旋转等效值,我没有看到它被引用。)

标签: java math physics algebra calculus


【解决方案1】:

哎呀,通过试图解释它想通了。我需要进行光线追踪,或者使用光线相交测试。

【讨论】:

    【解决方案2】:

    如果您尝试计算多边形的法线,看起来您正在尝试做一些过于复杂的事情。您可以简单地使用两个相邻边的叉积来获得它。更好的是,通过简单的数学运算explained here,您甚至不需要计算边缘向量。只需将每个相邻顶点对的叉积相加即可:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-02
      • 2021-12-29
      相关资源
      最近更新 更多