-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathmandlebrot.lua
More file actions
95 lines (95 loc) · 2.03 KB
/
mandlebrot.lua
File metadata and controls
95 lines (95 loc) · 2.03 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
-- optimized mandlebrot renderer
-- q to quit
-- mouse to select zoom area
-- z to zoom out
local component=require("component")
local event=require("event")
local term=require("term")
local unicode=require("unicode")
local oci=require("oci")
local s=require("serialization")
local gpu=component.gpu
local oldw,oldh=gpu.getResolution()
local w,h=gpu.maxResolution()
local x0=-2
local y0=-1
local x1=0.5
local y1=1
local err,res=pcall(function()
gpu.setResolution(w,h)
local depth=32
local zoom={}
while true do
local dx=x1-x0
local dy=y1-y0
local palette={}
for l1=0,depth do
palette[l1]={
r=math.min(math.max(math.floor(l1/depth*1024),0),255),
g=math.min(math.max(math.floor((l1-depth/3)/depth*1024),0),255),
b=math.min(math.max(math.floor((l1-depth/3*2)/depth*1024),0),255),
}
end
palette[depth-1]={r=0,g=0,b=0}
local map={}
local mu={}
for y=0,h-1 do
map[y+1]={}
for x=0,w-1 do
local r=0
local n=0
local b=x/w*dx+x0
local e=y/h*dy+y0
local i=0
while i<depth-1 and r*r<4 do
local d=r
r=r*r-n*n+b
n=2*d*n+e
i=i+1
end
local p=palette[i]
local c=(((p.r*256)+p.g)*256)+p.b
mu[c]=(mu[c] or 0)+1
map[y+1][x+1]=c
end
end
local dat=oci.encode(map,"yx24","oci")
oci.render(dat)
local ax
local ay
local bx
local by
while true do
local p={event.pull()}
if p[1]=="touch" then
ax=p[3]
ay=p[4]
elseif p[1]=="drag" then
bx=p[3]
by=p[4]
elseif p[1]=="key_down" then
if p[4]==44 and zoom[1] then
x0,x1,y0,y1=table.unpack(zoom[1])
table.remove(zoom,1)
break
elseif p[4]==28 and ax and bx then
table.insert(zoom,1,{x0,x1,y0,y1})
x0=(((ax-1)/(w-1))*(x1-x0))+x0
x1=(((bx-1)/(w-1))*(x1-x0))+x0
y0=(((ay-1)/(h-1))*(y1-y0))+y0
y1=(((by-1)/(h-1))*(y1-y0))+y0
break
elseif p[4]==16 then
os.exit()
end
end
end
end
end)
gpu.setBackground(0x000000)
term.clear()
gpu.setResolution(oldw,oldh)
print("zoom:",x0,y0,x1,y1)
if not err and type(res)~="table" then
print(res)
end