News:

Function Finder  Find all the functions within source code files

Main Menu

Rotate 2D Array

Started by kevin, May 21, 2012, 05:35:34 PM

Previous topic - Next topic

kevin

  Rotate 2D Array

   This example creates a 2D array and fills it will a set of colours in the shape pf the letter "B".  The code includes a render function to draw the array out as shadeboxes.  All of which, is just set up to demo Rotating the contents of the 2D array.   To rotate / turn our array we have a function that accepts that passes array, where we then create a local 2d array of Height BY Width.    All we do is copy from the source to the temp local flipping the Y access, and we get a Tetris styled rotation.



PlayBASIC Code: [Select]
   ; -------------------------------
; Read the Colour table
; -------------------------------
MaxColours=2
DIm Colours(MAxCOlours)
for lp =0 to MAxColours
Colours(lp)=readdata()
next


; --------------------------------
; Read the charcter table
; --------------------------------
restore rows

; Create our original Array
Width=8
Height=10
Dim Character(Width,Height)
For ylp=0 to Height

ThisRow$=Readdata$()
For xlp=0 to Width-1
Index = mid(ThisRow$,1+xlp)-asc("0")
Character(xlp,ylp)=Colours(Index)
next
next



; define the pixel width & Height
BlockWidth =40
BlockHeight =40

; -----------------------------------------------------------------------
; MAIN LOOP
; -----------------------------------------------------------------------

for lp =0 to 10
Cls 0

; Center this grid around the screen center
xpos=400-(GetArrayElements(Character(),1)*BlockWidth)/2
Ypos=300-(GetArrayElements(Character(),2)*BlockHeight)/2

; draw the grid at this point
DrawGrid(Character(),Xpos,Ypos,BlockWidth,BlockHeight)

; turn the array 90 degrees (clockwise)
TurnArray90Degrees(Character())

; show the frame
Sync

; wait one second
wait 1000
next

; program is complete, so we end
end




; -----------------------------------------------------------------------
; Draw Grid Function
; -----------------------------------------------------------------------


Function DrawGrid(Buffer(),Xpos,Ypos,BlockWidth,BlockHeight)

For ylp=0 to GetArrayElements(Buffer(),2)

y1=Ypos
y1+=Ylp*BlockHeight
Y2 =Y1+BlockHeight

x1=Xpos

For xlp=0 to GetArrayElements(Buffer(),1)
x2=X1+BlockWidth
c=BUffer(xlp,ylp)
ShadeBox x1,y1,x2,y2,C,c,0,0
x1=x2
next

next

EndFunction



; -----------------------------------------------------------------------
; Draw Grid Function
; -----------------------------------------------------------------------


Function TurnArray90Degrees(Buffer())

; get the width and height of the passes array
Width =GetArrayElements(Buffer(),1)
Height=GetArrayElements(Buffer(),2)


; create a local array that's Height BY Width
Dim TempArray(Height,Width)

; rotate
For ylp=0 to Height

; Flip the output y coord
ylp2=Height-Ylp
For xlp=0 to width

; Copy from the source array to the local temp array
TempArray(ylp2,xlp)=Buffer(xlp,ylp)
next
next

; copy the temp array back over the passes versionx
CopyArray TempArray(),Buffer()

EndFunction



PaletteColours:
Data 0,$ffffff,$00ff00



Rows:

Data "21111110"
Data "11111111"
Data "11000011"
Data "11000011"
Data "11000011"
Data "11111100"
Data "11111110"
Data "11000011"
Data "11000011"
Data "11111111"
Data "11111110"




ATLUS

kevin,, this is useful code, Thank You!