整理精简代码,抽出代码如下:
hilbert-curve {
init:
float x=0.0
float y=0.0
float r=0.0
float rmin=1e20
float u=0.0
float v=0.0
float msb=0.0
t=(0,0)
int iter=0
int nlast=0
int n=0
int fac3=0
int fac4=0
int power4=0
zh=(0,0)
zh0=(0,0)
;
; set up endpoints for the lines
;
float h=@fourcorners
float ooh=1/h
float ooomh=1/(1-h)
float hoomh=h/(1-h)
z0a=@enterexit*h
z0b=flip(@enterexit*h)
x=real(@centerll)*h
y=imag(@centerll)*h
z1=x+flip(y)
x=real(@centerul)*h
y=imag(@centerul)*(1-h)+h
z2=x+flip(y)
x=real(@centerur)*(1-h)+h
y=imag(@centerur)*(1-h)+h
z3=x+flip(y)
x=real(@centerlr)*(1-h)+h
y=imag(@centerlr)*h
z4=x+flip(y)
z5=1+flip(@enterexit*h)
loop:
final:
iter=0
zh0=#z*0.25+(0.5,0.5)
zh=zh0
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)
msb=(msb+nlast-1)/4
n=n*4+nlast-1
endwhile
if(n<0)
#solid=true
else
; distance to curve
;
; 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
;
; line from enter to lower left sub-square
;
t=(zh-z0)/(z1-z0)
x=real(t)
y=imag(t)
if(x<0.0)
r=sqr(x)+sqr(y)
elseif(x>1.0)
r=sqr(x-1.0)+sqr(y)
else
r=sqr(y)
endif
r=sqrt(r)*cabs(z1-z0)
if(r<rmin)
rmin=r
endif
;
; line from lower left to upper left sub-squares
;
t=(zh-z1)/(z2-z1)
x=real(t)
y=imag(t)
if(x<0.0)
r=sqr(x)+sqr(y)
elseif(x>1.0)
r=sqr(x-1.0)+sqr(y)
else
r=sqr(y)
endif
r=sqrt(r)*cabs(z2-z1)
if(r<rmin)
rmin=r
endif
;
; line from upper left to upper right sub-squares
;
t=(zh-z2)/(z3-z2)
x=real(t)
y=imag(t)
if(x<0.0)
r=sqr(x)+sqr(y)
elseif(x>1.0)
r=sqr(x-1.0)+sqr(y)
else
r=sqr(y)
endif
r=sqrt(r)*cabs(z3-z2)
if(r<rmin)
rmin=r
endif
;
; line from upper right to lower right sub-squares
;
t=(zh-z3)/(z4-z3)
x=real(t)
y=imag(t)
if(x<0.0)
r=sqr(x)+sqr(y)
elseif(x>1.0)
r=sqr(x-1.0)+sqr(y)
else
r=sqr(y)
endif
r=sqrt(r)*cabs(z4-z3)
if(r<rmin)
rmin=r
endif
;
; line from lower right sub-square to exit
;
t=(zh-z4)/(z5-z4)
x=real(t)
y=imag(t)
if(x<0.0)
r=sqr(x)+sqr(y)
elseif(x>1.0)
r=sqr(x-1.0)+sqr(y)
else
r=sqr(y)
endif
r=sqrt(r)*cabs(z5-z4)
if(r<rmin)
rmin=r
endif
;
#index=rmin
endif
default:
title="Hilbert curve"
param centerll
caption="lower left center"
default=(0.5,0.5)
hint="Center of the lower left sub-square. Make both coordinates \
between 0 & 1; use (0.5,0.5) for standard Hilbert curve."
endparam
param centerul
caption="upper left center"
default=(0.5,0.5)
hint="Center of the upper left sub-square. Make both coordinates \
between 0 & 1; use (0.5,0.5) for standard Hilbert curve."
endparam
param centerur
caption="upper right center"
default=(0.5,0.5)
hint="Center of the upper right sub-square. Make both coordinates \
between 0 & 1; use (0.5,0.5) for standard Hilbert curve."
endparam
param centerlr
caption="lower right center"
default=(0.5,0.5)
hint="Center of the lower right sub-square. Make both coordinates \
between 0 & 1; use (0.5,0.5) for standard Hilbert curve."
endparam
param enterexit
caption="enter/exit"
default=0.5
min=0.0
max=1.0
hint="Where the curve enters and exits the block of 4 sub-squares. \
Between 0 & 1; use 0.5 for standard Hilbert curve."
endparam
param fourcorners
caption="4 corners"
default=0.5
hint="Where the 4 corners meet. Between 0 & 1; use 0.5 for standard \
Hilbert curve."
endparam
param niter
caption="iterations"
default=0
min=0
endparam
}
这整理后的代码,我在UF验证没问题。代码很长,大家带出后,将文件帖在这。
效果图:
|