Search code examples
javageometry

Code for inverse sin, cos and tan


I'm writing a program which calculates the length of sides, angle measures at each vertex (in degrees) and the area of the triangle. The triangle is defined by the x and y-coordinates of its three vertices which are the user input.

I already finished methods calculating the length of sides and currently I'm trying to use law of cosines to find one of the angles. I need help in this area:

Does anyone know code calculating inverse sin, cos and tan with the answer in degrees?

This is more mathematical question but if somebody could help I would appreciate it. I also don't know how to find the height of the triangle knowing its length of sides. It would be needed to find the area in the next method I will be writing.

test:

public double getAngleA() {
    double angleA = Math.acos((side2 * side2 + side3 * side3 - side1 * side1)/(2.0 * side2 * side3));
    double angleA = Math.toDegrees(angleA);  
    return angleA;
}

Object Class:

public class Triangle
{
 private double x1;
 private double y1;
 private double x2;
 private double y2;
 private double x3;
 private double y3;
 private double sideA;
 private double sideB;
 private double sideC;
 private double angleA;
 private double angleB;
 private double angleC;



  public Triangle(double x1, double y1, double x2, double y2, double x3, double y3)
  {
 this.x1 = x1;
 this.y1 = y1;
 this.x2 = x2;
 this.y2 = y2;
 this.x3 = x3;
 this.y1 = y3;
  }
   public double getSideC()
  {
  sideC = Math.sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
  return sideC;
   }
  public double getSideA()
  {
  sideA = Math.sqrt((x2-x3)*(x2-x3) + (y2-y3)*(y2-y3));
    return sideA; 
  }
   public double getSideB()
  {
  sideB = Math.sqrt((x1-x3)*(x1-x3) + (y1-y3)*(y1-y3));
  return sideB ;
  }
  public double getAngleA() {

  double angleA = Math.acos((sideB * sideB + sideC * sideC - sideA * sideA)/(2.0 * sideB * sideC));
  angleA = Math.toDegrees(angleA);  
  return angleA;
   }
    public double getAngleB() {

  double angleB = Math.asin((Math.sin(angleA) * sideB)/sideA); 
  return angleB;
   }
     public double getAngleC() {

  double angleC = 180 - (angleA+angleB);
  return angleC;
   }


}

Tester:

public class TriangleTester {


  public static void main(String[] args) { 
    Triangle t = new Triangle (1, 2, 9, 7, 7, 10);
    System.out.println(t.getSideC());
  }


}

Expected Output: about 9.43398 Actual Output: 8.54400374531753

How do I fix it?


Solution

  • The java.lang.Math utility class has the arc sin method, asin(...), that returns the radian result which can then be converted via Math's toDegrees(...) method to degrees.