精解代码:
aug01-hilbert-spline2 {
init:
float x=0.0
float y=0.0
float r=0.0
float rmin=1e20
float u=0.0
float v=0.0
int iter=0
int nlast=0
int n=0
int fac3=0
int fac4=0
int power4=0
zh=(0,0)
;
; set up endpoints for the lines
;
float h=0.5
float ooh=1/h
float ooomh=1/(1-h)
float hoomh=h/(1-h)
z0a=@enterexit
z0b=flip(@enterexit)
z1=@centerll
z2=@centerul
z3=@centerur
z4=@centerlr
z5=1+flip(@enterexit)
loop:
final:
iter=0
zh=#z*0.25+(0.5,0.5)
while(iter<@niter)
iter=iter+1
x=real(zh)
y=imag(zh)
;
; lower left sub-square: shrink, flip horizontally, rotate by -90 degrees
;
if((x<h)&&(y<h))
nlast=3
u=ooh*y
v=ooh*x
;
; upper left sub-square: just shrink
;
elseif((x<h)&&(y>=h))
nlast=2
u=ooh*x
v=ooomh*y-hoomh
;
; upper right sub-square: shrink & flip horizontally
;
elseif((x>=h)&&(y>=h))
nlast=1
u=ooomh-ooomh*x
v=ooomh*y-hoomh
;
; lower right sub-square: shrink, rotate by 90 degrees
;
elseif((x>=h)&&(y<h))
nlast=4
u=ooh*y
v=ooomh-ooomh*x
else
nlast=-1
u=x
v=y
endif
zh=u+flip(v)
n=n*4+nlast-1
endwhile
;
; determine how to start line 0-1
;
z0=z0a
iter=0
fac3=2
fac4=1
power4=4
while(iter<@niter)
iter=iter+1
if(iter%2==1)
if(n%power4==fac3)
z0=z0b
endif
else
if(n%power4==fac3)
z0=z0a
endif
endif
power4=4*power4
fac4=4*fac4
fac3=fac3+2*fac4
endwhile
;
; spline from enter to lower left sub-square
;
zz0=z0
zz2=z1
zz1=(zz0+zz2)/2
c=zz0-zh
b=2*(zz1-zz0)
root1=-c/b
x=real(root1)
y=imag(root1)
if(x<0.0)
r=cabs(root1)
elseif(x>1.0)
r=cabs(root1-1)
else
r=abs(y)
endif
if(r<rmin)
rmin=r
endif
;
; spline from lower left to upper left sub-square
;
zz0=z1
zz2=z2
zz1=(zz0+zz2)/2
c=zz0-zh
b=2*(zz1-zz0)
root1=-c/b
x=real(root1)
y=imag(root1)
if(x<0.0)
r=cabs(root1)
elseif(x>1.0)
r=cabs(root1-1)
else
r=abs(y)
endif
if(r<rmin)
rmin=r
endif
;
; spline from upper left to upper right sub-square
;
zz0=z2
zz2=z3
zz1=(zz0+zz2)/2
c=zz0-zh
b=2*(zz1-zz0)
root1=-c/b
x=real(root1)
y=imag(root1)
if(x<0.0)
r=cabs(root1)
elseif(x>1.0)
r=cabs(root1-1)
else
r=abs(y)
endif
if(r<rmin)
rmin=r
endif
; spline from upper right to lower right sub-square
;
zz0=z3
zz2=z4
zz1=(zz0+zz2)/2
c=zz0-zh
b=2*(zz1-zz0)
root1=-c/b
x=real(root1)
y=imag(root1)
if(x<0.0)
r=cabs(root1)
elseif(x>1.0)
r=cabs(root1-1)
else
r=abs(y)
endif
if(r<rmin)
rmin=r
endif
; spline from lower right sub-square to exit
;
zz0=z4
zz2=z5
zz1=(zz0+zz2)/2
c=zz0-zh
b=2*(zz1-zz0)
root1=-c/b
x=real(root1)
y=imag(root1)
if(x<0.0)
r=cabs(root1)
elseif(x>1.0)
r=cabs(root1-1)
else
r=abs(y)
endif
if(r<rmin)
rmin=r
endif
;
#index=rmin
default:
title="Hilbert spline 2"
param niter
caption="iterations"
default=0
min=0
endparam
param centerll
caption="lower left center"
default=(0.25,0.25)
hint="Center of the lower left sub-square. Make both coordinates \
between 0 & 1; use (0.25,0.25) for standard Hilbert curve."
endparam
param centerul
caption="upper left center"
default=(0.25,0.75)
hint="Center of the upper left sub-square. Make both coordinates \
between 0 & 1; use (0.25,0.75) for standard Hilbert curve."
endparam
param centerur
caption="upper right center"
default=(0.75,0.75)
hint="Center of the upper right sub-square. Make both coordinates \
between 0 & 1; use (0.75,0.75) for standard Hilbert curve."
endparam
param centerlr
caption="lower right center"
default=(0.75,0.25)
hint="Center of the lower right sub-square. Make both coordinates \
between 0 & 1; use (0.75,0.25) for standard Hilbert curve."
endparam
param enterexit
caption="enter/exit"
default=0.25
min=0.0
max=1.0
hint="Where the curve enters and exits the block of 4 sub-squares. \
Between 0 & 1; use 0.25 for standard Hilbert curve."
endparam
} |