# Newton Fractal Sketch

**URL:** https://discourse.processing.org/t/newton-fractal-sketch/24211
**Category:** Gallery
**Created:** [September 29, 2020, 7:43am UTC](https://discourse.processing.org/t/newton-fractal-sketch/24211 "2020-09-29T07:43:26Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![monkstone](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/monkstone/32/64_2.png) [@monkstone](https://discourse.processing.org/u/monkstone)
#### Post date: [September 29, 2020, 7:43am UTC](https://discourse.processing.org/t/newton-fractal-sketch/24211/1 "2020-09-29T07:43:26Z")

</div>

@villares @solub @tabreturn [Jörg Kantel](http://cognitiones.kantel-chaos-team.de/cv.html) has been at it again on [his blog](http://blog.schockwellenreiter.de/index.html), I picked up his NewtonFractal sketch and translated it for JRubyArt here:-

```ruby
# frozen_string_literal: true

IMGX = 512
IMGY = 512
MAXIT = 20 # max iterations allowed
EPS = 1e-3 # max error allowed
COMPLEX = Complex(1e-6, 1e-6) # step increment

attr_reader :xa, :xb, :ya, :yb, :z

def settings
  size(IMGX, IMGY)
end

def setup
  sketch_title 'Newton Fractal'
  color_mode(HSB)
  # Drawing area
  @xa = -1.0
  @xb = 1.0
  @ya = -1.0
  @yb = 1.0
  no_loop
end

def draw
  load_pixels
  grid(IMGY, IMGX) do |y, x|
    zy = y * (yb - ya) / (IMGY - 1) + ya
    zx = x * (xb - xa) / (IMGX - 1) + xa
    @z = Complex(zx, zy)
    (0...MAXIT).each do |i|
      # Newton iteration
      z0 = z - func(z) / ((func(z + COMPLEX) - func(z)) / COMPLEX)
      break if (z0 - z).abs < EPS

      @z = z0
      # pixels[x + y * width] = color(i % 5 * 64, i % 17 * 16, i % 9 * 32)
      pixels[x + y * width] = color(i%5*64, i%9*32, i%17*16)
    end
  end
  update_pixels
end

def func(z)
  # z**3 - 1.0
  z**4 - 1.0
end

```

Here is what his suggested variation looks like:-

 ![newton_fractal](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/6/690b2181077e32edc1457d5254e8968b5f069671.png)

---

<div class="post-metadata">

### Author: ![monkstone](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/monkstone/32/64_2.png) [@monkstone](https://discourse.processing.org/u/monkstone)
#### Post date: [September 29, 2020, 9:17am UTC](https://discourse.processing.org/t/newton-fractal-sketch/24211/2 "2020-09-29T09:17:00Z")

</div>

In principle you can explore any number of roots of unity eg `z**8`:-

 ![eighth](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/3/3560a1110ea8bad81df280717856ba376a8450b1.png)  
Here I’ve expanded the drawing area to `-2 to 2` get a wider perspective.

---

<div class="post-metadata">

### Author: ![monkstone](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/monkstone/32/64_2.png) [@monkstone](https://discourse.processing.org/u/monkstone)
#### Post date: [September 29, 2020, 12:34pm UTC](https://discourse.processing.org/t/newton-fractal-sketch/24211/3 "2020-09-29T12:34:41Z")

</div>

Then if you substitute for some other funky cubic function there is this:-

 ![funky_function](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/9/9dfa5df5be1c9a9fff17c9f4973db3274b5a54c3.png)  
Or this view:-  
 ![funky2](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/6/64c5a24d038d2d31186737e5f1bd917daa13e80b.png)

---

<div class="post-metadata">

### Author: ![villares](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/villares/32/3166_2.png) [@villares](https://discourse.processing.org/u/villares)
#### Post date: [September 29, 2020, 1:45pm UTC](https://discourse.processing.org/t/newton-fractal-sketch/24211/4 "2020-09-29T13:45:26Z")

</div>

Thank you so much @monkstone! It is lovely!

```auto
# from https://discourse.processing.org/t/newton-fractal-sketch/24211
IMGX = 512
IMGY = 512
MAXIT = 20 # max iterations allowed
EPS = 1e-3 # max error allowed, could use Processing's EPSILON constant
COMPLEX = complex(1e-6, 1e-6) # step increment

def settings():
    size(IMGX, IMGY)

def setup():
    global xa, xb, ya, yb
    this.surface.setTitle('Newton Fractal')
    colorMode(HSB)
    # Drawing area
    xa = -1.0
    xb = 1.0
    ya = -1.0
    yb = 1.0
    noLoop()

def draw():
    loadPixels()
    for y in range(IMGY):
        for x in range(IMGX):
            zy = y * (yb - ya) / (IMGY - 1) + ya
            zx = x * (xb - xa) / (IMGX - 1) + xa
            z = complex(zx, zy)
            for i in range(MAXIT):
                # Newton iteration
                z0 = z - func(z) / ((func(z + COMPLEX) - func(z)) / COMPLEX)
                if abs(z0 - z) < EPS:
                    break
                z = z0
                # pixels[x + y * width] = color(i % 5 * 64, i % 17 * 16, i % 9 * 32)
                pixels[x + y * width] = color(i %
                                              5 * 64, i % 9 * 32, i % 17 * 16)
    updatePixels()

def func(z):
    # return z**3 - 1.0
    return z ** 4 - 1.0

```

---

<div class="post-metadata">

### Author: ![monkstone](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/monkstone/32/64_2.png) [@monkstone](https://discourse.processing.org/u/monkstone)
#### Post date: [September 29, 2020, 5:39pm UTC](https://discourse.processing.org/t/newton-fractal-sketch/24211/5 "2020-09-29T17:39:10Z")

</div>

Here’s another variation with a complex polynomial function (ie `4+3i`):-

 ![complex_polynomial](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/f/f44ca38493be1f8b750a2ba07588e8e22144ccce.png)  
Or this with `-0.5+3`:-  
 ![fractional](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/c/cf97a287b5bd524280464167f843ffb2afa0e626.png)

---

<div class="post-metadata">

### Author: ![monkstone](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/monkstone/32/64_2.png) [@monkstone](https://discourse.processing.org/u/monkstone)
#### Post date: [October 2, 2020, 6:11pm UTC](https://discourse.processing.org/t/newton-fractal-sketch/24211/6 "2020-10-02T18:11:53Z")

</div>

In this sketch I take advantage of the built in support for complex numbers in ruby. But this got me thinking that I’ve yet to take advantage of [ruby refinements](https://rubyreferences.github.io/rubyref/language/refinements.html) (a different sort of [monkey patching](https://en.wikipedia.org/wiki/Monkey_patch) that does not pollute global namespace. I reasoned that it might be quite interesting to explore refinements with the Vec2D class see my [recent blog entry](https://monkstone.github.io/jruby_art/update/2020/10/02/Refinements.html). This is akin to vec2 glsl code in my [droste.glsl](https://github.com/ruby-processing/picrate-examples/blob/master/library/video/capture/data/droste.glsl).
