"if" statement imbroglio

Hi all,

I’m trying to display noised “waves” on top of each other while avoiding any overlaps.

The waves are filled with point lines computed with the lerp() function (interpolation between a noisy line and the next one below)

PROBLEM: Some of the waves are overlapping. More specifically, any noisy wave with a large amplitude will be overlapping the second wave coming after it.

So the common sense would be to state something like:

if p2.y < p0.y: 
    None # No point or strokeAlpha = 0

but then nothing changes, the overlaps are still visible:

Something must be off with the way I’m creating the if statement but not sure what it is.

def setup():
    background(240)
    size(800, 1000, P2D)
    smooth(8)
    
    n_points, edge = 20.0, 5
    n_cols, n_rows = 200, 20
    stepX, stepY = int(width/n_cols), int(height/n_rows)
    t, factor, amplitude = 0, .01, 200
    
    n = [[0 for y in range(n_rows)] for x in range(n_cols)]
    
    for y in range(n_rows):
         for x in range(n_cols):
             t += factor
             n[x][y] = amplitude * noise(t)

    for y in range(n_rows-2):
        rdmOffset = random(-1, 1)
        stroke(random(50))
        strokeWeight(random(1, 1.8))
        for x in range(n_cols):
            for e in range(int(n_points)):
          
                p0 = PVector(x*stepX, y*stepY + n[x][y]) # Points on the first line
                p1 = PVector(x*stepX + rdmOffset, (y+1)*stepY + n[x][y+1] - edge) # Points on the second line
                p2 = PVector(x*stepX + rdmOffset, (y+2)*stepY + n[x][y+2] - edge) # Points on the third line
                
                px = lerp(p0.x, p1.x, e/n_points) 
                py = lerp(p0.y, p1.y,  e/n_points)
 
                # If no overlapping  -> Draw a point
                if (p1.y) > (p0.y) and (p2.y) > (p0.y): 
                   point(px, py)
                    
                ###BUT I STILL GET OVERLAPS so stating again: ###
                # If overlapping -> Make the points invisible
                if (p2.y) < (p0.y):
                    stroke(100, 100, 255, 0)
1 Like

Hi!

Notice that p0, p1 and p2 do not depend on e, so they could be assigned outside the for e loop.
Notice also that your overlapping test does not take into account e, so the test returns the same value for all values of e.

Maybe this slightly changed program helps you figure out what is going on?

def setup():
    background(240)
    size(800, 1000, P2D)
    smooth(8)
    
    n_points, edge = 20.0, 5
    n_cols, n_rows = 200, 20
    stepX, stepY = int(width/n_cols), int(height/n_rows)
    t, factor, amplitude = 0, .01, 200
    
    n = [[0 for y in range(n_rows)] for x in range(n_cols)]
    
    for y in range(n_rows):
         for x in range(n_cols):
             t += factor
             n[x][y] = amplitude * noise(t)

    for y in range(n_rows-2):
        rdmOffset = random(-1, 1)
        stroke(random(50), random(50), random(50))
        strokeWeight(random(1, 1.8))
        for x in range(n_cols):
            p0 = PVector(x*stepX, y*stepY + n[x][y]) # Points on the first line
            p1 = PVector(x*stepX + rdmOffset, (y+1)*stepY + n[x][y+1] - edge) # Points on the second line
            p2 = PVector(x*stepX + rdmOffset, (y+2)*stepY + n[x][y+2] - edge) # Points on the third line
            
            for e in range(int(n_points)):
                                               
                px = lerp(p0.x, p1.x, e/n_points) 
                py = lerp(p0.y, p1.y, e/n_points)

                r = g = b = 0
                if py > p1.y:
                    r = 255
                if py > p0.y:
                    g = 255
                    
                stroke(r, g, b) 
                point(px, py)                

I only moved the assignment of p0, p1 and p2 up, and compared py (which depends on e) instead of comparing only p0, p1 and p2 which do not depend on e.

2 Likes

Hi @hamoid,

Thank you for the suggestions.

I figured the main problem was coming from the statements order. The following did the trick:

if py > p2.y or py > p1.y : None
elif py > p0.y: point(px, py)

Of course, this works as long as the amplitude value remains limited.


Thanks again for your insight.

3 Likes

beautiful :slight_smile: I like it!

1 Like