# Difficulty in understanding noise turbulence code from processing book creating organic textures

**URL:** <https://discourse.processing.org/t/difficulty-in-understanding-noise-turbulence-code-from-processing-book-creating-organic-textures/16075>\
**Category:** Coding Questions\
**Created:** [December 3, 2019, 10:57am UTC](https://discourse.processing.org/t/difficulty-in-understanding-noise-turbulence-code-from-processing-book-creating-organic-textures/16075 "2019-12-03T10:57:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ruchi](https://avatars.discourse-cdn.com/v4/letter/r/f475e1/32.png) [@Ruchi](https://discourse.processing.org/u/Ruchi)\
**Post date:** [December 3, 2019, 10:57am UTC](https://discourse.processing.org/t/difficulty-in-understanding-noise-turbulence-code-from-processing-book-creating-organic-textures/16075/1 "2019-12-03T10:57:04Z")

</div>

```auto
float power=6; //TURBULENCE POWER
  float d=8; //TURBULENCE DENSITY
  noStroke();
  for (int y=0; y<height; y++) {
    for (int x=0; x<width; x++) {
      float total=0.0;
      for (float i=d; i>=1; i=i/2.0) {
        total+=noise(x/d, y/d)*d;
        //println("total"+total);
      }
      float turbulence=128.0*total/d;
      ////println("turbulence"+turbulence);
      float base=(x*0.2)+(y*0.12);
      //println("x"+x);
      //println("y"+y);
      //println("base"+base);
      float offset=base+(power*turbulence/256.0);
      //println("offset"+offset);
      float gray=abs(sin(offset))*256.0;
      stroke(gray);
      point(x, y);
    }
  }

```

This code is from processing book from section Math4:Random Pg. 132. I was playing around with this code but certain variables I find hard to understand.  
Like why total is accumulated till the amount of d. What is the role of turbulence & base? I can’t seem to get my head around this. Skipping some shows changes in textures but I can’t understand intuitively how it works as to why some variables were created in the first place.  
Elaborate explanation would help. Thanks:)

---

<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:** [December 3, 2019, 2:57pm UTC](https://discourse.processing.org/t/difficulty-in-understanding-noise-turbulence-code-from-processing-book-creating-organic-textures/16075/2 "2019-12-03T14:57:49Z")

</div>

I think it’s a typo. `d` doesn’t change in that for loop, so it’s basically calculating the same noise value 4 times. I think the intention was to calculate noise at different scales. The variable `i` contains 8.0, 4.0, 2.0, 1.0. So I think it should say `total += noise(x/i, y/i)*i`.

The Processing noise() function implements internally different levels of detail (see the `noiseDetail()` function). But the aesthetic is different.

An unrelated interesting thing is that this code uses point(), which I think is slower than `set()`, which is slower than accessing the `pixels[]` array to set pixel colors directly. But `point()` is more intuitive.

In case you are curious you can see how `noise()` is implemented here:

> <https://github.com/processing/processing/blob/master/core/src/processing/core/PApplet.java#L5300>

  
That code can be a bit tricky to understand because it uses both `<<` and `*=` for multiplication and several constants that need to be looked up elsewhere.

---

<div class="post-metadata">

**Author:** ![Ruchi](https://avatars.discourse-cdn.com/v4/letter/r/f475e1/32.png) [@Ruchi](https://discourse.processing.org/u/Ruchi)\
**Post date:** [December 4, 2019, 6:48am UTC](https://discourse.processing.org/t/difficulty-in-understanding-noise-turbulence-code-from-processing-book-creating-organic-textures/16075/3 "2019-12-04T06:48:37Z")

</div>

@hamoid Thanks! I somewhat understood the total noise accumulation as its similar to the perlin noise octave interpolation concept.

Changing the scale by which it accumulates (i=i/2) causes changes in the overall smoothness of the stripes.  
For i=i/1.5, the accumulation of noise is more causing deviation in the overall shape. Likewise if i=i/3, the accumulation of noise is less causing smooth change in the stripes.  
As for d , it imparts noise by giving tiny variations to the overall shape of stripes causing granular type look.  
For high value d, noise is less causing lesser tiny variations. For low value d, noise is more causing more tiny variations.
