How can I create a randomly generated line drawing like my Picture

proceedural Hi I've been struggling along with the coding for this. The aims of the sketch are :

  1. To create a randomly generated line drawing, that uses only horizontal and vertical marks - I'll try to work the diagonal elements out myself cheers-.
  2. The first line should begin on the border of the sketch at a random point along -width, -height, width or height. I've kind of got that working but the startline just loops and I don't know how to stop that part - I guess due to the line going beyond the sktech border.- (and I'm pretty sure the code could be written more easily with classes. I'd like to try to work that part out on my own if thats okay.)
  3. After the start line the code is meant to draw connected lines at random going up,down left or right for a random distance. - This should happen for a random amount of lines.- I can't get this section to work at all.

so heres my messy code,

    float y = random(200);
    float x = random(200);
    float Sline = random(1);
    float Rline =random(1);
    float posx;
    float posy;
    float dis = random(20);

    void setup(){
      size(400,400);
      background(255);
      stroke(10);
      strokeWeight(random (2));
      posx = x;
      posy = y;
    }

    void draw(){
//---------this should be the start line---          
    for(int i = 0; i < 1; i ++){
      if( Sline<=0.25){ 
           if (Rline<=0.25){
           line (0,y,0+100,y);// right from left side   
        }else if (Rline<=0.5){
          line(width,y,width-100,y);//left from left             
        }else if(Rline<=0.75){
          line(0,y,0,y+100);//up on left                
        }else if(Rline>0.75){
          line(0,y,0,y-100);//down on left
        }

      }else if (Sline<=0.50){
         if (Rline<=0.25){
           line (width,y,width+100,y);// right from right side 
        }else if (Rline<=0.5){
          line(0,y,0-100,y);//left from right
        }else if(Rline<=0.75){
          line(width,y,width,y+100);//up on right
        }else if(Rline>0.75){
          line(width,y,width,y-100);//down on right
        }
       }else if (Sline<=0.75){
          if (Rline<=0.25){
           line (x,0,x,0+100);// down from up side 
        }else if (Rline<=0.5){
          line(x,height,x,height-100);//up from up
        }else if(Rline<=0.75){
          line(x,0,x-100,0);//left on up
        }else if(Rline>0.75){
          line(x,0,x+100,0);
        }
      }else if (Sline>0.75){ 
          if (Rline<=0.25){
           line (x,height,x,height+100);// up from down side 
        }else if (Rline<=0.5){
          line(x,0,x,0+100);//down from top
        }else if(Rline<=0.75){
          line(x,height,x-100,height);//left on bottom
        }else if(Rline>0.75){
          line(x,0,x+100,0);//right on bottom
        }
      }
    }
      //------------------next line values-----------
      // these lines should connect to the startline.
      //and the start line should not run again. 
     // the lines should be horizontal or vertical only.
     // I can get the lines drawing but they don't connect and are not solely vertical or horizontal
     float stepNum =random(20);
    for(int j = 0; j < stepNum; j ++){ 
      x = posx + random(-dis, dis);
      y = posy + random(-dis, dis);


      Rline=random(1);
      if (Rline<=0.25){
         line (posx, posy,x,(y-dis));
      }else if (Rline<=0.5){
        line(posx,posy,(x+dis), y);
      }else if(Rline<=0.75){
        line(posx,posy,x,(y+dis));
      }else if(Rline>0.75){
        line(posx,posy,(x-dis), y);
      }
      posx = x;
      posy = y;
    }
    //-----end drawvoid----
    }

I'd appreciate any help. I'm mainly a painter and I'm trying to incorporate processing into my work. It suits my procedural process. Sorry about the mess. Ta.

Tagged:

Answers

  • Answer ✓

    Honestly, I am not too keen on trying to understand your code. :-?

    The start line should be:

    1. Computed, ie. you should calculate all the coordinates, to keep the ending position, then only, once, call line() with these coordinates.
    2. Draw in setup(), so you don't keep drawing it on each frame.

    You might want to re-compute dist on each frame.
    You don't need a loop inside draw(): it is called regularly, it already acts like a loop.

  • Cool I'll have a look see and tidy the code based on that. It will get me out of the rut I'm in not tonight though as my eyes burn.

    Cheerrs for replying to my query.

  • No problem, try to improve (and show) your code, we will criticize it and help you to improve it further. :-D

  • I've reduced what I've been trying to do downto just getting the startline working. It's not :((

            // This sketch is meant to draw a line that starts randomly 
            //on the edge or boarder of the sketch. But what I get instead is 
            //two lines from 0,0 to 50 on the Y and X axis. What am I doing wrong?
    
            Sline SX = new Sline();
            Sline SY = new Sline();
    
            float W;
            float H;
            float x = random(width);
            float y= random(height);
            float dis =50;
    
            void setup(){
              size(400,400);
              background(255);
              strokeWeight(3);
              drawSXline();
              drawSYline();
              drawSline();
            }
    
            class Sline {
            float distance, direction;
    
    
            void SX(){
              distance = dis;
              direction = W;
              W  = random (0, width);
              drawSXline();
                }
    
            void SY(){
              distance = dis;
              direction = H;
              H  = random(0, height);
              drawSYline();
                }
            }
    
            void drawSline(){
            float val =1;
             if (val <= 0.5){ 
               drawSXline(); // line(W, y, W+dis, y);
             }else if (val > 0.5){
               drawSYline(); //line(x, H, x, H+dis);
             }
            }
    
             void drawSXline(){
               line(W,y,W+dis,Y);
             }
    
             void drawSYline(){
               line(x, H, x, H+dis);
             }
    
  • edited November 2013 Answer ✓

    There are a lot of problems with your code ... :-? :-? :-?

    1. SX SY you defined as objects but you have not called it anywhere inside the draw(). Since you have also defined these as class methods I don't have any idea why you did this.
    2. x,y = random(0,w,h) you defined in the 10 & 11 th line suppose to be inside the class because it is generating random value in every loop so that it can get different x,y every time.
    3. You are not passing any values inside these functions drawSXline() , drawSYline()
    4. Why have you made this function drawSline() when you haven't called it anywhere
    5. float val =1 in line 43 is not changing so it is always equal to 1 so how you gonna compare it is greater than 0.5 or less than 0.5 ?
    6. calling functions inside setup will run only once so you need to call it inside draw

          Sline SXX = new Sline();
          Sline SYY = new Sline();
      
          float W;
          float H;
      
          void setup() {
            size(400, 400);
            background(255);
            strokeWeight(3);
      
            //  drawSXline();
            //  drawSYline();
            //  drawSline();
          }
      
      
          void draw() {
            SXX.SX();
          }
      
          class Sline {
            float distance, direction;
            float dis = 50;
            float x = random(width);
            float y = random(height);
      
            void SX() {
              float val = random(0, 1);
              if (val <= 0.5) { 
                W  = random (0, width);
                line(W, y, W+dis, y);
                x = W+dis;
              }
              else  if (val > 0.5) {
                H  = random(0, height);
                line(x, H, x, H+dis);
                y =  H+dis;
              }
            }
          }
      
  • Thanks alot. :)) I've coded the way I've coded because I don't really know what I'm doing. I'm trying to understand classes and I will be studying this, breaking it and getting what I need out of it. Thanks for the the time you spent on the code. I will be able to get stuff sorted a bit better I hope. I've only been at this about a week and can only grab an hour or two here and there. But this time next year I'll hopefully be helping people out (probably by asking them to ask you as I have no idea what they are on about.) ;)

Sign In or Register to comment.