Skip to content

Instantly share code, notes, and snippets.

@bit-hack
Last active September 25, 2018 10:30
Show Gist options
  • Save bit-hack/138f0bae88e24f4f93ece2d269aa1faa to your computer and use it in GitHub Desktop.
Save bit-hack/138f0bae88e24f4f93ece2d269aa1faa to your computer and use it in GitHub Desktop.
Mandelbrot set
// from http://blog.nothinguntoward.eu/?p=38
#include <cstdio>
#include <cmath>
using namespace std;
int main() {
const int width = 80;
const int height = 80;
const int zoom = 20;
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
// scale the indicies to floats in the range -2..2
const float cr = float(j - (width / 2)) / float(zoom);
const float ci = float(i - (height / 2)) / float(zoom);
// initialise z to 0
float zr = 0;
float zi = 0;
for (int iter = 0; iter < 64; iter++) {
const float tr = zr * zr - zi * zi + cr;
const float ti = zr * zi + zi * zr + ci;
zr = tr;
zi = ti;
}
const float m = 2.f;
const float v = zr * zr + zi * zi;
printf("%s", (v < m*m) ? "1" : "0");
}
printf("\n");
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment