TGC Codebase Backup



rotating an array by malcolm

3rd Jan 2009 8:02
Summary

take an array and rotate the contents



Description



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    rem rotate a array 
rem was recoded by malcolm bamber
rem taken from c source on the internet and rewritten in dark basic 

ink rgb(255,255,255),9
dim dst(24,22)
dim src(24,22)
xmax=21
ymax=21

global debug=0     `1= on

rem fill array with data
restore mydata
for y=1 to ymax
    for x=1 to xmax
        if debug=0
            read a
            dst(x,y)=a
            src(x,y)=a
        else
            dst(x,y)=64+y
            src(x,y)=64+y        
        endif
    next x
next y

mydata:
data 1,1,0,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,0,1,1      
data 1,0,0,0,0,1,1,0,0,0,0,0,0,0,1,1,0,0,0,0,1      
data 0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0
data 0,0,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,0,0
data 0,0,1,0,0,0,1,0,1,0,0,0,1,0,1,0,0,0,1,0,0
data 0,0,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,0,0
data 0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0
data 0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0
data 0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0
data 0,1,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,1,0
data 0,1,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,1,0
data 0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0
data 0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0
data 0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,1,0,0
data 0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,1,0,0
data 0,0,0,1,0,0,1,0,0,0,0,0,0,0,1,0,0,1,0,0,0
data 0,0,0,1,0,0,0,1,1,1,1,1,1,1,0,0,0,1,0,0,0
data 0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0
data 0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0         
data 1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1      
data 1,1,0,0,0,0,1,1,1,1,1,1,1,1,1,0,0,0,0,1,1  
    
sync on           
            cls 0
            `rotatearryleft(xmax,ymax)
            `dsttosrc(xmax,ymax)
            `rotatearryleft(xmax,ymax)
            `dsttosrc(xmax,ymax)
            `rotatearryleft(xmax,ymax)

            rotatearryright(xmax,ymax) 
            dsttosrc(xmax,ymax)
            rotatearryright(xmax,ymax)
            dsttosrc(xmax,ymax)
            rotatearryright(xmax,ymax)
            
            showsrcarray(xmax,ymax)
            showdstarray(xmax,ymax)
            sync:sync
            repeat:until mouseclick()<>0
            end
            
rem rotate antclockwise            
function rotatearryleft(xmax,ymax)
            rem add one becase data in array starts at 1 not zero
            inc xmax
            inc ymax
            
            for x=1 to xmax
                for y=1 to ymax
                        rem copy first row down to across bottom line
                        dst(y,ymax-x)=src(x,y)
                next y
            next x
endfunction

rem rotate clockwise            
function rotatearryright(xmax,ymax)
            rem add one becase data in array starts at 1 not zero
            inc xmax
            inc ymax
            for x=1 to xmax 
            for y=1 to ymax 
                    rem copy last row down to across bottom line
                    dst(xmax-y,x)=src(x,y)
            next y
            next x
endfunction

function dsttosrc(xmax,ymax)
            for y=1 to xmax
                for x=1 to ymax
                    src(x,y)=dst(x,y)
                next x
            next y
endfunction

function showdstarray(xmax,ymax)
            print "DIST"
            for y=1 to ymax
                for x=1 to xmax
                    rem line number
                    if x=1
                        a$=str$(y)+" "
                        rem add space before number
                        if y<10 then a$="0"+str$(y)+" "
                        print "",a$;
                    else
                        print " ";
                    endif
                    rem draw data in array
                    if dst(x,y)=0
                        rem print space
                        print chr$(32);
                    else
                        if debug=0
                            rem debug data
                            print "#";
                        else
                            rem debug data
                            print chr$(dst(x,y));
                        endif
                    endif
                next x
            print
            next y
            print
endfunction

function showsrcarray(xmax,ymax)
            print "SOURCE"
            for y=1 to ymax
                for x=1 to xmax
                    rem line number
                    if x=1
                        a$=str$(y)+" "
                        rem add space before number
                        if y<10 then a$="0"+str$(y)+" "
                        print "",a$;
                    else
                        print " ";
                    endif
                    rem draw data in array
                    if src(x,y)=0
                        rem print space
                        print chr$(32);
                    else
                        if debug=0
                            rem array data
                            print "#";
                        else
                            rem debug data
                            print chr$(src(x,y));
                        endif
                    endif
                next x
                print
            next y
            print
endfunction