Skip to content

Instantly share code, notes, and snippets.

@ikr7
Created August 8, 2017 05:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ikr7/f904c28be41d31c308ce6c2741cb8e3e to your computer and use it in GitHub Desktop.
Save ikr7/f904c28be41d31c308ce6c2741cb8e3e to your computer and use it in GitHub Desktop.
import std.stdio;
import std.complex : complex, Complex, abs, arg;
import std.algorithm.comparison : clamp;
import std.format : format;
import conv = std.conv;
import math = std.math;
import IF = imageformats;
alias Image = IF.IFImage;
immutable int size = 128; // 画像サイズ
real cx = 0; // 描画域の中心X座標
immutable real cy = 0; // 描画域の中心Y座標
immutable real zoom = 1; // -2 <= Im(z),Re(z) <= 2 からどれだけズームしたか?
immutable real scale = 4.0 / size / zoom; // 1 ピクセルは座標平面においていくつか?
immutable real PI = 3.1415926535897932384626;
// immutable int max_iter = conv.to!int(math.sqrt(2.0 * math.sqrt(math.abs(1 - math.sqrt(5 * zoom)))) * 66.5);
immutable int max_iter = 64;
Complex!real exp (Complex!real z) {
return math.exp(z.re) * complex!real(math.cos(z.im), math.sin(z.im));
}
Complex!real cos (Complex!real z) {
return (exp(
complex!real(-z.im, z.re)
) + exp(
complex!real(z.im, -z.re)
)) / 2.0;
}
int mand (Complex!real z, Complex!real c, int n) {
if (n >= max_iter) {
return max_iter;
}
z = ((7.0 * z + 2.0) - exp(complex!real(-z.im, z.re) * PI) * (5.0 * z + 2.0)) / 4.0;
// z = ((7.0 * z + 2.0) - cos(z * PI) * (5.0 * z + 2.0)) / 4.0;
if (abs(z) >= 1000.0) {
return n;
}
if (abs(z) == 0.0) {
return 999;
}
return mand(z, c, n + 1);
}
void main () {
auto logmax = math.log(max_iter);
for (int i = 0; i < 100; i++) {
ubyte[] pixels;
pixels.length = size * size;
for (int y = 0; y < size; y++) {
real ry = (y - size / 2) * scale + cy;
for (int x = 0; x < size; x++) {
real rx = (x - size / 2) * scale + cx;
// Complex!real z = complex!real(0, 0);
Complex!real c = complex!real(rx, -ry);
int n = mand(c, c, 0);
if (n < max_iter) {
auto v = conv.to!ubyte(clamp(math.log(n) * 0xFF / logmax, 0, 0xFF));
pixels[y * size + x] = v;
}
}
}
IF.write_image(format("out/%03d.png", i), size, size, pixels, IF.ColFmt.Y);
writeln(format("out/%03d.png", i));
cx += 0.1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment