picass01
Published © GPL3+

Fractal Mandelbrot With Arduino

Fractal Mandelbrot with Arduino with serial output.

BeginnerShowcase (no instructions)3,360
Fractal Mandelbrot With Arduino

Things used in this project

Story

Read more

Code

fractal mandelbrot with arduino

C/C++
//mandelbrot06.ino
// http://rosettacode.org/wiki/Mandelbrot_set
// https://www.khanacademy.org/computer-programming/mandelbrot-set/1054272371
// https://github.com/cslarsen/mandelbrot-js
// http://falcosoft.hu/html5_mandelbrot.html
//
int pinOnBoardLED = 13;
long startTime = 0;

void setup() {
  Serial.begin(9600);
  pinMode(pinOnBoardLED, OUTPUT);
}

void loop() {
  myMandel(-2.000, 1.000, -1.000,  1.000, 99, 50, 140);
  myMandel(-2.0262812499999998, -1.7157656249999997, 0.1242708333333333, -0.1088020833333334, 99, 50, 140);
  myMandel(0.3086785098726753, 0.5163214901273253, -0.2804711524861787, -0.4345288475138219, 150, 50, 140);
  myMandel(0.4329398558688174, 0.4498108480145078, -0.3687333735957660, -0.3812505613167620, 200, 50, 140);
  myMandel(0.4357822854352487, 0.4359042777391612, -0.3780553779480240, -0.3781461752993666, 250, 50, 140);
}

void switchLED() {
  digitalWrite(pinOnBoardLED, digitalRead(pinOnBoardLED) ^ 1);
}

void myMandel(double realMin, double realMax, double imagMin, double imagMax, int maxIterations, double ySize, double xSize) {
  // http://rosettacode.org/wiki/Mandelbrot_set#AWK
  startTime = millis();
  String myString = "\n\n\n\nrealMin=" + String(realMin) + ", realMax=" + String(realMax) + ", imagMin=" + String(imagMin) + ", imagMax=" + String(imagMax) + ", maxIterations=" + String(maxIterations) + ", startTime=" + String((startTime / 1000.0)) + "s";
  Serial.print(myString);
  //================================================================
  double xStep = (realMax - realMin) / xSize;
  double yStep = (imagMax - imagMin) / ySize;
  char* colorChar[] = {"#", "-", ":", "=", "o"};
  int colorCharLength = (sizeof(colorChar) / sizeof(colorChar[0])) - 1;
  int color = 0;
  for (int y = 0; y < ySize; y++) {
    Serial.print("\n");
    switchLED();
    double imagY = imagMin + yStep * y;
    for (int x = 0; x < xSize; x++) {
      double realX = realMin + xStep * x;
      double realZ = realX;
      double imagZ = imagY;
      for (int n = 0; n <= maxIterations; n++) {
        color = n;
        double a = realZ * realZ;
        double b = imagZ * imagZ;
        if (a + b > 4.0) break;
        imagZ = 2 * realZ * imagZ + imagY;
        realZ = a - b + realX;
      }
      (color == maxIterations ) ? color = 0 : color = color % colorCharLength + 1;
      Serial.print(colorChar[color]);
    }
  }
  //================================================================
  long elapseTime = millis() - startTime;
  myString = "\nelapseTime=" + String((elapseTime / 1000.0)) + "s";
  Serial.print(myString);
}

Credits

picass01
0 projects • 1 follower
Contact

Comments

Please log in or sign up to comment.