nac3_sca/nac3standalone/mandelbrot.py

65 lines
1.3 KiB
Python
Raw Normal View History

2021-09-19 17:50:01 +08:00
def y_scale(maxX: float, minX: float, height: float, width: float, aspectRatio: float) -> float:
return (maxX-minX)*(height/width)*aspectRatio
def check_smaller_than_sixteen(i: int32) -> bool:
return i < 16
def rec(x: int32):
if x > 1:
output(x)
rec(x - 1)
return
else:
output(-1)
return
def fib(n: int32) -> int32:
if n <= 2:
return 1
else:
return fib(n - 1) + fib(n - 2)
def draw():
2020-03-30 22:47:45 +08:00
minX = -2.0
maxX = 1.0
2021-08-19 15:30:52 +08:00
width = 78.0
height = 36.0
2020-03-30 22:47:45 +08:00
aspectRatio = 2.0
2021-09-19 17:50:01 +08:00
# test = 1.0 + 1
2021-09-19 17:50:01 +08:00
yScale = y_scale(maxX, minX, height, width, aspectRatio)
2020-03-30 22:47:45 +08:00
2021-08-19 15:30:52 +08:00
y = 0.0
2020-03-30 22:47:45 +08:00
while y < height:
2021-08-19 15:30:52 +08:00
x = 0.0
2020-03-30 22:47:45 +08:00
while x < width:
2021-08-19 15:30:52 +08:00
c_r = minX+x*(maxX-minX)/width
c_i = y*yScale/height-yScale/2.0
2020-03-30 22:47:45 +08:00
z_r = c_r
z_i = c_i
i = 0
2021-09-19 17:50:01 +08:00
while check_smaller_than_sixteen(i):
2020-03-30 22:47:45 +08:00
if z_r*z_r + z_i*z_i > 4.0:
break
new_z_r = (z_r*z_r)-(z_i*z_i) + c_r
z_i = 2.0*z_r*z_i + c_i
z_r = new_z_r
i = i + 1
output(i)
2021-08-19 15:30:52 +08:00
x = x + 1.0
2020-03-30 22:47:45 +08:00
output(-1)
2021-08-19 15:30:52 +08:00
y = y + 1.0
2021-09-19 17:50:01 +08:00
return
def run() -> int32:
rec(5)
output(fib(10))
output(-1)
draw()
2020-03-30 22:47:45 +08:00
return 0
2021-08-19 15:30:52 +08:00