はABがちょうどそう
ab = A + (B - A) * s
として表現することができることを確認して役立つだろう、ABの方向がB - A
、または(B.x - A.x, B.y - A.y)
です。方向が(A.y - B.y, B.x - A.x)
の線は垂直になります。 (私達はちょうど、xとyを交換し、そのうちの一つを否定。)
我々は、特にABに垂直であり、また、Pを通過するので、我々は
perp = P + (A.y - B.y, B.x - A.x) * t;
perp = (P.x + A.y - B.y, P.y + B.x - A.x) * t;
は今ちょうど交差点を見つけるのですか行をしたいですこの垂線とABとの間にある。 2つの方程式(交点のxとy成分)と2つの未知数(sとt)があります。 sとtを見つけたら、線の式のいずれかにそれらを差し込み交点を求めます。ここで
いくつかの作業コードです:これは、前回の* *数十人を頼まれてい
static Vect2 getIntersection(Vect2 A, Vect2 B, Vect2 P) {
Vect2 abDir = B.minus(A);
Vect2 perpDir = new Vect2(-abDir.y, abDir.x);
Vect2 apDir = P.minus(A);
double s = (perpDir.y * apDir.x - perpDir.x * apDir.y)
/(abDir.x * perpDir.y - abDir.y * perpDir.x);
return A.plus(abDir.scale(s));
}
class Vect2 {
final double x, y;
Vect2(double x, double y) {
this.x = x;
this.y = y;
}
Vect2 scale(double k) {
return new Vect2(x * k, y * k);
}
Vect2 plus(Vect2 that) {
return new Vect2(x + that.x, y + that.y);
}
Vect2 minus(Vect2 that) {
return this.plus(that.scale(-1));
}
}
。 –