r/processing • • May 02 '21

I seek help

I have to write a program in processing (java) that starts a line in the middle and has a lenght of random(0, 39); and a random direction west, east, north and south random(0, 3); and stops when the accumulated lenght goes over 1500. I've been trying hard but can't wrap my head around the directions part and accumulation tracking. Thanks for the help.

0 Upvotes

14 comments sorted by

4

u/Wootai May 02 '21 edited May 02 '21

So, we need to break down this problem into a few smaller problems. There are a lot of approaches to this, and you’ve stated you cannot use arrays or methods. So let’s look at the problem and break it down:

  1. Start in the center. (Easy enough)

  2. Choose a random length for our line segment. (Again easy enough)

  3. Choose a random direction (Got that)

  4. Draw a line segment from our current position, to the next position that is our random length, and our random direction. (Hmm)

  5. if all of our line segments are 1500 pixels long, stop, otherwise go back to step to

Ok. So let’s look at some pseudo code:

 //start with some variables
 int startxposition = width/2 //start at our center
 int startyposition = height/2 //start at the center
 int endxposition = 0
 int endyposition =0
 int totalLength = 0
 int linelength = 0
 int linedirection = 0
 
 while (totalLength < 1500){  //keep doing this until our total length is greater than 1500
      lineLength = random(0,39)
      linedirection = random(0,3)
      
      //Lets figure out what our random direction means
      //we use our startx and starty values and then add or subtract
      //the line length from them to determine the next position

      if (linedirection == 0){ 
           endxposition = startxposition
           endyposition = startxposition - linelength; //go up
      }
      if (linedirection == 1){ 
           endxposition = startxposition + linelength //go right
           endyposition = startxposition 
      }
      if (linedirection == 2){  
           endxposition = startxposition
           endyposition = startxposition - linelength; //go down
      }
      if (linedirection == 3){  
           endxposition = startxposition - linelength; //go left
           endyposition = startxposition - linelength;
      }
      
       //draw the new line:
      line(startxposition, startyposition, endxposition, endyposition)
      
      //make the end positions the new start positions
      startxposition = endxposition
      startyposition = endyposition
      
     //add the length from our line to the totallength
     totallength += linelength

     //and from here our while loop will continue until totallength is greater than 1500

1

u/Jimmyboy142 May 02 '21

I tried it and sadly it doesn't deliver the right sketch 😕

1

u/Wootai May 02 '21

Where did it go wrong? What did you try to make it work? What I wrote was note runnable code.

1

u/Jimmyboy142 May 02 '21

Something with the bodies of the if statements Isn't sitting right with the program. I tweaked it a little and it still goes only up with some turns here and there. Thank you for your help❤️

1

u/Wootai May 02 '21

I think the issue might be with random() then. Random() returns a float between the two specified numbers. So it may be that the floats are not converting to integers correctly. Or are all converting to the floor, and therefor most converting to 0, the up.

1

u/Jimmyboy142 May 03 '21

Can it be because I initialised it like this int(random(0,39));? And if so how do I change it?

1

u/x-seronis-x May 03 '21

To get a number from 0 to 39 inclusive you use floor(ranodm(40))

Whatever you put in random you get a floating point value from zero to 'just under' the number you put in. which gives a range of the argument you used. Never use round() to convert to integer because this makes the first and last numbers only have half the chance to appear as all other numbers.

1

u/Jimmyboy142 May 03 '21

i have done it with (int)random(0,39); and it's given so in my instructions

1

u/Jimmyboy142 May 02 '21

Wow, big thanks. You're the best!

2

u/ChuckEye May 02 '21 edited May 02 '21

Off the top of my head I’m thinking an array of PVectors storing the X, Y coordinates of all the points and a conditional to stop adding to the array when the array length is greater than 1500.

Then each frame of draw you use point(x,y) to in a for loop with the current array length as the max?

1

u/Jimmyboy142 May 02 '21

Thank you, but I'm not allowed to use arrays, nor methods. I have to do it with a while loop. 😕 Also I checked and there's no Pvector methid in the documentation for processing and it does not work

1

u/ChuckEye May 02 '21

You can get away with not using an array if you’re not using a setup/draw structure.

1

u/x-seronis-x May 02 '21 edited May 02 '21

its length not lenght. There is no hard 't' sound at the end so dont spell it with one.

Also you mentioned in a comment some weird stipulations that you failed to put in the OP. Not everyone is going to read comments before replying. To avoid wasting the time of people who are willing to help you should always put any requirements in the OP. Its quite rude not to