'By Fryer - Please Upload.
'vector square demo
'taken from balldemo but with fast sort (28 points)

setup()
repeat 
    setdispbuf ss
    j1=peek("port1")
    ss=1-ss
    setdrawbuf ss
    clear window

'motion control
    dist=mod(dist+3,360)
    zp=500+400*cs(dist)
    change=int(ran(60))
    if change=10 ma=max(min(int(ma+ran(3)-1),4),-4)
    if change=20 mb=max(min(int(mb+ran(3)-1),4),-4)
    if change=30 mc=max(min(int(mc+ran(3)-1),4),-4)
    a=mod(a+ma,360)
    b=mod(b+mb,360)
    c=mod(c+mc,360)

'morphing
    if change=5 and shapecount=0 and dist>180then
        shapecount=4
        lshape=shape
        shape=1+mod(shape,maxshape)
    end if
    if shapecount>0 then
        for i=1 to mx
        x(i)=(sx(i,shape)-sx(i,lshape))/100*shapecount+sx(i,lshape)
        y(i)=(sy(i,shape)-sy(i,lshape))/100*shapecount+sy(i,lshape)
        z(i)=(sz(i,shape)-sz(i,lshape))/100*shapecount+sz(i,lshape)
        next i
        shapecount=mod(shapecount+4,104)
    end if

'rotations
    csa=cs(a):sna=sn(a)
    csb=cs(b):snb=sn(b)
    csc=cs(c):snc=sn(c)
    sort(1)=0
    for i=1 to mx 
        tz=z(i)*csa+x(i)*sna
        tx=x(i)*csa-z(i)*sna
        rx(i)=tx*csb+y(i)*snb
        ty=y(i)*csb-tx*snb
        rz(i)=tz*csc-ty*snc
        ry(i)=ty*csc+tz*snc
        srz(i)=int(32-rz(i)*.12)
        sort(i)=i
    next i

'6-bit fast sort
  for i=1 to mx byte=and(7,srz(i))
    oc(byte)=oc(byte)+1 next i
  p=mx+1
  for i=0 to 7 c(i)=p
    p=p+oc(i)
    oc(i)=0 next i
  for i=1 to mx byte=and(7,srz(sort(i)))
    sc=c(byte)
    sort(sc)=sort(i)
    c(byte)=sc+1
    byte=srz(i)*0.125
    oc(byte)=oc(byte)+1 next i
  p=1
  for i=0 to 7 c(i)=p
    p=p+oc(i)
    oc(i)=0 next i
  p=mx+1
  en=mx+mx
  for i=p to en byte=srz(sort(i))*0.125
    sc=c(byte)
    sort(sc)=sort(i)
    c(byte)=sc+1 next i

'draw
    ra=mod(ra+8,360)
    for i=1 to mx
        sort=sort(i)
        ora=mod(ra+sort*25,360)
        centre=200/mx*sort
        br=1-.5*sn(ora)
        setrgb 1,100*br,0,90*br
        r=400/(rz(sort)+zp)
        x=rx(sort)*r
        y=ry(sort)*r
        r=(16+6*sn(ora))*r
        gtriangle x,y to x+r,y+r to x+r,y-r
        gtriangle x,y to x+r,y-r to x-r,y-r
        gtriangle x,y to x-r,y-r to x-r,y+r
        gtriangle x,y to x-r,y+r to x+r,y+r
    next i 

until (and(j1,8)>0)
end

sub setup()
    open window 640,512
    window origin"CC"
    setrgb 2,0,0,0
    setrgb 3,0,0,0
    dim sn(400),cs(400)
    for i=0 to 400
        sn(i)=sin(i/180*pi)
        cs(i)=cos(i/180*pi) 
    next i
    dim x(100),y(100),z(100),sort(100)
    dim c(15),oc(15),sort(200),srz(100)
    dim rx(100),ry(100),rz(100)
    dim sx(100,10),sy(100,10),sz(100,10)
    mx=28
    for i=1 to mx
        sx(i,1)=(i-mx/2)*300/mx
        sy(i,1)=50*sn(mod(int(i*60),360))
        sz(i,1)=50*cs(mod(int(i*60),360))
        sx(i,2)=100*cs(int(360/mx*i))
        sy(i,2)=100*sn(int(360/mx*i))
        sz(i,2)=0
        sx(i,3)=100*cs(int(360/mx*i))
        sy(i,3)=100*sn(int(360/mx*i))
        sz(i,3)=50*sn(mod(int(1080/mx*i),360))
        sx(i,4)=(i-mx/2)*500/mx
        sy(i,4)=0
        sz(i,4)=0
'        read sx(i,5),sy(i,5),sz(i,5)
        x(i)=sx(i,1)
        y(i)=sy(i,1)
        z(i)=sz(i,1)
    next i
for i=1 to mx sort(i)=i next i
    zp=500
    shape=1
    shapecount=0
    maxshape=4
    ma=0:mb=0:mc=0
    dist=0
end sub
data -40,-80,0
data -40,-40,0
data -40,0,0
data 0,0,0
data -40,40,0
data -40,80,0
data 0,80,0
data 40,80,0
data 80,-80,-80
data 80,-80,-40
data 80,-80,0
data 80,-80,40
data 80,-80,80
















