-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mandelbrot.zig
47 lines (40 loc) · 1.28 KB
/
mandelbrot.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
//! Compute Mandelbrot Set in Zig. Based on...
//! https://github.com/sleibrock/zigtoys/blob/main/toys/mandelbrot/mandelbrot.zig
const std = @import("std");
const io = std.io;
const math = std.math;
/// Import `print` Function from JavaScript
extern fn print(i32) void;
/// Width and Height should match HTML Canvas Size
const MAX_ITER: u8 = 255;
const WIDTH: f32 = 640.0;
const HEIGHT: f32 = 480.0;
/// Compute the Pixel Color at (px,py) for Mandelbrot Set
export fn get_pixel_color(px: i32, py: i32) u8 {
var iterations: u8 = 0;
var x0 = @intToFloat(f32, px);
var y0 = @intToFloat(f32, py);
x0 = ((x0 / WIDTH) * 2.51) - 1.67;
y0 = ((y0 / HEIGHT) * 2.24) - 1.12;
var x: f32 = 0;
var y: f32 = 0;
var tmp: f32 = 0;
var xsquare: f32 = 0;
var ysquare: f32 = 0;
while ((xsquare + ysquare < 4.0) and (iterations < MAX_ITER)) : (iterations += 1) {
tmp = xsquare - ysquare + x0;
y = 2 * x * y + y0;
x = tmp;
xsquare = x * x;
ysquare = y * y;
}
// Test printing to JavaScript Console.
// Warning: This is slow!
if (iterations == 1) {
print(iterations);
}
// smoothed valued
// return n + 1 - log(log2(abs(z)))
// figure out import math first to get log/log2/abs
return iterations;
}