Last active
August 28, 2021 00:26
-
-
Save keymon/47a67d32e926a0a559aa4a1cfac5c97d to your computer and use it in GitHub Desktop.
Compute the Mandelbrot set using jq.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
jq -r -n --argjson limit 150 --argjson size_x 140 --argjson size_y 400 ' | |
def add_i($a;$b): [$a[0]+$b[0],$a[1]+$b[1]]; | |
def mult_i($a;$b): [$a[0]*$b[0]-$a[1]*$b[1], $a[0]*$b[1]+$b[0]*$a[1]]; | |
def norm($a): $a[0]*$a[0]+$a[1]*$a[1] | sqrt; | |
def compute_c: [-2.0+.[0]*2.5/$size_x+0.8, 1.15-.[1]*2.3/$size_y*1.2+0.22]; | |
def compute_z($iter;$c): | |
if ($iter == 0) or (norm(.) >= 4) then $iter | |
else | |
add_i(mult_i(.;.); $c//[0,0]) | compute_z($iter-1; $c//.) | |
end | |
; | |
def get_letter: | |
(" 123456789abcdefghijklmnopqrstuvwxyz" | split("")) as $letters | | |
$letters[.%($letters|length)] | |
; | |
def compute_letter: | |
compute_c | compute_z($limit;null) | get_letter | |
; | |
def to_text: | |
map(.|join(""))|join("\n") | |
; | |
def generate_coordinates: | |
[ range($size_x) as $x | range($size_y) as $y | [$x,$y] ] | group_by(.[0]) | |
; | |
generate_coordinates | map(map(compute_letter)) | to_text | |
' |
Author
keymon
commented
Aug 28, 2021
I don't know why it mirror on the X axis :-/
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment