# "if" statement imbroglio

**URL:** <https://discourse.processing.org/t/if-statement-imbroglio/2722>\
**Category:** Processing.py\
**Created:** [August 17, 2018, 1:03pm UTC](https://discourse.processing.org/t/if-statement-imbroglio/2722 "2018-08-17T13:03:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![solub](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/solub/32/333_2.png) [@solub](https://discourse.processing.org/u/solub)\
**Post date:** [August 17, 2018, 1:03pm UTC](https://discourse.processing.org/t/if-statement-imbroglio/2722/1 "2018-08-17T13:03:53Z")

</div>

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**.

 ![](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/b/b7b867912be524082ba107e24f7c44d9a98e37fa.jpeg)

So the common sense would be to state something like:

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

```

but then nothing changes, the overlaps are still visible:

 ![](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/c/c71e7c48ee291ddb741a0222aa3b6ff06bf7e0c6.png)

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

```auto
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)

```

---

<div class="post-metadata">

**Author:** ![hamoid](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hamoid/32/58_2.png) [@hamoid](https://discourse.processing.org/u/hamoid)\
**Post date:** [August 22, 2018, 12:12am UTC](https://discourse.processing.org/t/if-statement-imbroglio/2722/2 "2018-08-22T00:12:07Z")

</div>

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?

```python
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.

 ![2018-08-22-020745_800x1000_scrot](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/0/040f07e273e45a359457343620aab382a3d2cea8.png)

---

<div class="post-metadata">

**Author:** ![solub](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/solub/32/333_2.png) [@solub](https://discourse.processing.org/u/solub)\
**Post date:** [August 22, 2018, 4:15pm UTC](https://discourse.processing.org/t/if-statement-imbroglio/2722/3 "2018-08-22T16:15:14Z")

</div>

Hi @hamoid,

Thank you for the suggestions.

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

```auto
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.

 ![](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/e/ee3dca44aa56ea9580d5aa1e9035c260d1734b8f.png)  
 ![](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/e/ef9ddc99d706eb94a1a1ff2270a7b9b0b1aa9442.png)

Thanks again for your insight.

---

<div class="post-metadata">

**Author:** ![hamoid](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hamoid/32/58_2.png) [@hamoid](https://discourse.processing.org/u/hamoid)\
**Post date:** [August 22, 2018, 8:08pm UTC](https://discourse.processing.org/t/if-statement-imbroglio/2722/4 "2018-08-22T20:08:34Z")

</div>

beautiful 🙂 I like it!
