Calculating the angle between two lines without having to calculate the slope? (Java)

后端 未结 8 1232
夕颜
夕颜 2020-11-28 07:27

I have two Lines: L1 and L2. I want to calculate the angle between the two lines. L1 has points: {(x1, y1), (x2, y2)} and L2 has points: {(x3, y3), (x4, y

8条回答
  •  鱼传尺愫
    2020-11-28 07:35

    Dot product is probably more useful in this case. Here you can find a geometry package for Java which provides some useful helpers. Below is their calculation for determining the angle between two 3-d points. Hopefully it will get you started:

    public static double computeAngle (double[] p0, double[] p1, double[] p2)
    {
      double[] v0 = Geometry.createVector (p0, p1);
      double[] v1 = Geometry.createVector (p0, p2);
    
      double dotProduct = Geometry.computeDotProduct (v0, v1);
    
      double length1 = Geometry.length (v0);
      double length2 = Geometry.length (v1);
    
      double denominator = length1 * length2;
    
      double product = denominator != 0.0 ? dotProduct / denominator : 0.0;
    
      double angle = Math.acos (product);
    
      return angle;
    }
    

    Good luck!

提交回复
热议问题