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 › random and polyline
Page Index Toggle Pages: 1
random and polyline (Read 392 times)
random and polyline
Aug 18th, 2008, 6:03am
 
Hi,
So this is a very fundamental question. I'm writing an applet that will create polylines (to use CAD terminology) based on random points. If I draw a line generated from points x,y and u,v, I want to draw the next point starting at u,v.  I'm using the translate and rotate commmnd with this. I'm also using random. I'm hoping that the random values I generate would be consistent if I use the randomSeed function. However this doesn't seem to be working. That is to say, the line drawn from (u,v,a,b) does not meet up with the line (x,y,u,v). Can anyone tell me what I'm doing wrong. Is there a way to save out random values once they are generated once, so that they will be used consistently? thanks....

float y = 0;

void setup () {
 size (500,500);
 background (255);
 smooth ();
 frameRate (4);
}

void draw () {
strokeWeight (.2);
float z = 400;
randomSeed (1);
float x = random (20,420);
float y = random (20,420);
float u = random (20,420);
float v = random (20,420);
float a = random (20,420);
float b = random (20,420);

 if (z>=350) {
   line (x,y,u,v);
   translate (u,v);
   rotate (PI/2);
   line (u,v,a,b);
   }
 }

Re: random and polyline
Reply #1 - Aug 18th, 2008, 8:52am
 
randomSeed is to generate always the same random sequence for a given seed. So if you use it in draw(), the values will be always the same, ie. not random at all. Usually a call to this function is done only once, in setup().

And I don't understand your use of translate & rotate. If you change the coordinate system, lines starting at the same coordinates values will not join.
If I just remove randomSeed and translate/rotate, I get a bunch of random pairs of lines, connected on u,v.
If you want to draw connected lines, draw one line per draw() call, save the end point in a global variable and use it as starting point in the next iteration.

That would be something like:

Code:
float u = 0, v = 0;

void setup () {
size (500,500);
background (255);
smooth ();
frameRate (4);
}

void draw () {
strokeWeight (.2);
float z = 400;
float x = random (20,420);
float y = random (20,420);

if (z>=350) {
line (x,y,u,v);
u = x; v = y;
}
}
Page Index Toggle Pages: 1