Search code examples
javaslick2d

Java : 2 lines angle finding?


I'm using Java and slick2D.

The Class:Line is referenced from : org.newdawn.slick.geom.Line

My question is how can I find out how many degrees is the angle that concluded two lines.

Example:

Line ln1 = new Line(0,1,0,3);
Line ln2 = new Line(1,0,3,0);

The angles that ln1 and ln2 are concluding are : 90,90 degrees - the two lines are perpendicular.

I hope you could help me :D


Solution

  • This code calculates the angle between the two lines in radians:

    double angle1 = Math.atan2(ln1.getY1() - ln1.getY2(), ln1.getX1() - ln1.getX2());
    double angle2 = Math.atan2(ln2.getY1() - ln2.getY2(), ln2.getX1() - ln2.getX2());
    double desiredAngle = angle1 - angle2