Created
February 2, 2021 13:10
-
-
Save aferust/3ddfb53c56e89cf1f5aace01fcac9798 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env dub | |
/+ dub.sdl: | |
dependency "arsd-official:simpledisplay" version="~>9.1.2" | |
+/ | |
// ported from https://www.geeksforgeeks.org/fractals-in-cc/ | |
import arsd.simpledisplay; | |
enum MAXCOUNT = 30; | |
SimpleWindow window; | |
void fractal(float left, float top, float xside, float yside) | |
{ | |
float xscale, yscale, zx, zy, cx, tempx, cy; | |
int maxx, maxy, count; | |
// getting maximum value of x-axis of screen | |
maxx = window.width; | |
// getting maximum value of y-axis of screen | |
maxy = window.height; | |
// setting up the xscale and yscale | |
xscale = xside / maxx; | |
yscale = yside / maxy; | |
// calling rectangle function | |
// where required image will be seen | |
auto painter = window.draw(); | |
painter.outlineColor = Color.red; | |
painter.drawRectangle(Point(0,0), maxx, maxy); | |
// scanning every point in that rectangular area. | |
// Each point represents a Complex number (x + yi). | |
// Iterate that complex number | |
Color clr = Color.red; | |
foreach (y ; 1..maxy) | |
{ | |
foreach (x; 1..maxx) | |
{ | |
// c_real | |
cx = x * xscale + left; | |
// c_imaginary | |
cy = y * yscale + top; | |
// z_real | |
zx = 0; | |
// z_imaginary | |
zy = 0; | |
count = 0; | |
// Calculate whether c(c_real + c_imaginary) belongs | |
// to the Mandelbrot set or not and draw a pixel | |
// at coordinates (x, y) accordingly | |
// If you reach the Maximum number of iterations | |
// and If the distance from the origin is | |
// greater than 2 exit the loop | |
while ((zx * zx + zy * zy < 4) && (count < MAXCOUNT)) | |
{ | |
// Calculate Mandelbrot function | |
// z = z*z + c where z is a complex number | |
// tempx = z_real*_real - z_imaginary*z_imaginary + c_real | |
tempx = zx * zx - zy * zy + cx; | |
// 2*z_real*z_imaginary + c_imaginary | |
zy = 2 * zx * zy + cy; | |
// Updating z_real = tempx | |
zx = tempx; | |
// Increment count | |
count = count + 1; | |
} | |
// To display the created fractal | |
painter.outlineColor = clr.setHue(360.0 / count); | |
painter.drawPixel(Point(x, y)); | |
} | |
} | |
} | |
void main() { | |
window = new SimpleWindow(500, 500, "Fractal"); | |
fractal(-1.75, -0.25, 0.25, 0.45); | |
window.windowResized = delegate(w, h) { | |
fractal(-1.75, -0.25, 0.25, 0.45); | |
}; | |
window.eventLoop(0); // handle events | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment