Components and supplies
Arduino UNO
Apps and platforms
Arduino IDE
Project description
Code
fractal mandelbrot with arduino
c_cpp
1//mandelbrot06.ino 2// http://rosettacode.org/wiki/Mandelbrot_set 3// https://www.khanacademy.org/computer-programming/mandelbrot-set/1054272371 4// https://github.com/cslarsen/mandelbrot-js 5// http://falcosoft.hu/html5_mandelbrot.html 6// 7int pinOnBoardLED = 13; 8long startTime = 0; 9 10void setup() { 11 Serial.begin(9600); 12 pinMode(pinOnBoardLED, OUTPUT); 13} 14 15void loop() { 16 myMandel(-2.000, 1.000, -1.000, 1.000, 99, 50, 140); 17 myMandel(-2.0262812499999998, -1.7157656249999997, 0.1242708333333333, -0.1088020833333334, 99, 50, 140); 18 myMandel(0.3086785098726753, 0.5163214901273253, -0.2804711524861787, -0.4345288475138219, 150, 50, 140); 19 myMandel(0.4329398558688174, 0.4498108480145078, -0.3687333735957660, -0.3812505613167620, 200, 50, 140); 20 myMandel(0.4357822854352487, 0.4359042777391612, -0.3780553779480240, -0.3781461752993666, 250, 50, 140); 21} 22 23void switchLED() { 24 digitalWrite(pinOnBoardLED, digitalRead(pinOnBoardLED) ^ 1); 25} 26 27void myMandel(double realMin, double realMax, double imagMin, double imagMax, int maxIterations, double ySize, double xSize) { 28 // http://rosettacode.org/wiki/Mandelbrot_set#AWK 29 startTime = millis(); 30 String myString = "\ 31\ 32\ 33\ 34realMin=" + String(realMin) + ", realMax=" + String(realMax) + ", imagMin=" + String(imagMin) + ", imagMax=" + String(imagMax) + ", maxIterations=" + String(maxIterations) + ", startTime=" + String((startTime / 1000.0)) + "s"; 35 Serial.print(myString); 36 //================================================================ 37 double xStep = (realMax - realMin) / xSize; 38 double yStep = (imagMax - imagMin) / ySize; 39 char* colorChar[] = {"#", "-", ":", "=", "o"}; 40 int colorCharLength = (sizeof(colorChar) / sizeof(colorChar[0])) - 1; 41 int color = 0; 42 for (int y = 0; y < ySize; y++) { 43 Serial.print("\ 44"); 45 switchLED(); 46 double imagY = imagMin + yStep * y; 47 for (int x = 0; x < xSize; x++) { 48 double realX = realMin + xStep * x; 49 double realZ = realX; 50 double imagZ = imagY; 51 for (int n = 0; n <= maxIterations; n++) { 52 color = n; 53 double a = realZ * realZ; 54 double b = imagZ * imagZ; 55 if (a + b > 4.0) break; 56 imagZ = 2 * realZ * imagZ + imagY; 57 realZ = a - b + realX; 58 } 59 (color == maxIterations ) ? color = 0 : color = color % colorCharLength + 1; 60 Serial.print(colorChar[color]); 61 } 62 } 63 //================================================================ 64 long elapseTime = millis() - startTime; 65 myString = "\ 66elapseTime=" + String((elapseTime / 1000.0)) + "s"; 67 Serial.print(myString); 68}
Comments
Only logged in users can leave comments
picass01
0 Followers
•0 Projects
Table of contents
Intro
2
0