Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mandelbrot #69

Merged
merged 1 commit into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions examples/mandelbrot/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
run:
moon build
wat2wasm target/build/main/main.wat --output=target/mandelbrot.wasm
npx serve ./

17 changes: 17 additions & 0 deletions examples/mandelbrot/REAME.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Mandelbrot
## How to Run
```
make run
```

Open `localhost::3000`

# Result
-0.7+0.0i@3.0769
![-0.7+0.0i@3.0769](./docs/-0.7+0.0i@3.0769.png)
-0.743030+0.126433i@0.016110
![-0.743030+0.126433i@0.016110](./docs/-0.743030+0.126433i@0.016110.png)
-0.74364085+0.13182733i@0.00012068
![-0.74364085+0.13182733i@0.00012068](./docs/-0.74364085+0.13182733i@0.00012068.png)
-0.743643135+0.131825963i@0.000014628
![-0.743643135+0.131825963i@0.000014628](./docs/-0.743643135+0.131825963i@0.000014628.png)
Binary file added examples/mandelbrot/docs/-0.7+0.0i@3.0769.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
73 changes: 73 additions & 0 deletions examples/mandelbrot/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<html>
<body>
<canvas id="canvas"></canvas>
<input type="number" id="x" value="-0.7"/>
<input type="number" id="y" value="0.0"/>
<input type="number" id="width" value="3.0769"/>
<button type="button" id="button">calculate</button>
</body>
<script>
const spectest = {
imagedata: {
set: (img, x, rgb) => set_image_data(imagedata, x, rgb)
},
math: {
log: (x) => Math.log(x),
floor: (x) => Math.floor(x),
float_of_int: (x) => x,
},
spectest: {
print_i32: (x) => console.log(String(x)),
print_f64: (x) => console.log(String(x)),
print_char: (x) => console.log(String.fromCharCode(x)),
},
};

function set_image_data(imagedata, x, rgb) {
imagedata.data[x] = rgb;
}

let canvas = document.getElementById("canvas");
let ox = document.getElementById("x");
let oy = document.getElementById("y");
let width = document.getElementById("width");
let button = document.getElementById("button");
var context = canvas.getContext("2d");
var IMAGEWIDTH = 800;
var IMAGEHEIGHT = 600;
canvas.width = IMAGEWIDTH;
canvas.height = IMAGEHEIGHT;
var imagedata = context.createImageData(IMAGEWIDTH, IMAGEHEIGHT);

WebAssembly.instantiateStreaming(fetch("target/mandelbrot.wasm"), spectest).then(
(obj) => {
obj.instance.exports._start();
const calcColor = obj.instance.exports["mandelbrot/lib::calc_color"];
const drawColor = obj.instance.exports["mandelbrot/lib::draw_color"];

function saveImage() {
context.putImageData(imagedata, 0, 0);
}

function generateImage() {
for (row = 0; row < IMAGEHEIGHT; row++) {
for (col = 0; col < IMAGEWIDTH; col++) {
let x = +ox.value;
let y = +oy.value;
let w = +width.value;
var color = calcColor(col, row, x, y, w);
drawColor(imagedata, col, row, color);
}
}

saveImage();
}

generateImage();
button.onclick = function() {
generateImage();
}
}
)
</script>
</html>
132 changes: 132 additions & 0 deletions examples/mandelbrot/lib/mandelbrot.mbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
func log(x : Float64) -> Float64 = "math" "log"

func floor(x : Float64) -> Int = "math" "floor"

func float_of_int(x : Int) -> Float64 = "math" "float_of_int"

type ImageData

func set_image_data(self : ImageData, x : Int, rgb : Int) = "imagedata" "set"

let escape_radius = 4.0

let max_iter_number = 1000

// -0.743030 + 0.126433i @ 0.016110 /0.75
// -0.74364085 + 0.13182733i @ 0.00012068 /.75
// -0.7 + 0.0i @ 3.0769 / 0.75

let ratio = 0.75

let image_width = 800.0

let image_height : Float64 = image_width * ratio // 600

let coffset : Float64 = if floor(image_width) % 2 == 0 {
image_width / 2.0 - 0.5
} else {
image_width / 2.0
}

let roffset : Float64 = if floor(image_height) % 2 == 0 {
image_height / 2.0 - 0.5
} else {
image_height / 2.0
}

pub func iter(cx : Float64, cy : Float64) -> Float64 {
var x = 0.0
var y = 0.0
var newx = 0.0
var newy = 0.0
var smodz = 0.0
var i = 0
while i < max_iter_number {
newx = x * x - y * y + cx
newy = 2.0 * x * y + cy
x = newx
y = newy
i = i + 1
smodz = x * x + y * y
if smodz >= escape_radius {
return float_of_int(i) + 1.0 - log(log(smodz) * 0.5) / log(2.0)
}
}
return -1.0
}

func interpolation(f : Float64, c0 : Int, c1 : Int) -> Int {
let r0 = c0.asr(16).land(0xFF)
let g0 = c0.asr(8).land(0xFF)
let b0 = c0.land(0xFF)
let r1 = c1.asr(16).land(0xFF)
let g1 = c1.asr(8).land(0xFF)
let b1 = c1.land(0xFF)
let r = floor((1.0 - f) * float_of_int(r0) + f * float_of_int(r1) + 0.5)
let g = floor((1.0 - f) * float_of_int(g0) + f * float_of_int(g1) + 0.5)
let b = floor((1.0 - f) * float_of_int(b0) + f * float_of_int(b1) + 0.5)
return r.lsl(16).lor(g.lsl(8).lor(b))
}

pub func get_color(d : Float64) -> Int {
if d >= 0.0 {
var k = 0.021 * (d - 1.0 + log(log(128.0)) / log(2.0))
k = log(1.0 + k) - 29.0 / 400.0
k = k - float_of_int(floor(k))
k = k * 400.0
if k < 63.0 {
return interpolation(k / 63.0, 0x000764, 0x206BCB)
} else if k < 167.0 {
return interpolation((k - 63.0) / (167.0 - 63.0), 0x206BCB, 0xEDFFFF)
} else if k < 256.0 {
return interpolation((k - 167.0) / (256.0 - 167.0), 0xEDFFFF, 0xFFAA00)
} else if k < 342.0 {
return interpolation((k - 256.0) / (342.0 - 256.0), 0xFFAA00, 0x310230)
} else {
return interpolation((k - 342.0) / (400.0 - 342.0), 0x310230, 0x000764)
}
} else {
return 0x000000
}
}

pub func calc_color(col : Int, row : Int, ox : Float64, oy : Float64,
width : Float64) -> Int {
let pixel_size = width / image_width
let cx = (float_of_int(col) - coffset) * pixel_size + ox
let cy = (float_of_int(row) - roffset) * pixel_size + oy
var r = 0
var g = 0
var b = 0
var i = -1
while i <= 1 {
var j = -1
while j <= 1 {
let d = iter(
cx + float_of_int(i) * pixel_size / 3.0,
cy + float_of_int(j) * pixel_size / 3.0,
)
let c = get_color(d)
r = r + c.asr(16).land(0xFF)
g = g + c.asr(8).land(0xFF)
b = b + c.land(0xFF)
j = j + 1
}
i = i + 1
}
r = r / 9
g = g / 9
b = b / 9
return r.lsl(16).lor(g.lsl(8)).lor(b)
}

pub func draw_color(image_data : ImageData, col : Int, row : Int, rgb : Int) {
let pindex = ((image_height - float_of_int(row) - 1.0) * image_width +
float_of_int(col)) * 4.0
let pindex = floor(pindex)
set_image_data(image_data, pindex, rgb.lsr(16).land(0xFF))
set_image_data(image_data, pindex + 1, rgb.lsr(8).land(0xFF))
set_image_data(image_data, pindex + 2, rgb.land(0xFF))
set_image_data(image_data, pindex + 3, 0xFF)
}

1 change: 1 addition & 0 deletions examples/mandelbrot/lib/moon.pkg
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package lib
4 changes: 4 additions & 0 deletions examples/mandelbrot/main/main.mbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
func init {
()
}

3 changes: 3 additions & 0 deletions examples/mandelbrot/main/moon.pkg
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package main

import "mandelbrot/lib"
1 change: 1 addition & 0 deletions examples/mandelbrot/moon.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module "mandelbrot"