Drawing a line in a random direction from the end of a line

Just started out using processing for a project and theres a drawing I’m trying to create.
If this makes sense I’m trying to,

Draw a line from a random point
with a random length
at a random direction

Then draw a line of the same length as the line before
starting from the end point of the last line and in a random direction

Then continue to draw lines like the second repeatedly in draw() until the line goes off the screen.

I have managed to draw the first 2 lines but I’m struggling to work out how to repeat the process.

This is what I’ve got:

float a = random(width);
float b = random(height);
float c = random(width);
float d = random(height);
float dis = dist(0, 100, 0, 0);

//intial point
float zX = a;
float zY = b;

//first point (fX,fY)
float fX = zX + cos( a  )*dis;
float fY = zY+ sin( b  )*dis;

void setup() {
  size(1400, 800);

  //first line
  line(zX, zY, fX, fY);

void draw() {
  float d1 = dist(zX, zY, fX, fY);

  //new point = (px,py)
  float px = fX + cos( c  )*d1;
  float py = fY + sin( d  )*d1;

  line(fX, fY, px, py);

I have tried too many methods and can’t get it right!!
Any help would be great!


1 Like


Can you please edit your post and format your code?

I’m sorry I’m not going more into what you have done so far but have you consider using PVector objects? They have very handdy methods for what you want to do. Here is an example:

PVector startPoint;
float l;

void setup() {
  size(1280, 1024);
  startPoint = new PVector(random(width), random(height)); // Initialize with a random point on the screen
  l = random(50, 200);                                     // Get a random length for the lines

void draw() {
  PVector randomVec = PVector.random2D();                     // Get a random vector
  randomVec.mult(l);                                          // Give that vector the length of your line
  randomVec.add(startPoint);                                  // Add it to the start point to figure out the end point
  line(startPoint.x, startPoint.y, randomVec.x, randomVec.y); // Draw the line
  startPoint = randomVec.copy();                              // Keep he coordinate of that last point -> it will be the first point of the next line
1 Like

Sorry didn’t know how to format it. I had a look at them but was struggling to understand them but ill have a read thanks!

Now, with the code you provided, you can do some cleaning. You have plenty of variables to keep track of just 4 points. So you want to get read of the zX, zY, fX, fY.

Then the basic idea to have an infinite loop is to set your end point as the start point for the new loop.

And to get the random direction, you simply need to get a random angle.

float a, b, c, d;
float dis = dist(0, 100, 0, 0);

void setup() {
  size(1400, 800);
  a = random(width);
  b = random(height);

void draw() {
  // Get the end point
  float randomAngle = random(TWO_PI);
  c = dis * cos(randomAngle) + a;
  d = dis * sin(randomAngle) + b;
  // Draw the line
  line(a, b, c, d);
  // Set the end point as the new start point for the next loop
  a = c;
  b = d;
1 Like

ah yes that makes more sense was totally over complicating it.
thanks so much