aUCBLogo Demos and Tests / pursuittest
to pursuittest ;for aUCBLogo
;Original by Mike Sandy (a bit extended with randoms and zoom)
pursuit 100 200
end
to pursuit n r
;n number of sides
;r distance from centre to vertex
local [pos_list ang d dmin i]
pos_list=[] ;COORDINATE LIST
ang=360/n
d=2*r*sin ang/2
clearScreen PenUp hideTurtle
forward :r
left 90+ang/2
repeat n ;DRAW POLYGON STORE COORDS. IN LIST
[ pos_list=fput pos pos_list
fd d lt ang
ang=random 360
setPos (list sin ang cos ang)*random r
]
dmin=0.1
i=0
catch "stopping
[ pursuitHelper 1
]
end
to zoom
local [p minx miny maxx maxy pmin pmax size newPosList]
minx=0 miny=0
maxx=0 maxy=0
foreach pos_list
[ p=?
if p.1 < minx [minx=p.1]
if p.2 < miny [miny=p.2]
if p.1 > maxx [maxx=p.1]
if p.2 > maxy [maxy=p.2]
]
pmin=list minx miny
pmax=list maxx maxy
size=pmax-pmin
if size.1==0 or2 size.2==0
[ throw "stopping
]
newPosList=pos_list
foreach pos_list
[ newPosList.repcount=((?-pmin)/size)*2*r-r
]
pos_list=newPosList
posn=((posn-pmin)/size)*2*r-r
clearScreen
setPos posn
end
to pursuitHelper d
;d IS THE DISTANCE TRAVELLED TOWARDS NEXT POSITION
; THE SMALLER d THE MORE ACCURATE THE PLOT
if dmin > Norm pos_list.(int n/2)-pos_list.1 [stop]
posn=first pos_list
seth towards posn
setPenColor HSB 360*i/(n-1) 1 1
pd forward d
PenUp ;ALTERNATIVELY COMMENT OUT THIS PenUp (BUT INCREASE d)
pos_list=lput Pos butFirst pos_list
setPos posn
i=i+1
if i > n
[ i=0
updateGraph
if Key?
[ c=readChar
if c==char 27 [throw "stopping]
zoom
]
]
pursuitHelper d
end