点の線への射影は次のとおりです。
x = a + t * n の形式の線と点 p から始めます。
点 p からの直線上の最も近い点を表すベクトル コンポーネントは次のとおりです。
(a - p) - ((a - p) ドット n)n
p + (a - p) - ((a - p) dot n)n
単純化すると、次のようになります。 a - ((a - p) dot n)n
((a - p) dot n) n は、最も近いポイントから開始点までの線に沿った位置を表すベクトル コンポーネントです (つまり、最も近いポイントから p から a に戻る)。
PVector
s を使って生活を少し楽にしましょう。
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