# How to Plot a Mandelbulb

**URL:** <https://discourse.processing.org/t/how-to-plot-a-mandelbulb/23490>\
**Category:** Coding Questions\
**Created:** [August 25, 2020, 6:52pm UTC](https://discourse.processing.org/t/how-to-plot-a-mandelbulb/23490 "2020-08-25T18:52:41Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![rubbs](https://avatars.discourse-cdn.com/v4/letter/r/838e76/32.png) [@rubbs](https://discourse.processing.org/u/rubbs)\
**Post date:** [August 25, 2020, 6:52pm UTC](https://discourse.processing.org/t/how-to-plot-a-mandelbulb/23490/1 "2020-08-25T18:52:41Z")

</div>

Hello,

I am trying to recreate the mandelbulb in processing (a 3D version of the mandelbrot set). I have looked through multiple sites for the equations to plot but they are different depending on the site. I am also struggling to actually plot the equations as-well (currently just plotting parallel lines). I know that it’s weird to plot using points but I’m really keen to understand the basics before making this project more complicated.

If anyone can give me some advice or pointers I would really appreciate it 🙂

```auto
/*
BY: Callum Clegg
DATE: Tuesday 25th August 2020
TITLE: Mandelbulb
*/

// -----------------------------------START-----------------------------------
// ~variables
ArrayList<PVector> points = new ArrayList();

float i;

void setup() {
  size(800, 800, P3D);
  colorMode(HSB);

  for (i = -50; i <= 50; i += 0.01) {
    points.add(mandlebulb(i, i, i));
  }
  
}

void draw() {
  background(0);
  translate(width/2, height/2, -850);
  
  float hu = 0;
  for (PVector v : points) {
      stroke(hu+=0.1 * (i * 0.1), 180, 170);
      strokeWeight(2);
      point
      (
        v.x * 10,
        v.y * 10,
        v.z * 10
      ); 
      if(hu > 255) { hu = 0; }
  }

}

void plot() {
  noFill();
  
}

PVector mandlebulb(float x, float y, float z) {
    
  /* 
  Calculating spherical coordinates.
  source: https://en.wikipedia.org/wiki/Mandelbulb
  */
  
  float r = sqrt(pow(x, 2) + pow(y, 2) + pow(z, 2));
  float theta = atan(y/x);
  float phi = acos(z/r);
  
  PVector v = new PVector(r, theta, phi);
  
  return v;
}

// ------------------------------------END------------------------------------

```

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [August 25, 2020, 9:36pm UTC](https://discourse.processing.org/t/how-to-plot-a-mandelbulb/23490/2 "2020-08-25T21:36:05Z")

</div>

> [@rubbs](#):
>
> I know that it’s weird to plot using points

Is there another way to render this other then plotting points?  
I really love 3d fractal´s but I never thought of it because when I coded a 2d mandelbrot ([see here](https://www.openprocessing.org/sketch/794150)) I already saw that this renders very slow, let alone a 3d.  
But I’m interested and found [this page](https://www.iquilezles.org/www/articles/mandelbulb/mandelbulb.htm) to give a bit more insight.  
When I finish my current projects I definitely will look into this.

---

<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:** [August 26, 2020, 9:53am UTC](https://discourse.processing.org/t/how-to-plot-a-mandelbulb/23490/3 "2020-08-26T09:53:51Z")

</div>

Mikael Hvidtfeldt Christensen (Structure Synth and Fragmentarium projects) is a very interesting person to follow when it comes to 3D Fractals see this [blog entry](http://blog.hvidtfeldts.net/index.php/2009/12/mandelbulb-implementations/). Interestingly I found this [shadertoy example](https://www.shadertoy.com/view/MdXSWn) might be possible to convert to run with processing.

---

<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:** [August 26, 2020, 8:20pm UTC](https://discourse.processing.org/t/how-to-plot-a-mandelbulb/23490/4 "2020-08-26T20:20:58Z")

</div>

OMG it was that simple, used Raphaël de Courville [SableRaf/Shadertoy2Processing](https://github.com/SableRaf/Shadertoy2Processing) on [shadertoy example](https://www.shadertoy.com/view/MdXSWn)

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

Needs refinement to remove unnecessary parameters, but hey it worked out of the tin…

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [August 26, 2020, 9:16pm UTC](https://discourse.processing.org/t/how-to-plot-a-mandelbulb/23490/5 "2020-08-26T21:16:24Z")

</div>

Have not looked into it yet, but is it static?  
If animated what frameRate did you get?

---

<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:** [August 27, 2020, 6:48am UTC](https://discourse.processing.org/t/how-to-plot-a-mandelbulb/23490/6 "2020-08-27T06:48:34Z")

</div>

About 30 fps, on Linux Mint 5.4.0-42-generic. Hardware GeForce GT 730, driver NVidia-440.10, AMD A4-7300 APU. About 12 fps as a 1080, 720 sketch. Did you see how the sketch generated a job offer?

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [August 27, 2020, 9:27am UTC](https://discourse.processing.org/t/how-to-plot-a-mandelbulb/23490/7 "2020-08-27T09:27:41Z")

</div>

Do you just load a model or is it really calculated?

Can you please post your code?

---

<div class="post-metadata">

**Author:** ![neilcsmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/neilcsmith/32/144_2.png) [@neilcsmith](https://discourse.processing.org/u/neilcsmith)\
**Post date:** [August 27, 2020, 10:28am UTC](https://discourse.processing.org/t/how-to-plot-a-mandelbulb/23490/8 "2020-08-27T10:28:48Z")

</div>

Ah, fun! 😄

 ![Screenshot from 2020-08-27 11-07-08](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/a/a15354d516e681ade3804e66027e4be861839c19.jpeg)

Both ~43fps at 1024x768 (Radeon RX560) with LWJGL backend via PraxisLIVE and JOGL backend in standard Processing 3.5.3. Been trying a few different types of sketch to ensure similar (if not better, but definitely not worse) performance via LWJGL.

I liked your name for it! 😉

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [August 27, 2020, 12:04pm UTC](https://discourse.processing.org/t/how-to-plot-a-mandelbulb/23490/9 "2020-08-27T12:04:25Z")

</div>

> [@monkstone](#):
>
> Did you see how the sketch generated a job offer?

Yes, but I think you will have to be an expert in the GLSL language.  
As soon as I’m done with current projects I will start to study [this](https://learnopengl.com/Getting-started/Shaders) excellent OpenGL tutorial.  
I wonder what the frameRate would be on a ordinary PC without an external video card.  
Did you just copy the shader code of the link you gave above and saved it as explained in Processing’s Shaders tutorial?

---

<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:** [August 27, 2020, 1:15pm UTC](https://discourse.processing.org/t/how-to-plot-a-mandelbulb/23490/10 "2020-08-27T13:15:33Z")

</div>

Initially I used [Raphaël de Courville](https://github.com/SableRaf/Shadertoy2Processing) defaultShader, but then edited it to be more like the [shaders by codeanticode](https://github.com/ruby-processing/JRubyArt-examples/blob/master/processing_app/topics/shaders/data/mandelbub.glsl).

---

<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:** [August 27, 2020, 1:24pm UTC](https://discourse.processing.org/t/how-to-plot-a-mandelbulb/23490/11 "2020-08-27T13:24:18Z")

</div>

See [JRubyArt-example](https://github.com/ruby-processing/JRubyArt-examples/tree/master/processing_app/topics/shaders) shader in data folder,

---

<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:** [August 27, 2020, 1:44pm UTC](https://discourse.processing.org/t/how-to-plot-a-mandelbulb/23490/12 "2020-08-27T13:44:45Z")

</div>

@noel @neilcsmith had this vague feeling that there was a Sunflow example, but also [Fragmentarium](http://syntopia.github.io/Fragmentarium/) of a Mandelbulb. But the sunflow turned out to be a [sphereflake](https://github.com/fpsunflower/sunflow/blob/master/examples/sphereflake.sc). May’be bub wasn’t such a good idea, apparently it might be equivalent to `bro` in US but kind of derogatory…

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [August 27, 2020, 2:25pm UTC](https://discourse.processing.org/t/how-to-plot-a-mandelbulb/23490/13 "2020-08-27T14:25:42Z")

</div>

> [@monkstone](#):
>
> defaultShader

That made it really easy !  
Only a frameRate of 6 on my modest PC, but still very nice.  
I’m exited to learn GLSL now!
