1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
|
/**
* Distance calculation between two points.
* @param p1
* @param p2
* @return the distance between p1 and p2.
*/
public static double distance(final Point p1, final Point p2) {
Point p1p2 = remove(p1,p2); //p1-p2;
return Math.sqrt(Math.pow(p1p2.x(), 2) + Math.pow(p1p2.y(), 2) +
Math.pow(p1p2.z(), 2));
}
public static double length(final Line l) {
return l.a().distance(l.b());
}
public static Point projectionOrthoPointOnLine(final Point p, final Line l) {
//Q = P1 + u*(P2-P1);
//(P3-Q)dot(P2-P1) = 0;
//(P3-P1-u*(P2-P1))dot(P2-P1) = 0;
//u = ((x3-x1)*(x2-x1) + (y3-y1)*(y2-y1) + (z3-z1)*(z2-z1))/length(l);
//Qx = P1x + u*(P2x-P1x);
//Qy = P1y + u*(P2y-P1y);
//Qz = P1z + u*(P2z-P1z);
double lengthLine = l.length();
Vector ap = new Vector(Point.remove(p, l.a()));
Vector ab = new Vector(Point.remove(l.b(), l.a()));
double u = ap.dot(ab)/(lengthLine*lengthLine);
float qx = (float) (l.a().x() + u*(ab.x()));
float qy = (float) (l.a().y() + u*(ab.y()));
float qz = (float) (l.a().z() + u*(ab.z()));
return new Point(qx, qy, qz);
} |
Partager