Scaling and Repeating Array Data

Blitz3D Forums/Blitz3D Beginners Area/Scaling and Repeating Array Data

_PJ_(Posted 2016) [#1]
Sorry for the vague title.

In theory, this should be a lot simpler and straightforward than it's proving to describe as well as for me to complete in practice.
I think I'm just getting too tired!

Okay, so I have an array of a known (and fortunately fixed) size.
For the sake of argument (and because it fits a nice example) let's say it's 12 x 4 2D array.

Just with a really simple (this is not accurate to the real world issue) example, let's say the array is populated merely with 0 or 1

So it may be

000011110000
000011110000
111100001111
111100001111


What I am trying to do is a "realy should be simple" algorithm to just take that data and shrink & repeat it a given number of times


for example, if the above was then squashed to repeat 4 x 2 times (hence my simple example values) the result would be:


010010010010
101101101101
010010010010
101101101101

I hope my explanation makes sense and is clear enough.

My current code which I don't believe is working as intended but may help provide some general starting point and some of the variables used:


PATTERN_BRICKS_W = "Horizontal" array size
PATTERN_BRICKS_H = "Vertical" array size


Function RepeatPattern()
	Local RepeatH=Rand(1,4)
	Local RepeatV=Rand(1,2)
	
	If ((RepeatV*RepeatH)=1) Then Return
	
	Local StepX=Int(Floor(PATTERN_BRICKS_W/RepeatH))
	Local StepY=Int(Floor(PATTERN_BRICKS_H/RepeatV))
	
	If (RepeatH>1) Then RepeatHorizontal(StepX,RepeatH)
	If (RepeatH>1) Then RepeatVertical(StepY,RepeatV)
	
End Function

Function RepeatHorizontal(StepX,RepeatH)
	Local X
	Local Y
	
	Local XX
	
	For Y=0 To PATTERN_BRICKS_H-1
		For X=0 To StepX-1
			For XX=1 To RepeatH
				TempBrickArray(X,Y)=TempBrickArray(XX*X,Y)
			Next
		Next
	Next
End Function

Function RepeatVertical(StepY,RepeatV)
	Local X
	Local Y
	
	Local YY
	
	For X=0 To PATTERN_BRICKS_W-1
		For Y=0 To StepY-1
			For YY=1 To RepeatV
				TempBrickArray(X,Y)=TempBrickArray(X,YY*Y)
			Next
		Next
	Next
End Function



TomToad(Posted 2016) [#2]
Read ArrayWidth ;width And height of the arrays
Read ArrayHeight

Local RepeatX = 4 ;The number of times the main array is copied into the new array
Local RepeatY = 2

Dim Array(ArrayWidth,ArrayHeight)
Dim NewArray(ArrayWidth,ArrayHeight)

For y = 0 To ArrayHeight - 1 ;We are using 0 based indexing
	For x = 0 To ArrayWidth - 1
		Read Array(x,y) ;fill the array
	Next
Next


For y = 0 To ArrayHeight - 1
	For x = 0 To ArrayWidth - 1
		PickX = x * RepeatX Mod ArrayWidth ;Calculate where in the source array we will get the data
		PickY = Y * RepeatY Mod ArrayHeight
		NewArray(x,y) = Array(PickX,PickY) ;Put it in the destination array
	Next
Next

;print the contents of the arrays
Print "Original Array"
For y = 0 To ArrayHeight - 1
	s$ = ""
	For x = 0 To ArrayWidth - 1
		s = s + array(x,y)
	Next
	Print s
Next

Print 
Print "New Array"
For y = 0 To ArrayHeight - 1
	s$ = ""
	For x = 0 To ArrayWidth - 1
		s = s + NewArray(x,y)
	Next
	Print s
Next

WaitKey()

Data 12,4
Data 0,0,0,0,1,1,1,1,0,0,0,0
Data 0,0,0,0,1,1,1,1,0,0,0,0
Data 1,1,1,1,0,0,0,0,1,1,1,1
Data 1,1,1,1,0,0,0,0,1,1,1,1



_PJ_(Posted 2016) [#3]
Thanks Tom Toad that works perfectly! I should have used a separate array all along. Now that I see your method, it's remarkably clear. Thank you again.