Post by David Drake on Feb 25, 2020 8:42:57 GMT -5
I created the code below for the Rosetta Code task "Julia set" found here: rosettacode.org/wiki/Julia_set. The details are at the top of the page. This is mostly a recoding of the Perl 6 implementation, with a few bells and whistles added to make it a little more usable. I recommend that, when you run this, start with a larger "granularity" number (10 or so) because it runs much faster. I have found, at least on my computer, that a setting of 1 or 2 runs VERY slowly and eventually crashes (which is why I added error trapping).
I welcome the community's thoughts and corrections.
I welcome the community's thoughts and corrections.
nomainwin
[start]
on error goto [errorHandler]
prompt "Select granularity (1 [very fine] - 20 [very course])";gr
gr=abs(int(gr))
if gr<3 then
confirm "This will take a LONG time and might crash. Proceed?";ans$
if ans$="no" then [start]
end if
[setupWindow]
UpperLeftX=1
UpperLeftY=1
WindowWidth=900
WindowHeight=800
open "Julia Set" for graphics_nf_nsb as #1
#1 "trapclose [q];fill black;flush;down;color white;size ";gr
w = 850
h = 750
zoom = 1
maxiter = 255
moveX = 0
moveY = 0
cX = -0.7
cY = 0.27015
global zx, zy, tmp, i, rd, gn, bu
for x = 0 to w step gr
for y = 0 to h step gr
zx=1.5*(x-w/2)/(0.5*zoom*w)+moveX
zy=1.0*(y-h/2)/(0.5*zoom*h)+moveY
i=maxiter
while zx*zx+zy*zy<4 and i>1
tmp = zx * zx - zy * zy + cX
zy = 2.0 * zx * zy + cY
zx = tmp
i=i-1
call hsv2rgb i/maxiter*360,1,i
#1 "color ";rd;" ";gn;" ";bu
#1 "set ";x;" ";y
wend
next y
scan
next x
#1 "flush"
notice "Complete"
wait
[errorHandler]
print "Error number ";Err;" has occurred."
print "Error message: ";Err$
[q]
close #1
end
sub hsv2rgb h,s,v
c=v*s
x=c*(1-abs(((h/60))-1))
m=v-c
select case
case h < 60
r = c
g = x
b = 0
case h>=60 and h<120
r = x
g = c
b = 0
case h>=120 and h<180
r = 0
g = c
b = x
case h>=180 and h<240
r = 0
g = x
b = c
case h>=240 and h<300
r = x
g = 0
b = c
case h>=300
r = c
g = 0
b = x
end select
rd = abs(int((r+m)*255))
gn = abs(int((g + m)*255))
bu = abs(int((b + m)*255))
end sub