We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › direction of a line; mid point; perpindicular?
Page Index Toggle Pages: 1
direction of a line; mid point; perpindicular??? (Read 354 times)
direction of a line; mid point; perpindicular???
Aug 10th, 2006, 3:22am
 
sorry, im completely lost on this one and was wondering if someone here wouldn't mind helping me out w/ a few pointers...

basically, if im given a line, i'd like to know how to calculate what the mid point on the line is, and which direction its pointing so that i can create another line that intersects it perpendicularly.

Quote:

void setup(){
size (200,200);
line (40,100,160,100);//this one is easy to find the middle point,and create a line that intersects it at the middle perpendicularly
stroke (255,0,0);
line (100,75,100,125);
stroke (255,0,255);
line (14,65,80,10);//but this one? no idea how to find mid point, or to make a line intersect it perpendicularly.
}

void draw(){
}


i know it would be able to draw a cross, then transform it by rotating, but thats not what i need for this application.
any help anyone could give would be much appreciated

cheers!

jc
Re: direction of a line; mid point; perpindicular?
Reply #1 - Aug 10th, 2006, 11:30am
 
Here's some code that should hopefully help:

Code:
float startX,startY,endX,endY;
startX=10;
startY=10;
endX=190;
endY=190;

midX=startX+((endX-startX)/2.0);
midY=startY+((endY-startY)/2.0);

float dir=atan2(endY-startY,endX-startX);
float rotatedDir=dir+PI/2.0;

float lineLength=sqrt(sq(endX-startX)+sq(endY-startY));

float newStartX=midX+( (lineLength/2.0) * cos(rotatedDir) );
float newStartY=midY+( (lineLength/2.0) * sin(rotatedDir) );
float newEndX=midX-( (lineLength/2.0) * cos(rotatedDir) );
float newEndY=midY-( (lineLength/2.0) * sin(rotatedDir) );


The above was all just typed and not tested, so may have a few small errors, but I think should work and illustrates the actions needed to be performed to do what you want.
Re: direction of a line; mid point; perpindicular?
Reply #2 - Aug 10th, 2006, 7:33pm
 
brilliant dude thats exactly what i was looking for.
thank you very much!

-jc
Page Index Toggle Pages: 1