public class GeradenDemo {
public static void main(String[] args) {
LineGO line1 = new LineGO(new double[] {2.0000,-2.000,0.0000},new double[] {0.0000,0.9487,2.8460});
LineGO line2 = new LineGO(new double[] {2.0000,2.0000,0.0000},new double[] {-0.6030,-0.6030,1.8091});
System.out.println("Gerade 1: " + line1);
System.out.println("Gerade 2: " + line2);
System.out.println("Winkel zwischen Gerade 1 und 2 im Bogenmaß: " + line1.getAngleRAD(line2));
System.out.println("Winkel zwischen Gerade 1 und 2 im Dezimalgrad: " + line1.getAngleDEG(line2));
System.out.println("Abstand zwischen Geraden 1 und 2 : " + line1.getDistance(line2));
double[] punktA = line1.getNearestPoint(line2);
double[] punktB = line2.getNearestPoint(punktA);
System.out.println("Punkt A = Nähester Punkt auf Gerade 1 zur Geraden 2 ("+punktA[0]+","+punktA[1]+","+punktA[2]+")");
System.out.println("Punkt B = Lotpunkt von Punkt A auf Gerade 2 ("+punktB[0]+","+punktB[1]+","+punktB[2]+")");
double[] punktC = line2.getNearestPoint(line1);
System.out.println("Punkt C = Nähester Punkt von Gerade 2 zur Geraden 1 ("+punktC[0]+","+punktC[1]+","+punktC[2]+")");
}
}
class LineGO{
public double orientation_X;
public double orientation_Y;
public double orientation_Z;
public double supportpoint_X;
public double supportpoint_Y;
public double supportpoint_Z;
LineGO(){
orientation_X=1;
}
LineGO(double[] supportpoint,double[] orientation){
orientation_X=orientation[0];
orientation_Y=orientation[1];
orientation_Z=orientation[2];
supportpoint_X=supportpoint[0];
supportpoint_Y=supportpoint[1];
supportpoint_Z=supportpoint[2];
}
public double getAngleRAD(LineGO line){
double d=Math.sqrt(
(orientation_X*orientation_X
+orientation_Y*orientation_Y
+orientation_Z*orientation_Z) *
(line.orientation_X*line.orientation_X
+line.orientation_Y*line.orientation_Y
+line.orientation_Z*line.orientation_Z) );
double angle = Math.acos(Math.abs(
(orientation_X*line.orientation_X
+orientation_Y*line.orientation_Y
+orientation_Z*line.orientation_Z)/d
));
return angle;
}
public double getAngleDEG(LineGO line){
return(Math.toDegrees(getAngleRAD(line)));
}
public double getDistance(double[] point){
double len = Math.sqrt(orientation_X*orientation_X
+orientation_Y*orientation_Y
+orientation_Z*orientation_Z);
double dist = 0;
if (len > 0) {
double dx = orientation_Z*(point[1]-supportpoint_Y)-orientation_Y*(point[2]-supportpoint_Z);
double dy = orientation_X*(point[2]-supportpoint_Z)-orientation_Z*(point[0]-supportpoint_X);
double dz = orientation_Y*(point[0]-supportpoint_X)-orientation_X*(point[1]-supportpoint_Y);
dist = Math.sqrt(dx*dx+dy*dy+dz*dz)/len;
}
return (dist);
}
public double getDistance(LineGO line){
double[] normal ={
orientation_Y*line.orientation_Z-orientation_Z*line.orientation_Y,
orientation_Z*line.orientation_X-orientation_X*line.orientation_Z,
orientation_X*line.orientation_Y-orientation_Y*line.orientation_X
};
double len = Math.sqrt(normal[0]*normal[0]+normal[1]*normal[1]+normal[2]*normal[2]);
double dist = 0;
if (len > 0) {
dist=Math.abs(
(supportpoint_X-line.supportpoint_X)*normal[0]
+(supportpoint_Y-line.supportpoint_Y)*normal[1]
+(supportpoint_Z-line.supportpoint_Z)*normal[2]
)/len;
}
else{
double[] point ={line.supportpoint_X,line.supportpoint_Y,line.supportpoint_Z};
dist = getDistance(point);
}
return (dist);
}
public double[] getNearestPoint(double[] point){
double pnt[] = {point[0],point[1],point[2]};
double len = Math.sqrt(orientation_X*orientation_X
+orientation_Y*orientation_Y
+orientation_Z*orientation_Z);
if (len > 0) {
double dx = orientation_Z * (point[1]-supportpoint_Y) - orientation_Y * (point[2]-supportpoint_Z);
double dy = orientation_X * (point[2]-supportpoint_Z) - orientation_Z * (point[0]-supportpoint_X);
double dz = orientation_Y * (point[0]-supportpoint_X) - orientation_X * (point[1]-supportpoint_Y);
pnt[0] -= (orientation_Y * dz - orientation_Z * dy) / len / len;
pnt[1] -= (orientation_Z * dx - orientation_X * dz) / len / len;
pnt[2] -= (orientation_X * dy - orientation_Y * dx) / len / len;
}
return (pnt);
}
public double[] getNearestPoint(LineGO line){
double s1 =
orientation_X * line.orientation_X +
orientation_Y * line.orientation_Y +
orientation_Z * line.orientation_Z;
double s2 = line.orientation_X * line.orientation_X
+ line.orientation_Y * line.orientation_Y
+ line.orientation_Z * line.orientation_Z;
double[] vec = {s1 * line.orientation_X - s2 * orientation_X,
s1 * line.orientation_Y - s2 * orientation_Y,
s1 * line.orientation_Z - s2 * orientation_Z
};
double div = orientation_X * vec[0] + orientation_Y * vec[1] + orientation_Z * vec[2];
double k = vec[0] * (supportpoint_X - line.supportpoint_X)
+ vec[1] * (supportpoint_Y - line.supportpoint_Y)
+ vec[2] * (supportpoint_Z - line.supportpoint_Z);
if (div != 0){
k /= div;
}
double[] pnt = {supportpoint_X - k * orientation_X,
supportpoint_Y - k * orientation_Y,
supportpoint_Z - k * orientation_Z};
return (pnt);
}
public String toString(){
String str = "(" + supportpoint_X + "," + supportpoint_Y + "," + supportpoint_Z + ") + k * ("
+ orientation_X + "," + orientation_Y + "," + orientation_Z + ") = (X,Y,Z)";
return (str);
}
}