option explicit
const OUTFOLDER="Frames"
const PI=3.14159265358979
dim x,y,t,xx,yy,tmpX,tmpY,oPic,oFS,filename,clip
dim StartFrame,EndFrame,FrameWidth,FrameHeight
dim xL,xR,yT,yB,xW,yH,r,g,b
dim offsetx,offsety,FrameCount,theta
set oFS=createobject("scripting.filesystemobject")
set oPic=createObject("ASPPicture.Picture")

drawFrames
cleanup

sub drawFrames()
    clip=4
    select case Clip
    case 1
        setValues 0,64,256,256,-3*pi,3*pi,-3*pi,3*pi
        offsetx=0
        offsety=0
    case 2
        setValues 0,50,256,256,-4*pi,4*pi,-4*pi,4*pi
        offsetx=0
        offsety=0
    case 3
        setValues 0,50,256,256,-5*pi,5*pi,-5*pi,5*pi
        offsetx=0
        offsety=0
    case 4
        setValues 0,50,256,256,-4*pi,4*pi,-4*pi,4*pi
        offsetx=0
        offsety=0
    end select

    if not oFS.FolderExists(OUTFOLDER) then oFS.CreateFolder OUTFOLDER
    if not oFS.FolderExists(OUTFOLDER & "\" & setDigits(Clip,3)) then oFS.CreateFolder OUTFOLDER & "\" & setDigits(Clip,3) 

    FrameCount=EndFrame-StartFrame
    for t=StartFrame to EndFrame
        oPic.Init FrameWidth,FrameHeight
        for x=0 to FrameWidth-1
            for y=0 to FrameHeight-1
                theta=4*PI*t/FrameCount
                tmpX=(x-128)*cos(theta) + (y-128)*sin(theta)
                tmpY=(x-128)*-sin(theta) + (y-128)*cos(theta)
                xx=xL+((tmpX+128)/FrameWidth)*xW
                yy=yT-((tmpY+128)/FrameHeight)*yH
                g=cInt(16+(64*sin(xx^2-yy^2))*(.75+abs(sin(pi/3+pi*t/FrameCount)/4)))
                r=cInt(16+(64*sin(yy^2-xx^2))*(.75+abs(sin(2*pi/3+pi*t/FrameCount)/4)))+g/2
                b=cInt(16+(64*cos(abs(sin(xx)*pi-sin(yy)*pi)))*abs(sin(pi*t/FrameCount)))
                if r<0 or r>255 then r=0
                if g<0 or g>255 then g=0
                if b<0 or b>255 then b=0
                
                oPic.PutPoint x, y, RGB(r,g,b)
            next
        next
        oPic.SaveFile OUTFOLDER & "\" & setDigits(Clip,3) & "\" & setDigits(t,4) & ".png"
    next
end sub

sub calcRGBx(byRef r, byRef g, byRef b,byVal pClip)
    if pClip=1 then
                theta=4*PI*t/FrameCount
                tmpX=(x-128)*cos(theta) - (y-128)*sin(theta)
                tmpY=(x-128)*sin(theta) + (y-128)*cos(theta)
                xx=xL+((tmpX+128)/FrameWidth)*xW
                yy=yT-((tmpY+128)/FrameHeight)*yH
                b=48*sin(xx^2)
                g=48*sin(yy^2)
                r=48*cos(sin(xx)*pi-sin(yy)*pi)+48
                if r<0 or r>255 then r=0
                if g<0 or g>255 then g=0
                if b<0 or b>255 then b=0
    elseif pClip=2 then
                theta=4*PI*t/FrameCount
                tmpX=(x-128)*cos(theta) + (y-128)*sin(theta)
                tmpY=(x-128)*-sin(theta) + (y-128)*cos(theta)
                xx=xL+((tmpX+128)/FrameWidth)*xW
                yy=yT-((tmpY+128)/FrameHeight)*yH
                b=cInt(16+(64*sin(yy^2-xx^2))*(.75+abs(sin(2*pi/3+pi*t/FrameCount)/4)))
                g=cInt(16+(64*sin(xx^2-yy^2))*(.75+abs(sin(pi/3+pi*t/FrameCount)/4)))
                r=cInt(16+(64*cos(abs(sin(xx)*pi-sin(yy)*pi)))*abs(sin(pi*t/FrameCount)))
                if r<0 or r>255 then r=0
                if g<0 or g>255 then g=0
                if b<0 or b>255 then b=0
    elseif pClip=3 then
                theta=4*PI*t/FrameCount
                tmpX=(x-128)*cos(theta) + (y-128)*sin(theta)
                tmpY=(x-128)*-sin(theta) + (y-128)*cos(theta)
                xx=xL+((tmpX+128)/FrameWidth)*xW
                yy=yT-((tmpY+128)/FrameHeight)*yH
                r=cInt((64*sin(xx^2-yy^2)+24)*(.75+abs(sin(pi/3+2*pi*t/FrameCount)/4)))
                g=cInt((64*sin(yy^2-xx^2)+36)*(.75+abs(sin(2*pi/3+2*pi*t/FrameCount)/4)))
                r=0
                if r>=g then 
                    b=r-g+10
                elseif g>r then 
                    b=g-r+10
                else 
                    b=0
                end if
                if r<0 or r>255 then r=0
                if g<0 or g>255 then g=0
                if b<0 or b>255 then b=0
    elseif pClip=4 then
                xx=xL+(x/FrameWidth)*xW
                yy=yT-(y/FrameHeight)*yH
                g=abs(sin(PI*(t+1)/(FrameCount+2)))
                b=abs((sin((yy-sin(xx^2))+0)))
                r=abs((cos((xx-cos(yy^2))+0)))
                g=cInt( (((g)*(r+b)*96)) mod 255)
                r=cInt(r*32)
                b=cInt(b*192)
    end if
end sub

sub cleanup()
    set oFS=nothing
    set oPic=nothing
end sub

sub setValues(start_frame,end_frame,frame_width,frame_height,left,right,bottom,top)
    StartFrame=start_frame
    EndFrame=end_frame
    FrameWidth=frame_width
    FrameHeight=frame_height
    xL=left
    xR=right
    yB=bottom
    yT=top
    xW=xR-xL
    yH=yT-yB
end sub

function setDigits(pVal,pDigits)
    dim strVal
    strVal=pVal
    setDigits=string(pDigits-len(strVal),"0") & strVal
end function
