Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import std.stdio;
import std.math;
import core.time;
import core.time;
real myFmax(real a, real b) {
if (a > b) return a;
else return b;
}
// --ffast-math --fp-contract=fast
struct Complex {
final:
real x;
real y;
this(A)(A px, A py) {
this.x = px;
this.y = py;
}
unittest {
auto complex = Complex(2, 2);
assert(complex.x == 2 && complex.y == 2);
}
auto abs() const {
return myFmax(this.x * this.x, this.y * this.y);
}
void add(T)(const T other) {
this.x += other.x;
this.y += other.y;
}
void mul(T)(const T other) {
auto newX = this.x * other.x - this.y * other.y;
auto newY = this.x * other.y + this.y * other.x;
this.x = newX;
this.y = newY;
}
}
unittest {
auto c = new Complex(5, 3);
c.mul(new Complex(4, 2));
assert(c.x == 14 && c.y == 22);
}
unittest {
auto org = new Complex(0, 0);
org.add(new Complex(3, 3));
assert(org.x == 3 && org.y == 3);
}
auto iterate_mandelbrot(const Complex c, const int maxIters) {
auto z = Complex(0, 0);
for (int i = 0; i < maxIters; i++) {
if (z.abs() >= 2.0) {
return i;
}
z.mul(z);
z.add(c);
}
return maxIters;
}
enum x0 = -2.5, x1 = 1, y0 = -1, y1 = 1;
enum cols = 72, rows = 24;
enum maxIters = 1000000;
void main() {
auto now = MonoTime.currTime;
for (int row = 0; row < rows; row++) {
const y = (cast(real)row / rows) * (y1 - y0) + y0;
char[] str;
for (int col = 0; col < cols; col++) {
// real is needed here because otherwise "/" does integer division
const x = (cast(real)col / cols) * (x1 - x0) + x0;
auto c = Complex(x, y);
auto iters = iterate_mandelbrot(c, maxIters);
if (iters == 0) {
str ~= '.';
} else if (iters == 1) {
str ~= '%';
} else if (iters == 2) {
str ~= '@';
} else if (iters == maxIters) {
str ~= ' ';
} else {
str ~= '#';
}
}
str.writeln;
}
writeln(MonoTime.currTime- now, " milliseconds");
}
class Complex {
constructor(x, y) {
this.x = x;
this.y = y;
}
abs() {
return Math.max(this.x * this.x, this.y * this.y);
//return Math.sqrt(this.x * this.x, this.y * this.y);
//return Math.hypot(this.x, this.y);
}
add(other) {
this.x += other.x;
this.y += other.y;
}
mul(other) {
let newX = this.x * other.x - this.y * other.y;
let newY = this.x * other.y + this.y * other.x;
this.x = newX;
this.y = newY;
}
}
//Object.freeze(Complex);
// z(n+1) = z(n)^2 + c
function iterate_mandelbrot(c, maxIters) {
const z = new Complex(0, 0);
for (let i = 0; i < maxIters; i++) {
if (z.abs() >= 2) {
return i;
}
z.mul(z);
z.add(c);
}
return maxIters;
}
console.log(iterate_mandelbrot(new Complex(1, 1), 50))
const start = Date.now();
const x0 = -2.5, x1 = 1, y0 = -1, y1 = 1;
const cols = 72, rows = 24;
const maxIters = 1000000;
for (let row = 0; row < rows; row++) {
const y = (row / rows) * (y1 - y0) + y0;
let str = '';
for (let col = 0; col < cols; col++) {
const x = (col / cols) * (x1 - x0) + x0;
const c = new Complex(x, y);
const iters = iterate_mandelbrot(c, maxIters);
if (iters === 0) {
str += '.';
} else if (iters === 1) {
str += '%';
} else if (iters === 2) {
str += '@';
} else if (iters === maxIters) {
str += ' ';
} else {
str += '#';
}
}
console.log(str);
}
let end = Date.now() - start;
console.log(end, 'milliseconds runtime');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment