# Triangle class

Page 1 of 1

## 5 Replies - 6725 Views - Last Post: 05 February 2010 - 08:10 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=154162&amp;s=fc473bf7e124426c88eae3e581bb5423&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

Reputation: 1
• Posts: 21
• Joined: 01-November 09

# Triangle class

Posted 05 February 2010 - 03:25 PM

The triangle constructor creates a triangle object with two sides and their included angle (a Side Angle Side triangle). There are then methods to calculate the third side, determine if the triangle is equilateral, determine if the triangle is isosceles, and to determine if the triangle is valid. I have all of this done. What I'm having problems with is determining if the triangle is a right triangle (Note: the 90 degree angle of the right triangle may not be the same angle that is passed to the constructor).

Here's what I have so far. I'm including the getThirdSide() method just for completeness, it is working fine though.

```public double getThirdSide() {
if (!(isValid()))
return 0;
double radianAngle = angle * Math.PI / 180;
double lawOfCos = 0;
lawOfCos = (side1 * side1) + (side2 * side2) - 2 * side1 * side2
* cosAngle;
double thirdSide = Math.sqrt(lawOfCos);
return thirdSide;
}

public boolean isRight() {
double side3 = this.getThirdSide();

// This converts the angle from degrees to radians
double radianAngle = angle * Math.PI / 180;

// This uses the law of sines to compute the second angle
double sinB = (Math.sin(radianAngle) * side1) / side3;

// 1/sin(sinB) to equal csc(sinB) which is the inverse of sin
double angleB = 1 / Math.sin(sinB);

// Converts the second angle back to degrees
angleB = angleB * 180 / Math.PI;

double angleC = 180 - angle - angleB;

// declared at beginning of program, final double TOLERANCE = 0.01
if (Math.abs(angle - 90) < TOLERANCE)
return true;
if (Math.abs(angleB - 90) < TOLERANCE)
return true;
if (Math.abs(angleC - 90) < TOLERANCE)
return true;
else
return false;
}

```

Like I said, the getThirdSide() method works great, but I can't get isRight() to return true for a known right triangle such as the triangle with side1 = 1, side2 = 2, and angle = 60. The third side is the square root of 3, and the two other angles are 30 and 90. I changed some code around to get isRight() to return a String so I could see what the angle values were and did not come up with 30 60 90, so I know there is a problem with the math. Any help would be greatly appreciated.

Is This A Good Question/Topic? 0

## Replies To: Triangle class

### #2 333OnlyHalfEvil

Reputation: 24
• Posts: 674
• Joined: 20-March 09

## Re: Triangle class

Posted 05 February 2010 - 04:38 PM

You mean angle = 90...

Also, you should really post all of your code.

This post has been edited by 333OnlyHalfEvil: 05 February 2010 - 04:42 PM

Reputation: 1
• Posts: 21
• Joined: 01-November 09

## Re: Triangle class

Posted 05 February 2010 - 06:07 PM

I'm not at home right now so I don't have access to the rest of the code, but I will post it in a few hours.

I did in fact mean 60 for the angle, not 90. The full triangle has legs of length 1 and the square root of 3, and a hypotenuse of 2. The included angle between sides length 1 and 2 is 60, between 2 and sqrt 3 is 30, and between sqrt 3 and 1 is 90. The idea of the isRight() method is to determine if the triangle is a right triangle regardless of if the given angle is 90 degrees or not. The class DOES work if the given angle is 90 degrees.

As I said, I'll post my full code ASAP. Any insight based on this code would be great, if that's possible.

Reputation: 1
• Posts: 21
• Joined: 01-November 09

## Re: Triangle class

Posted 05 February 2010 - 07:19 PM

Here's the full code:

```public class triangle {
private double side1;
private double angle;
private double side2;
private final double TOLERANCE = 0.1;

public triangle(double side1, double angle, double side2) {
this.side1 = side1;
this.angle = angle;
this.side2 = side2;
}

public double getSide1() {
return side1;
}

public double getAngle() {
return angle;
}

public double getSide2() {
return side2;
}

public String toString() {
return "side1 = " + side1 + " angle = " + angle + " side2 = " + side2;
}

public boolean isValid() {
if (side1 != 0 && angle > 0 && angle < 180 && side2 != 0)
return true;
else
return false;
}

public double getThirdSide() {
if (!(isValid()))
return 0;
double radianAngle = angle * Math.PI / 180;
double lawOfCos = 0;
lawOfCos = (side1 * side1) + (side2 * side2) - 2 * side1 * side2
* cosAngle;
double thirdSide = Math.sqrt(lawOfCos);
return thirdSide;
}

public double getArea() {
if (!(isValid()))
return 0;
double radianAngle = angle * Math.PI / 180;
double area = 0;
area = 0.5 * side1 * side2 * sinAngle;
return area;
}

public boolean isEquilateral() {
double side3 = this.getThirdSide();
double oneTwo = Math.abs(side1 - side2);
double oneThree = Math.abs(side1 - side3);
double twoThree = Math.abs(side2 - side3);
double total = oneTwo + oneThree + twoThree;
if (total < TOLERANCE)
return true;
else
return false;
}

public boolean isIsosolece() {
double side3 = this.getThirdSide();
double diff12 = Math.abs(side1 - side2);
double diff13 = Math.abs(side1 - side3);
double diff23 = Math.abs(side2 - side3);
if (diff12 < TOLERANCE || diff13 < TOLERANCE || diff23 < TOLERANCE)
return true;
else
return false;

}

public boolean isRight() {
double side3 = this.getThirdSide();
double radianAngle = angle * Math.PI / 180;

double sinB = (Math.sin(radianAngle) * side2) / side3;
double angleB = 1 / Math.sin(sinB);
angleB = angleB * 180 / Math.PI;
double angleC = 180 - angle - angleB;
if(Math.abs(angle - 90) < TOLERANCE)
return true;
if (Math.abs(angleB - 90) < TOLERANCE)
return true;
if (Math.abs(angleC - 90) < TOLERANCE)
return true;
else
return false;
}

}

```

Edit: Closed the code tag. Sorry!

This post has been edited by riadfodig: 05 February 2010 - 08:09 PM

### #5 erik.price

• D.I.C Lover

Reputation: 486
• Posts: 2,690
• Joined: 18-December 08

## Re: Triangle class

Posted 05 February 2010 - 07:24 PM

Don't forget to close your code tags

Reputation: 1
• Posts: 21
• Joined: 01-November 09

## Re: Triangle class

Posted 05 February 2010 - 08:10 PM

I figured out my problem, and also the solution. I was trying to find the arcsin of the sinB variable by using 1 / Math.sin(sinB), whereas I should have just used Math.asin(sinB).

So much heart ache for such a simple answer. I'm glad to have this worked out though