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 › Line(x,y,angle,length)
Page Index Toggle Pages: 1
Line(x,y,angle,length) (Read 921 times)
Line(x,y,angle,length)
Dec 18th, 2009, 9:38am
 
Hello

I'm trying to build simple "radar" application. With help of Arduino my ultrasonic ranging sensor mounted on servo is returing  servo angle and cm as ints. Angle is from 0 to 180 and cm are from 4 to 300.

I'm seeking for formula able to draw line from one common point to specific length with exact angle. Something like line(x,y,angle, length)

Will be much obliged for any help

P.S. After trying to refresh my school trigenometry studies with help of Google I'm completelly lost.
Re: Line(x,y,angle,length)
Reply #1 - Dec 18th, 2009, 10:10am
 
Angle in radians.

Code:

void setup()
{
 size(200, 200);  
}

void draw()
{
 background(0);
 stroke(255);
 lineAngle(100, 100, (millis()/1000.0), 50);
}

void lineAngle(int x, int y, float angle, float length)
{
 line(x, y, x+cos(angle)*length, y-sin(angle)*length);
}

Re: Line(x,y,angle,length)
Reply #2 - Dec 18th, 2009, 10:21am
 
You can use this formula to calculate the second point of your line

x = cos( a );
y = sin( a );


so this would become :

Code:
 
void setup() {
 size(400,400);
 smooth();
 background(0);

}

float speed = 0.015;
float a;

void draw() {
 fill(0,10);
 noStroke();


rect(0,0,width,height);
 
float px = width/2 + cos( a  )*120;
float py = height/2 + sin( a  )*120;

stroke(0,255,0,120);
strokeWeight(3);
line(width/2,height/2,px,py);


a+=speed;

}



edit: im to slow Smiley
Re: Line(x,y,angle,length)
Reply #3 - Dec 18th, 2009, 11:06am
 
Yes! And radians = deg * (3,14/180) !
Thank you, code is working excelent
Re: Line(x,y,angle,length)
Reply #4 - Dec 18th, 2009, 11:13am
 
there are degrees() and radians() methods to do the conversion for you... (but i can never remember whether they convert TO degrees or FROM degrees)
Page Index Toggle Pages: 1