Search code examples
javatriangulationtriangular

Java calculating points of Angles in a non-right triangle


I am currently working on a project in which i need to draw a non-right triangle in the center of a JFrame using either, java.awt.Graphics's drawLine() or drawPolygon() methods. Both of these methods require the coordinates of all of the points to function. My problem is that instead of points, all i have are all of the angles and side lengths of the triangle. I've drawn up a nifty diagram of what I hope helps you visualize my problem:

enter image description here

(EDIT the position of C in this Senario is not fixed betwen a and b and may be anywhere below the axis on which AB rests)

as you can see from my picture, I need the coordantes of C based off the coordanes of A, is there any way to calculate this given the lengths of all sides and angles of the non-right triangle?

Bonus: How would i find an (x, y) value for A that would effectivly center the triangle in the middle of the JFrame?


Solution

  • If you know angle CAB, the coordinate of point C should be:

    (x+b·sin(θ), y-b·cos(θ))
    

    In Java, there is:

    double Math.sin(double radians);
    double Math.cos(double radians);
    

    Keep in mind that the angle needs to be in radians. If your angles are in degrees, try:

    double Math.sin(Math.toRadians(double degrees));
    double Math.cos(Math.toRadians(double degrees));
    

    Hope this helps.