WIP
This commit is contained in:
parent
572116ba4a
commit
2948a9ea5d
7 changed files with 83 additions and 7 deletions
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
|
|
@ -0,0 +1,2 @@
|
||||||
|
mandelbrot
|
||||||
|
|
||||||
BIN
hsb
Executable file
BIN
hsb
Executable file
Binary file not shown.
47
hsb.nim
Normal file
47
hsb.nim
Normal file
|
|
@ -0,0 +1,47 @@
|
||||||
|
import math, colors
|
||||||
|
|
||||||
|
proc hsbToRgb*(h:float64, s:float64, v:float64): Color =
|
||||||
|
var
|
||||||
|
H = h
|
||||||
|
R = 0'f64
|
||||||
|
G = 0'f64
|
||||||
|
B = 0'f64
|
||||||
|
|
||||||
|
while H < 0'f64:
|
||||||
|
H = H + 360'f64
|
||||||
|
while H >= 360'f64:
|
||||||
|
H = H - 360'f64
|
||||||
|
|
||||||
|
if v <= 0'f64:
|
||||||
|
R = 0'f64
|
||||||
|
G = 0'f64
|
||||||
|
B = 0'f64
|
||||||
|
elif s <= 0'f64:
|
||||||
|
R = v
|
||||||
|
G = v
|
||||||
|
B = v
|
||||||
|
else:
|
||||||
|
let
|
||||||
|
hf = H / 60'f64
|
||||||
|
i = int(floor(hf))
|
||||||
|
f = hf - float64(i)
|
||||||
|
pv = v * (1'f64 - s)
|
||||||
|
qv = v * (1'f64 - s * f)
|
||||||
|
tv = v * (1'f64 - (1'f64 - f))
|
||||||
|
|
||||||
|
case i:
|
||||||
|
of 0:
|
||||||
|
R = v; G = tv; B = pv
|
||||||
|
of 1: R = qv; G = v; B = pv
|
||||||
|
of 2: R = pv; G = v; B = tv
|
||||||
|
of 3: R = pv; G = qv; B = v
|
||||||
|
of 4: R = tv; G = pv; B = v
|
||||||
|
of 5: R = v; G = pv; B = qv
|
||||||
|
of 6: R = v; G = tv; B = pv
|
||||||
|
of -1: R = v; G = pv; B = qv
|
||||||
|
else:
|
||||||
|
R = v; G = v; B = v
|
||||||
|
|
||||||
|
result = rgb(clamp(int(R*255), 0, 255),
|
||||||
|
clamp(int(G*255), 0, 255),
|
||||||
|
clamp(int(B*255), 0, 255))
|
||||||
BIN
main
Executable file
BIN
main
Executable file
Binary file not shown.
15
main.nim
Normal file
15
main.nim
Normal file
|
|
@ -0,0 +1,15 @@
|
||||||
|
import mandelbrot, colors
|
||||||
|
|
||||||
|
echo mandelbrot.generateImage(800, 600, 150'f64, 32, 570)[0][0]
|
||||||
|
|
||||||
|
import stb_image/write as stbiw
|
||||||
|
|
||||||
|
# Stuff some pixels
|
||||||
|
var data: seq[uint8] = @[]
|
||||||
|
data.add(0x00)
|
||||||
|
data.add(0x80)
|
||||||
|
data.add(0xFF)
|
||||||
|
|
||||||
|
# save it (as monochrome)
|
||||||
|
stbiw.writeBMP("three.bmp", 3, 1, stbiw.Y, data)
|
||||||
|
|
||||||
BIN
mandelbrot
BIN
mandelbrot
Binary file not shown.
|
|
@ -1,20 +1,27 @@
|
||||||
import math, complex, colors
|
import sequtils, math, complex, colors, hsb
|
||||||
|
|
||||||
proc getComplexValueForPixel(x:int, y:int, height:int, width:int, zoom:float64): Complex[float64] =
|
proc getComplexValueForPixel(x:int, y:int, height:int, width:int, zoom:float64): Complex[float64] =
|
||||||
complex.complex64((x.float64-width.float64/2.0'f64)/zoom, (y.float64-height.float64/2.0'f64)/zoom)
|
complex64((x.float64-width.float64/2.0'f64)/zoom, (y.float64-height.float64/2.0'f64)/zoom)
|
||||||
|
|
||||||
proc colorMap(i:int, nMax:int): Color =
|
proc colorMap(i:int, nMax:int): Color =
|
||||||
rgb(0, 0, 0)
|
let
|
||||||
|
h = if (float(i) / 100'f64) <= 1.0:
|
||||||
|
(float(i) / 100'f64) * 360'f64
|
||||||
|
else: 360'f64
|
||||||
|
s = 1'f64
|
||||||
|
v = if i < nMax: 1'f64 else: 0'f64
|
||||||
|
|
||||||
|
result = hsbToRgb(h, s, v)
|
||||||
|
|
||||||
proc generatePixel(x:int, y:int, height:int, width:int, zoom:float64,
|
proc generatePixel(x:int, y:int, height:int, width:int, zoom:float64,
|
||||||
rMax: int, maxIter:int) : Color =
|
rMax: int, maxIter:int) : Color =
|
||||||
let c = getComplexValueForPixel(x, y, height, width, zoom)
|
let c = getComplexValueForPixel(x, y, height, width, zoom)
|
||||||
|
|
||||||
var
|
var
|
||||||
n = 0
|
n = maxIter
|
||||||
z = complex64(0'f64, 0'f64)
|
z = complex64(0'f64, 0'f64)
|
||||||
|
|
||||||
for i in 0..maxIter:
|
for i in 0..maxIter-1:
|
||||||
if z.abs >= rMax.float64:
|
if z.abs >= rMax.float64:
|
||||||
n = i
|
n = i
|
||||||
break
|
break
|
||||||
|
|
@ -22,5 +29,10 @@ proc generatePixel(x:int, y:int, height:int, width:int, zoom:float64,
|
||||||
|
|
||||||
result = colorMap(n, maxIter)
|
result = colorMap(n, maxIter)
|
||||||
|
|
||||||
proc generateImage(width:int, height:int, zoom:float64, rMax:int, maxIter:int) =
|
proc generateImage*(width:int, height:int, zoom:float64, rMax:int, maxIter:int): seq[seq[Color]] =
|
||||||
discard nil
|
|
||||||
|
result = newSeqWith(height, newSeq[Color](width))
|
||||||
|
|
||||||
|
for y in 0..height-1:
|
||||||
|
for x in 0..width-1:
|
||||||
|
result[y][x] = generatePixel(x, y, height, width, zoom, rMax, maxIter)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue