Three-Point Triangle

Make a 3-point triangle class

Page 1 of 1

3 Replies - 6731 Views - Last Post: 29 June 2009 - 06:52 AM Rate Topic: -----

#1 aszecsei   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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   User is offline

  • GAME OVER. NERD WINS.
  • member icon

Reputation: 124
  • View blog
  • 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. :D
Was This Post Helpful? 1
  • +
  • -

#3 aszecsei   User is offline

  • New D.I.C Head

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

Re: Three-Point Triangle

Posted 29 June 2009 - 06:47 AM

View Postcrazyjugglerdrummer, 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. :D


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).
Was This Post Helpful? 0
  • +
  • -

#4 aszecsei   User is offline

  • New D.I.C Head

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

Re: Three-Point Triangle

Posted 29 June 2009 - 06:52 AM

View Postaszecsei, on 29 Jun, 2009 - 05:47 AM, said:

View Postcrazyjugglerdrummer, 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. :D


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.

Thank you for your help,

Alic
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1