# Three-Point Triangle

Page 1 of 1

## 3 Replies - 6731 Views - Last Post: 29 June 2009 - 06:52 AMRate 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=111980&amp;s=cab55b57518f8942a043773927899258&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 aszecsei

Reputation: 0
• Posts: 9
• Joined: 21-October 08

# Three-Point Triangle

Posted 26 June 2009 - 12:12 PM

I'm making a class to define a triangle based on three points. I need to test to see if a triangle is acute, obtuse, right, and other things. I have made several methods, but for some reason am messing up when it comes to the angles. Here is my code for that test:

```	   public boolean isAcute()
{
return ((this.getAngle(0)<Math.PI/2 && this.getAngle(1)<Math.PI/2 && this.getAngle(2)<Math.PI/2) && this.isTriangle());
}

public boolean isObtuse()
{
return ((this.getAngle(0)>Math.PI/2 || this.getAngle(1)>Math.PI/2 || this.getAngle(2)<Math.PI/2) && this.isTriangle());
}

public boolean isRight()
{
return ((this.approxEqual(this.getAngle(0),Math.PI/2) || this.approxEqual(this.getAngle(1),Math.PI/2) || this.approxEqual(this.getAngle(2),Math.PI/2)) && this.isTriangle());
}
```

The approxEqual method:

```	private boolean approxEqual(double x, double y)
{
double epsilon = 1E-12;
return Math.abs(x - y) <= epsilon;
}
```

And the getAngle method:

```	public double getAngle(int vertex)
{
if (this.isTriangle()){
if(vertex == 0) {
double angle = Math.acos((-Math.pow(this.getLength(0),2)+Math.pow(this.getLength(1),2)+Math.pow(this.getLength(2),2))/(2*this.getLength(1)*this.getLength(2)));
return angle;
} else if(vertex == 1) {
double angle = Math.acos((Math.pow(this.getLength(0),2)-Math.pow(this.getLength(1),2)+Math.pow(this.getLength(2),2))/(2*this.getLength(0)*this.getLength(2)));
return angle;
} else {
double angle = Math.acos((Math.pow(this.getLength(0),2)+Math.pow(this.getLength(1),2)-Math.pow(this.getLength(2),2))/(2*this.getLength(0)*this.getLength(1)));
return angle;
}
} else {
return 0;
}
}
```

Any help is appreciated. Thank you very much,

Alic

This post has been edited by aszecsei: 26 June 2009 - 12:14 PM

Is This A Good Question/Topic? 0

## Replies To: Three-Point Triangle

### #2 crazyjugglerdrummer

• GAME OVER. NERD WINS.

Reputation: 124
• Posts: 690
• Joined: 07-January 09

## Re: Three-Point Triangle

Posted 26 June 2009 - 12:32 PM

The problem is in your test methods. All triangles have at least 1 angle less than 90 degrees, so you need a different acute test. Go through all of the angles, and if one of them is > 90, the triangle's obtues, and if one is equal to 90, then its right. Otherwise, its acute.

### #3 aszecsei

Reputation: 0
• Posts: 9
• Joined: 21-October 08

## Re: Three-Point Triangle

Posted 29 June 2009 - 06:47 AM

crazyjugglerdrummer, on 26 Jun, 2009 - 11:32 AM, said:

The problem is in your test methods. All triangles have at least 1 angle less than 90 degrees, so you need a different acute test. Go through all of the angles, and if one of them is > 90, the triangle's obtues, and if one is equal to 90, then its right. Otherwise, its acute.

Thank you. However, the problem is in my Obtuse versus Right test methods. When I have a right angle, it can be a little off (hence the approxEqual method). Therefore, I can have a triangle supposedly be both Right and Obtuse (because it is actually greater than that, but it's not supposed to be).

### #4 aszecsei

Reputation: 0
• Posts: 9
• Joined: 21-October 08

## Re: Three-Point Triangle

Posted 29 June 2009 - 06:52 AM

aszecsei, on 29 Jun, 2009 - 05:47 AM, said:

crazyjugglerdrummer, on 26 Jun, 2009 - 11:32 AM, said:

The problem is in your test methods. All triangles have at least 1 angle less than 90 degrees, so you need a different acute test. Go through all of the angles, and if one of them is > 90, the triangle's obtues, and if one is equal to 90, then its right. Otherwise, its acute.

Thank you. However, the problem is in my Obtuse versus Right test methods. When I have a right angle, it can be a little off (hence the approxEqual method). Therefore, I can have a triangle supposedly be both Right and Obtuse (because it is actually greater than that, but it's not supposed to be).

I figured it out. In my isObtuse() method, I just added another requirement: it cannot also be a right triangle.