Logo by AGUS - Contribute your own Logo!

END OF AN ERA, FRACTALFORUMS.COM IS CONTINUED ON FRACTALFORUMS.ORG

it was a great time but no longer maintainable by c.Kleinhuis contact him for any data retrieval,
thanks and see you perhaps in 10 years again

this forum will stay online for reference
News: Visit the official fractalforums.com Youtube Channel
 
*
Welcome, Guest. Please login or register. November 29, 2025, 04:23:48 PM


Login with username, password and session length


The All New FractalForums is now in Public Beta Testing! Visit FractalForums.org and check it out!


Pages: [1]   Go Down
  Print  
Share this topic on DiggShare this topic on FacebookShare this topic on GoogleShare this topic on RedditShare this topic on StumbleUponShare this topic on Twitter
Author Topic: Rendering Buddhabrot in Processing (need help)  (Read 697 times)
Description: Need help with my Processing code
0 Members and 1 Guest are viewing this topic.
Iariak
Alien
***
Posts: 26



« on: March 13, 2017, 06:30:30 PM »

Hello everyone. Basically I am trying to recreate my atrociously slow VBA program for rendering the Buddha and Anti-Buddha versions of different fractals in Processing aaaaaand it's not working. This is especially infuriating because I have made it work in significantly inferior environment before but I just can't get it to work here  sad I will attach the image the program is generating and the entirety of the code. If anyone could take a look at it (the code is probably terrible, sorry cheesy ), I would be extremely grateful. I suspect the error is in the incrementPixels function and in the way I am mapping the values but I have no idea how to do it differently. Thanks!

Code:
float range;
int maxIter;
int[] newPixels;
void setup () {
  size(600, 600);
  range = 2;
  maxIter = 500;
  newPixels = new int[width * height];
}

void draw() {
  render();
  colorPix();
  newPixels = new int[width * height];
  saveFrame("failed-buddha.jpg");
}

void render() {
  // this function just picks numbers and passes them down for iterating and stuff.
  for (float x = -range; x < range; x += 0.001) {
    for ( float y = -range; y < range; y += 0.001) {
      test (x, y);
    }
  }
}

void test(float x, float y) {
  // This function tests if the complex number is part of M-set.
  // If it is not, the number is passed to the incrementPixels function.
  C cNum = new C(x, y);
  for (int i = 0; i < maxIter; i++) {
    cNum.MIter();
    if (cNum.escaped()) {
      incrementPixels(cNum);
      return;
    }
  }
}

void incrementPixels(C cNum) {
  // This function takes values stored in track ArrayList
  // (all the values Z became during iterating)
  // and increment values for according pixels. At least that's what I want it to do.
  // I suspect this is where the error is, but I have no idea what's wrong.
  for (float[] nums : cNum.track) {
    float r = nums[0];
    float i = nums[1];
    float coord =  map(r, -range, range, 0, width) + map(i, -range, range, 0, height) * width;
    if (coord < newPixels.length && coord >= 0) {
      newPixels[int(coord)] += 1;
    }
  }
}

void colorPix() {
  // This just takes the newPixels array and loads it onto the screen.
  loadPixels();
  for (int i = 0; i < pixels.length; i++) {
    pixels[i] = color(newPixels[i]);
  }
  updatePixels();
}

// Following is the code for the C class that represents complex number
// I doubt there is anything wrong here
class C {
  float i;
  float r;
  float iConst;
  float rConst;
  ArrayList<float[]> track;
  C (float real, float imaginary) {
    i = imaginary;
    r = real;
    iConst = i;
    rConst = r;
    track = new ArrayList<float[]>();
  }
  void MIter() {
// fixed this bit, thanks to Claude.
    float br = r;
    r = r * r - i * i + rConst;
    i = 2 * br * i + iConst;
    float[] comp = new float[2];
    comp[0] = r;
    comp[1] = i;
    track.add(comp) ;
    
  }
  
  boolean escaped() {
    if (r * r + i * i > 16) {
      return true;
    } else {
      return false;
    }
  }
}



* failed-buddha.jpg (107.5 KB, 600x600 - viewed 67 times.)
« Last Edit: March 13, 2017, 07:54:04 PM by Iariak » Logged
claude
Fractal Bachius
*
Posts: 563



WWW
« Reply #1 on: March 13, 2017, 07:24:11 PM »


Code:
  void MIter() {
    float bi = i;
    r = i * i - r * r + rConst;
    i = 2 * bi * r + iConst;
    float[] comp = new float[2];
    comp[0] = r;
    comp[1] = i;
    track.add(comp) ;
   
  }

You should save r instead of i, here you use the new value of r instead of the old one when updating i.  Don't know if this is the only problem...
Logged
Iariak
Alien
***
Posts: 26



