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.
Page Index Toggle Pages: 1
draw an arrow (Read 1772 times)
draw an arrow
Aug 24th, 2008, 9:57pm
 
Hi,

I'm trying to create a function that draws an arrow (a line with an arrow head at the end) from point x1, y1 to x2, y2 but I don't have enough knowledge about the math to do it. Can someone help me out?

Thanks!
Jori
Re: draw an arrow
Reply #1 - Aug 24th, 2008, 11:02pm
 
maybe something like this :

Code:
// draw the line
line(x1, y1, x2, y2);

// draw a triangle at (x2, y2)
pushMatrix();
rotate(atan2(y2-y1, x2-x1));
translate(x2, y2);
triangle(0, 0, -10, 5, -10, -5);
popMatrix();


hope it will work (I haven't tried).
Re: draw an arrow
Reply #2 - Aug 25th, 2008, 11:48am
 
Thanks a lot! That helped me out.
Here is the arrow function:

Code:
void arrow(int x1, int y1, int x2, int y2) {
line(x1, y1, x2, y2);
pushMatrix();
translate(x2, y2);
float a = atan2(x1-x2, y2-y1);
rotate(a);
line(0, 0, -10, -10);
line(0, 0, 10, -10);
popMatrix();
}
Page Index Toggle Pages: 1