Random Lines with no intersection



  • Hi its me again with another problem

    newPage(2000, 2000)
    xMiddle = 2000/2
    yMiddle = 2000/2
    strokeWidht=2
    
    
    g1=randint (20,100)
    g2=randint (500,900)
    g3=randint (20,100)
    g4=randint (20,100)
    g5=randint (20,100)
    
    
    w=1
    h=1
    def legs (x,y,w,h):
           
        scale(w, h)
        newPath()
        stroke(0)
        fill(None)
        strokeWidth(strokeWidht)
        polygon ((0,0),(g2,0),(g2,-g3),(g4,-g5),(g1,-g2),close=False)
    
    def legs2 (x,y,w,h):
           
        scale(w, h)
        newPath()
        stroke(0)
        fill(None)
        strokeWidth(strokeWidht)
        polygon ((0,0),(-g2,0),(-g2,-g3),(-g4,-g5),(-g1,-g2),close=False)
        
    # def wing (x,y):
    #     newPath()
    #     stroke(None)
    #     fill(0,0,20, 0.5)
    #     blendMode('exclusion')
    #     strokeWidth(strokeWidht)
    #     polygon ((0,29),(g2,g5),(g2,-g3),(g4,-g5),(g1,-g2),close=True)
        
        
       
    
    
    
    
    translate(xMiddle,yMiddle)
    rect(-20,-3,40,40)
    # wing(0,0)
    
    for i in range(4):
     
        g1=randint (20,100)
        g2=randint (20,644)
        g3=randint (20,100)
        g4=randint (20,100)
        g5=randint (20,100)
    
               
        
        
        
        
        translate (0,+i+5)
        
    
        legs(0,0,w,h)
        legs2(-0,-0,w,h)
        
    
    

    I wrote this code which generates "legs "randomly
    now Iam looking to generate these randomly without them intersecting each others

    Maybe one of you have a little hint for me

    Greetings David



  • @mrrouge
    I am not quite sure if this is what you are asking for but you either need to translate(x,y) the maximum leg length or width or just draw the legs at the position you want them to. In your legs() function you do have the parameters x and y but they are not used inside the function.

    Also I do recommend to use a variable if you repeatedly use the same value. So instead of randint(20, 100) you could use

    min_val = 20
    max_val = 100
    randint(min_val, max_val)
    

    I put together a simple “legs” drawing that might help.
    Good luck

    # -------------
    #  settings 
    
    pw = ph = 500
    amount_x = 10
    amount_y =  4
    
    cell_w = pw / amount_x
    cell_h = ph / amount_y
    
    # -------------
    #  fucntion(s)
    
    def legs(pos, max_w, max_h):
    
        x, y = pos
        length = random() * max_h
        step_s = random() * max_w
        
        polygon((x - step_s, y - length) , pos, (x + step_s, y - length), close = False)
    
    
    # -------------
    #  drawings
    
    newPage(pw, ph) 
    fill(None)
    stroke(0)
    strokeWidth(2)
    
    for x in range(amount_x):
        for y in range(amount_y):
            legs((x * cell_w + cell_w/2, y * cell_h + cell_h/2), cell_w/2, cell_h/2)
    


  • hey thanks for the answer

    but its not totally what i meant

    if I had to reformulate it now i would have asked for a function which scans the previous one and creates a random pair of legs "on top" of the other and places it without intersections ans rescans the newly created one again and again .

    the basic "idea" of the sketch was how i wanted it
    but the lines were too chaotic

    i hope my text is not too incomprehensible

    regards David



  • @mrrouge

    ok if i got you right you want the function to return a value — in your case the height of the legs. this value could then be used in a while loop. while loops are quite helpful but might run forever if done incorrect. I adapted the code a bit so the legs function returns the length of the legs which is then added to the y value in the while loop. There is a check inside the while loop so it will run for a maximum of 3000 times.

    # -------------
    #  settings 
    
    pw = ph = 500
    amount_x = 10
    
    cell_w = pw / amount_x
    
    max_h  = 50
    gap = max_h * .2
    
    # -------------
    #  fucntion(s)
    
    def legs(pos, max_w, max_h):
    
        x, y = pos
        length = random() * max_h
        step_s = random() * max_w
        polygon((x - step_s, y) , (x, y + length), (x + step_s, y), close = False)
        
        return length
    
    # -------------
    #  drawings
    
    newPage(pw, ph) 
    fill(None)
    stroke(0)
    strokeWidth(2)
    
    for x in range(amount_x):
        
        count = 0
        y = -random() * max_h
        while y < ph:
            y += legs((x * cell_w + cell_w/2, y), cell_w/2, max_h)
            y += gap
            count += 1
            
            if count > 3000: break 
    
    

    and the while loop without the check:

    for x in range(amount_x):
        y = -random() * max_h
        while y < ph:
            y += legs((x * cell_w + cell_w/2, y), cell_w/2, max_h)
            y += gap
    
    

    legs.jpg