« Reply #2 on: March 13, 2017, 07:49:02 PM »

Oh yea, that's a dumb mistake. The result is pretty much the same though, code still doesn't generate what I want. Thank you so much for looking through my code though  smiley

* I did some poking around and found out that if I change

float coord =  map(r, -range, range, 0, width) + map(i, -range, range, 0, height) * width;
to
float coord =  map(cNum.iConst, -range, range, 0, width) + map(cNum.rConst, -range, range, 0, height) * width;

Which should just show the fractal rotated, or viewed from a different angle (from this angle, it should be pretty much just a regular Mandelbrot set), it gives me this (attached). It might give you more of an idea of what's happening. I have no clue as to why it's spread out like this.

One last edit...I found the mistake myself, it turned out that the way I was creating the newPixels array and assigning it to the pixels array was completely stupid and nonsensical cheesy Here is the fixed code
Code:
float range;
int maxIter;
int[][] newPixels;
void setup () {
  size(600, 600);
  range = 2;
  maxIter = 500;
  newPixels = new int[width][height];
}

void draw() {
  render();
  colorPix();
  newPixels = new int[width][height];
  saveFrame("failed-buddha.jpg");
}

void render() {
  // this function just picks numbers and passes them down for iterating and stuff.
  for (float x = -range; x < range; x += 0.001) {
    for ( float y = -range; y < range; y += 0.001) {
      test (x, y);
    }
  }
}

void test(float x, float y) {
  // This function tests if the complex number is part of M-set.
  // If it is not, the number is passed to the incrementPixels function.
  C cNum = new C(x, y);
  for (int i = 0; i < maxIter; i++) {
    cNum.MIter();
    if (cNum.escaped()) {
      incrementPixels(cNum);
      return;
    }
  }
}

void incrementPixels(C cNum) {
  // This function takes values stored in track ArrayList
  // (all the values Z became during iterating)
  // and increment values for according pixels. At least that's what I want it to do.
  // I suspect this is where the error is, but I have no idea what's wrong.
  for (float[] nums : cNum.track) {
    float r = nums[0];
    float i = nums[1];
    float iC = cNum.iConst;
    float rC = cNum.rConst;
    float xcoord = map(r, -range, range, 0, width);
    float ycoord = map(i, -range, range, 0, height);
    if (xcoord >= 0 && xcoord < width && ycoord >= 0 && ycoord < height) {
      newPixels[int(xcoord)][int(ycoord)] += 1;
    }
  }
}


void colorPix() {
  // This just takes the newPixels array and loads it onto the screen.
  loadPixels();
  for (int i = 0; i < width; i++) {
    for (int j = 0; j < height; j++) {
      float val = newPixels[i][j];
      pixels[j + i * width] = color(map(val, 0, 1000, 0, 255));
    }
  }
  updatePixels();
}

// Following is the code for the C class that represents complex number
// I doubt there is anything wrong here
class C {
  float i;
  float r;
  float iConst;
  float rConst;
  ArrayList<float[]> track;
  C (float real, float imaginary) {
    i = imaginary;
    r = real;
    iConst = i;
    rConst = r;
    track = new ArrayList<float[]>();
  }
  void MIter() {
    float br = r;
    r = r * r - i * i + rConst;
    i = 2 * br * i + iConst;
    float[] comp = new float[2];
    comp[0] = r;
    comp[1] = i;
    track.add(comp) ;
   
  }
 
  boolean escaped() {
    if (r * r + i * i > 16) {
      return true;
    } else {
      return false;
    }
  }
}



* failed-buddha2.jpg (114.69 KB, 600x600 - viewed 64 times.)
« Last Edit: March 13, 2017, 09:09:08 PM by Iariak » Logged
Pages: [1]   Go Down
  Print  
 
Jump to:  

Related Topics
Subject Started by Replies Views Last post
a creative buddhabrot rendering Fractal Programs ker2x 14 6583 Last post May 19, 2010, 08:00:13 PM
by kram1032
Harmonic Ratios with Processing Programming Thunderwave 3 2147 Last post August 19, 2010, 09:28:56 AM
by jehovajah
a small CFD app in java/processing Fluid Dynamics, Turbulence & Weather Prediction ker2x 0 4657 Last post March 22, 2011, 11:58:52 PM
by ker2x
Central processing unit Mandelbulb3D Gallery Dermis 0 954 Last post March 24, 2013, 08:21:20 PM
by Dermis
Self-organized processing unit Images Showcase (Rate My Fractal) Chaos_Ink 1 1607 Last post January 07, 2014, 07:06:19 PM
by cKleinhuis

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines

Valid XHTML 1.0! Valid CSS! Dilber MC Theme by HarzeM
Page created in 0.167 seconds with 24 queries. (Pretty URLs adds 0.008s, 2q)