Frightlin
Frightlin

Reputation: 161

How do I implement these angles in my ThreePoint triangle program in java?

I'm trying to implement

enter image description here

in my ThreePoint program in java.

Here is my getLength method to calculate the sides

   private double getLength(int side){


     if(side == 0 && isTriangle()){
         return Math.sqrt((x2-x1) * (x2-x1) + (y2-y1) * (y2-y1));
     } else if (side == 1 && isTriangle()){
         return Math.sqrt((x2-x0) * (x2-x0) + (y2-y0) * (y1-y0));
     } else if (side == 2 && isTriangle()){
         return Math.sqrt((x1-x0) * (x1-x0) + (y1-y0) * (y1-y0)); 
     }else{ return 0;

     }
     }

The parameter vertex should be 0, 1, or 2 and is used to specify angle a0, a1, or a2. The method returns the angle of the specified vertex. If the three points do not form a triangle, this method should return zero. To determine the angles of the triangle you can use the law of cosines.(as above)

so below is what the skeleton would look like, how do I implement the diagram though?

 public double getAngle(int vertex){

            if(vertex == 0 && isTriangle()) {
           return a0 here; }
           else if(vertex == 1 && isTriangle()) {
           return a1 here; }
           else if(vertex == 2 && isTriangle()) {
           return a2; }

Upvotes: 0

Views: 162

Answers (1)

alanmanderson
alanmanderson

Reputation: 8230

My java is a little rusty, and my trig is even rustier, but I think what you want for a0 is below. You can figure out a1 and a2 from below. Also note that Java trigonometry uses Radians not degrees, but that shouldn't be a problem here.

double s02 = Math.pow(this.getLength(0),2);
double s12 = Math.pow(this.getLength(1),2);
double s22 = Math.pow(this.getLength(2),2);
a0 = Math.acos((-s02+s12+s22)/(2*s12*s22));

Note: I assume s0 s1 and s2 are the lengths of side 0,1, and 2 correct?

Upvotes: 1

Related Questions