noob question about pythagorean theoram
in
Programming Questions
•
3 years ago
hi there
i'm totally a noob here and have a question about how to find the middle point of a line (in terms of the points x and y co-ordinates).
simply in the program i've written the user draws a line and i can tell the user how long the line is using the pythagorean theoram. i'd like the program to print the length of the line (that the user has 'drawn') in the middle of that exact line. i've got a working example of what i want it to look like, problem is that although this does work, the code is difficult to track and read particularly because of all the nested if statements. is there a simple mathematical formula i can use to determine the x and y corordinates of the middle of a line given that i already know what the length of the line is?
here is the overcoded version
i'm totally a noob here and have a question about how to find the middle point of a line (in terms of the points x and y co-ordinates).
simply in the program i've written the user draws a line and i can tell the user how long the line is using the pythagorean theoram. i'd like the program to print the length of the line (that the user has 'drawn') in the middle of that exact line. i've got a working example of what i want it to look like, problem is that although this does work, the code is difficult to track and read particularly because of all the nested if statements. is there a simple mathematical formula i can use to determine the x and y corordinates of the middle of a line given that i already know what the length of the line is?
here is the overcoded version
- /*Pythagorean Theory
*28 June 2010
*This script was made to demonstrate the theory of pythagoras, which states that
*the square of the hypotenuese of a right angle triangle is equal to the sum of the
*squares of the remaining sides
*/
//Declare and data type variables
int mXFirst;
int mYFirst;
int mX;
int mY;
//since xSide and ySide are being used in the pythag variable i've declared them as
//floats because sqrt() returns a float.
float xSide;
float ySide;
float pythag;
void setup(){
//any size will do
size(500,500);
}
void draw(){
//any color
background(127);
//shortening commands with new variable names
mX = mouseX;
mY = mouseY;
//although x- and ySide do not have to be fed into pythag as absolutes because they
//will be squared and invariably be returned as absolutes, i've converted them to
//abs() because they need to be positive for the placement of the text later
xSide = float(abs(mX - mXFirst));
ySide = float(abs(mY - mYFirst));
//pythagoras magic
pythag = sqrt(xSide*xSide + ySide*ySide);
//placement of text indicating the length of the hypotenuse
//this is simply to place the text half way along the hypotenuse
if(mousePressed == true){
line(mXFirst, mYFirst, mouseX, mouseY);
if(mX >= mXFirst && mYFirst <= mY ){
text(pythag, (xSide/2)+mXFirst, (ySide/2)+mYFirst);
}else if (mX <= mXFirst && mYFirst <= mY){
text(pythag, (xSide/2)+mX, (ySide/2)+mYFirst);
}else if (mX >= mXFirst && mYFirst >= mY){
text(pythag, (xSide/2)+mXFirst, (ySide/2)+mY);
}else if (mX <= mXFirst && mYFirst >= mY){
text(pythag, (xSide/2)+mX, (ySide/2)+mY);
}
}
}
//mouseClicked() does not work here instead mousePressed() must be used
void mousePressed(){
mXFirst = mX;
mYFirst = mY;
}
- /*Pythagorean Theory
*28 June 2010
*This script was made to demonstrate the theory of pythagoras, which states that
*the square of the hypotenuese of a right angle triangle is equal to the sum of the
*squares of the remaining sides
*/
//Declare and data type variables
int mXFirst;
int mYFirst;
int mX;
int mY;
//since xSide and ySide are being used in the pythag variable i've declared them as
//floats because sqrt() returns a float.
float xSide;
float ySide;
float pythag;
//new
float halfXSide;
float halfYSide;
float theta;
float textPosX;
float textPosY;
void setup(){
//any size will do
size(500,500);
}
void draw(){
//any color
background(127);
//shortening commands with new variable names
mX = mouseX;
mY = mouseY;
//although x- and ySide do not have to be fed into pythag as absolutes because they
//will be squared and invariably be returned as absolutes, i've converted them to
//abs() because they need to be positive for the placement of the text later
xSide = float(abs(mX - mXFirst));
ySide = float(abs(mY - mYFirst));
//pythagoras magic
pythag = sqrt(xSide*xSide + ySide*ySide);
//placement of text indicating the length of the hypotenuse
if(mousePressed == true){
line(mXFirst, mYFirst, mouseX, mouseY);
halfXSide = xSide/2;
halfYSide = ySide/2;
theta = atan2(halfYSide, halfXSide);
textPosX = pythag/2 * cos(theta);
textPosY = pythag/2 * sin(theta);
text(pythag, abs(textPosX), abs(textPosY));
}
}
//mouseClicked() does not work here instead mousePressed() must be used
void mousePressed(){
mXFirst = mX;
mYFirst = mY;
}
1