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 of a specific length
Page Index Toggle Pages: 1
line of a specific length? (Read 959 times)
line of a specific length?
Apr 16th, 2010, 11:01am
 
Hi all,

I was wondering if anyone knew of an easy way to draw a random line of a specific length?  Basically what I'm trying to do is draw "trees" where every branch is the same length but goes in a random direction.  My math skills are limited so I apologize if this is really a no-brainer...

Thanks for any help,
Scogle
Re: line of a specific length?
Reply #1 - Apr 16th, 2010, 11:55am
 
line(x,y,x+length*cos(angle),y+length*sin(angle))
Re: line of a specific length?
Reply #2 - Apr 16th, 2010, 12:32pm
 
Thanks, that's exactly what I needed.  Now I wish I had payed better attention in trig class...
Re: line of a specific length?
Reply #3 - Apr 17th, 2010, 12:35am
 
There is another solution if the trig does your head in, and that is to use affine transforms. Then you could use:-
Code:

line(0, 0, length, 0);
translate(length, 0);

then do a rotate, and carry on in the same vein (you will have to learn about pushing and popping the matrix though, to create a branching tree).
As for drawing trees, it is a classic for using lsystems (as you may not want entirely random angles, as many of the random angles will produce an unsatisfying result). Smiley

Update Sunday 18th April, I went a bit mad exploring the drawing of trees in processing (albeit using ruby-processing, but it is quite easy to translate) see my posting on the processing implementation discourse http://processing.org/discourse/yabb2/num_1271596814.html
On my blog I have got simpler examples honest which do not involve the use of the context free DSL (results nowhere near as impressive though).
Page Index Toggle Pages: 1