3 Replies - 239 Views - Last Post: 22 January 2013 - 11:28 AM Rate Topic: -----

#1 Pin Head  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 228
  • Joined: 31-July 09

Pythagorem theorem

Posted 22 January 2013 - 10:14 AM

hi
having a little problem with pythagorem theorem the problem is is that I've wrote the formula to find the length of the hypotenuse line but when I draw this to the screen the line is longer tha my right triangle help would be appreciated

package trigSystem;

import java.awt.*;
import javax.swing.*;

public class TrigGUI1 extends JFrame
{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public TrigGUI1()
	{
		// TODO Auto-generated constructor stub
		super();
		setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );	
			
		setSize( 500, 500 );
		setVisible( true );
	}
	
	public void rightTriangle( Graphics g )
	{
		g.drawLine( 250, 250, 400, 250 );
		g.drawLine( 400, 250, 400, 100 );
		
		g.setColor( Color.RED );
		g.drawLine( 250, 250, 250 + ( int )pythagoremTheorem(), 250 - ( int )pythagoremTheorem()  );
		
	}		
	public double pythagoremTheorem()
	{
		double adj = 150;
		double opp = 150;
		
		double hyp = opp * opp + adj * adj;
		double calc = Math.sqrt( hyp );
		//System.out.println( "Pythagorem length: " + calc );
		return calc;
	}
	
	public void paint( Graphics g )
	{
                super.paint( g );

		rightTriangle( g );
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) 
	{
		// TODO Auto-generated method stub
		new TrigGUI1();
	}

}




I do have an image but having trouble uploading it but will try

This post has been edited by Pin Head: 22 January 2013 - 10:15 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Pythagorem theorem

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2194
  • View blog
  • Posts: 5,222
  • Joined: 10-September 10

Re: Pythagorem theorem

Posted 22 January 2013 - 10:45 AM

Knowing the hypotenuse would be useful if there was a method that drew a line from a specific point of a certain length in a specific direction. That would look something like:

drawLine( x1, y1, vector );

In your case, you already know x1, y1, and x2, and y2:

x1, y1 = 250, 250
x2, y2 = 400, 100

If you use the x2, y2 in place of the current calculated coordinates for the end of your red line, you'll draw the right line. So, you're not really having a problem with the theorem - your method works fine - but with applying the theorem to a specific problem.
Was This Post Helpful? 0
  • +
  • -

#3 sk8ermeb  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 20
  • View blog
  • Posts: 111
  • Joined: 23-March 10

Re: Pythagorem theorem

Posted 22 January 2013 - 11:13 AM

This is a Math issue. Greg Brannon's solution will solve it with code. But FYI if you use the hypotenuse to do you diff in X and diff in Y you need to multiply by the cos/sin, respectively, of the angle of the hypotenuse.
Was This Post Helpful? 0
  • +
  • -

#4 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2194
  • View blog
  • Posts: 5,222
  • Joined: 10-September 10

Re: Pythagorem theorem

Posted 22 January 2013 - 11:28 AM

View Postsk8ermeb, on 22 January 2013 - 01:13 PM, said:

This is a Math issue. Greg Brannon's solution will solve it with code.

Absolutely. I didn't mean to suggest using my "solution" as the final answer, rather as a demonstration that the hypotenuse used in the code's current equation would not arrive at the correct answer. The beginning point + vector = ending point is the more correct approach.

Sorry if I confused.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1