【问题标题】:Orthogonal projection of a point onto a line in processing?在处理中将点正交投影到一条线上?
【发布时间】:2016-07-14 06:38:15
【问题描述】:

我的处理草图中有一条线段和一个圆。我希望圆的中心点 q 在线段上找到最近的点 p,并且圆将朝它移动。 我不太确定如何编码(在处理中),所以任何建议都会很棒!谢谢! 到目前为止,这是我的代码:

int xPos1 = 200;
int yPos1 = 200;
int xp1 = 50;
int yp1 = 50;
int xp2 = 350;
int yp2 = 50;

void setup() {
    size(400, 400); 
    strokeWeight(2);
    line(xp1, yp1, xp2, yp2);
    strokeWeight(1);
}

void draw() {
    drawCircle();
}

void drawCircle() {
    fill(50, 120, 120);
    //circle
    ellipse(xPos1, yPos1, 75, 75); 
    //circle center
    ellipse(xPos1, yPos1, 7, 7);  
    fill(255);
    text("Q", xPos1 + 15, yPos1 + 5);
    fill(50, 120, 120);
}

【问题讨论】:

    标签: java math vector processing orthogonal


    【解决方案1】:

    点到直线上的投影如下:

    从 x = a + t * n 形式的线和点 p 开始。


    表示直线上离点p最近的点的向量分量是:

    (a - p) - ((a - p) 点 n)n

    所以我们有:p + (a - p) - ((a - p) dot n)n

    经过一些简化,我们有: a - ((a - p) 点 n)n


    注意,((a - p) dot n) n 是向量分量,表示沿直线从最近点到起点的位置(即从最近点到p回到a)

    让我们使用PVectors 让生活更轻松。

    PVector p = new PVector(200, 200);
    PVector a = new PVector(50, 50);
    PVector b = new PVector(350, 50);
    PVector n = new PVector(350, 50); // |p2 - p1|
    
    void setup() {
        size(400, 400); 
        strokeWeight(2);
        strokeWeight(1);
    
        // initialize our normalized (unit length) line direction
        n.sub(a);
        n.normalize();
    }
    
    void draw() {
        drawCircle();
    }
    
    PVector getNearestPointOnLine(PVector p, PVector a, PVector n){
        // the notation turns the computation inside out,
        // but this is equivalent to the above equation
        PVector q = PVector.mult(n, -PVector.sub(a, p).dot(n));
        q.add(a);
        return q;
    }
    
    void drawCircle() {
        // lets draw everything here where we can see it
        background(255, 255, 255);
        line(a.x, a.y, b.x, b.y);
    
        fill(50, 120, 120);
        //circle
    
        // NOTE: this may require hooking up a mouse move event handler
        p.x = mouseX;
        p.y = mouseY;
        PVector q = getNearestPointOnLine(p, a, n);
    
        ellipse(q.x, q.y, 75, 75); 
        //circle center
        ellipse(q.x, q.y, 7, 7);  
        fill(0); // make text visible on white background
        text("Q", q.x + 15, q.y + 5);
        //fill(50, 120, 120);
    }
    

    参考:https://en.wikipedia.org/wiki/Distance_from_a_point_to_a_line#Vector_formulation

    【讨论】:

    • 请注意,点 q 仍然会出现在线段的任一端之外,即这里没有应用“边界”检查。
    • 谢谢!这很有帮助。
    • 我也在寻找的东西是圆朝着线段上最近的点移动。就像我们看到圆圈移动一样。任何关于如何做到这一点的想法都会很棒!
    • 您可以使用相同的向量公式,其中 t 是 0-1 的值,n 是从球到最近点的方向上的归一化向量(即 norm(q - p)使用我们已经拥有的变量)。维基百科文章将每个向量分量描述为一个通过公式工作的分量。 :) 例如,当 t 为 0 时,上述结果导致球位于点 p,当 t 为 1 时,球将位于点 q。
    • 如果您需要帮助确定在这种情况下如何为球设置动画,请再问一个问题。如果您没有得到快速回复,请在此处发布指向它的链接,我会在下班后查看它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-31
    • 2018-06-14
    • 1970-01-01
    • 2011-01-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多