Referring to a two dimensional array - rows and columns - arrays

I have a two dimensional array and I want to know how I can refer to the rows and columns in it. Do I use [row,column] or [column,row]? I also have some graphics. Do I calculate the (x,y) coordinate set of each graphic with (row*size,col*size) or with (col*size,row*size)?
The whole two dimensional array is the building instructions for the grid.
Each element in the array refers to a piece of the grid.
I know how to construct this grid and its pieces and I know how to access and manipulate the array's elements.
The problem is that when I construct the grid I have to calculate the x and y coordinate of each piece, but I just don't know if my variable curRow should be used for the x or y coordinate. It's similarly with the variable curCol.
My code is working, but it confuses me.
I think of it like the rows control the y coordinates and the columns control the x coordinates, because I just learned of the way matrices are referred to.
I ask, because it came to my mind that I am unsure of how to do this.
In the past I have used [row,column] to loop and (row*size,col*size) to position.
The code so far is:
function buildGrid(gridInfo:Array):Sprite {
var displaySprite:Sprite = new Sprite();
for(var curRow:uint=0;curRow<gridInfo.length;curRow++) {
for(var curCol:uint=0;curCol<gridInfo[curRow].length;curCol++) {
var infoRef:Object = gridInfo[curRow][curCol];//create reference for fast access
var pieceGraphic:Shape = new Shape();
pieceGraphic.graphics.beginFill(infoRef.fillColor);
pieceGraphic.graphics.lineStyle(infoRef.borderThickness,infoRef.theBorderColor);
pieceGraphic.graphics.drawRect(0,0,infoRef.sideLength,infoRef.sideLength);
pieceGraphic.graphics.endFill();
pieceGraphic.x = curRow*(infoRef.sideLength+infoRef.spaceX);//later use of graphic requires known x
pieceGraphic.y = curCol*(infoRef.sideLength+infoRef.spaceY);//later use of graphic requires known y
displaySprite.addChild(pieceGraphic);
}
}
return displaySprite;
}

It's correct to refer to a two dimensional array with [row,column], but it's easier with other variable names like curX and curY. It's important to be consistent of what the indexes mean throughout the project and what indexes are used. To access the first elements in the array create a loop for rows or for curX and for the elements in the second dimension create a loop for columns or for curY.
You can calculate coordinate sets (x,y) for graphics in a grid with (row*size,col*size) or (curX*size,curY*size), so your code is correct and doesn't need to be changed.
Remember that rows are horizontal and columns are vertical.

Related

Finding max (& min) among values of specific index in a series of arrays that are the values of a map

I have a data structure defined in the following way: An array of maps where each map has String keys and Arrays of Doubles as values. i.e. Array [Map [String, Array [Double]]]
The reasoning behind this structure is that each String key is an identifier of a highway, with the values representing the lats and longs of traffic sensors along the highway. I have multiple highways so the need for multiple maps. What I need to do now is find the maximum and minimum latitude (index 0) and maximum and minimum longitude (index 1) for all sensors in the entire data structure.
I could definitely work it out with a series of loops in the traditional way, but I'm wondering if there is some slick way to do this in Scala (I'm not an expert in Scala). Ultimately it's not a super-important question since I can do it with my own loops. Just thought it would make an interesting question, and if someone can find a nice solution it would be worth finding out.
Edit: Here's a more concrete example of how the data structure works. Here, R1S1 means Road #1, Sensor #1, etc.
Array(Map("R1S1"->Array(32,117), "R1S2"->Array(33,118)),
Map("R2S1"->Array(32,116), "R2S2"->Array(34,118)))
If you can just convert your data structure to a flat list of one part of the coordinates, then you can call min and max on that. If you could start from the inside and work your way out:
Given the innermost Array[Double], you want to get the first element (or second).
Given the containing map, you just want to get the values out.
Given the containing array, flatten the array of arrays down to a single array.
Take the maximum of the result.
You can essentially do this in reverse, working from the outside in, dealing only with containers, with no for loops. Here's a complete example:
// Say how to order Double values
import Ordering.Double.IeeeOrdering
// Create a dedicated data structure for the coordinates
case class Coordinates(lat: Double, lon: Double)
// Find the minimum latitude value for your data structure
def minLat(world: Seq[Map[String, Seq[Coordinates]]]): Double =
world // Seq[Map[String, Seq[Coordinates]]]
.map { _.values } // Seq[Seq[ Seq[Coordinates]]]
.flatten // Seq[ Seq[Coordinates] ]
.flatten // Seq[ Coordinates ]
.map { _.lat } // Seq[ Double ]
.min
// Let's try it
val aWorld = Seq(Map(("a", Seq(Coordinates(-70, 40)))))
minLat(aWorld) // -70

Google Scripts: Populating Multi-Dimensional Array With Cell Values

I am learning the basics of Google Scripting after using VBA in Excel for many years, so please forgive the basic question.
I want to take a range of cells in my sheet using getValues() and then create a multi-element array.
Here is a simple example from my code:
var gameInfo = [];
gameInfo = gameMasterSheet.getRange(3, 3, 1, 9).getValues();
As you can see, the defined range is 9 cells in a single row.
My goal is to create an array from these 9 cells, and have each cell be accessible via a separate array element. However, it seems all 9 cell values are being inserted into gameInfo[0], and when I reference gameInfo[2] hoping to obtain the value of the third cell in the range, "undefined" is returned.
Is there a way to use getValues() to populate an array with separate elements? If so, how is this done? If not, what is a better alternative?
Thanks for any help you all can provide!
getValues returns a 2d array, even when it's a single row of cells. Think of it like this: gameInfo[row - 1][column - 1], so the top left is gameInfo[0][0].
All of your data is in gameInfo is in one row (gameInfo[0]), so the third element will be accessed as gameInfo[0][2] (row 1, column 3).
gameInfo[2] would be the third row, which is indeed outside of the range and undefined.
Also: to get just the values into an array from a 2d array, you could do this:
const values = [];
gameInfo.forEach(row => {
row.forEach(column => {
values.push(column);
})
})
See the script at the bottom of the Simple Mail Merge Tutorial for some useful code to retrieve data. Copy all the code from this comment down:
//////////////////////////////////////////////////////////////////////////////////////////
//
// The code below is reused from the 'Reading Spreadsheet data using JavaScript Objects'
// tutorial.
//
/////////
/////////////////////////////////////////////////////////////////////////////////
// getRowsData iterates row by row in the input range and returns an array of objects.
Once you have, use getRowsData to retrieve the data as an array of objects. If I understand you correctly, this will get you want you are after.
Karl S

Duplicate and giving identity to movieclips

I have a question regarding array and snap function to ask about on ActionScript3
I am creating a simple cube game which requires players to drag cubes of different colors together and the cubes would snap on the right of each other.
Cubes are able to duplicate infinitely from a master stationary cube and they are all MovieClips.
I have conditioned cubes of different colors to snap into position as I've wanted but here is the problem...
Cubes for example red, will not snap together as each duplicated cube does not have its personal identity like red1, red2, red3 and so on as they do not know who to snap to due to the fact that both would be called reddup.
I've read up on duplicating cubes and numbering it at the same time using dynamic array, but I have no clue how to implement it into my game.
The answer is to not work with explicit names.
An instance name is just a reference to an object. But there are other ways to reference objects that do not require individual names. An array is an example for that. Just add the cube objects to the array:
var cubes:Array = [];
// then
cubes.push(new Cube());
You can then iterate over the array via an index:
for(var i:int = 0; i < cubes.length; i++)
{
cubes[i] // this is the i-th cube, use it like an instance name
}

glDrawArrays with multiple triangles in a single array with a persistent first and count

I have a bunch of polygon data that I want to draw. I pulled out that drawing code so that it currently looks like this
for (int Index = 0; Index < Count; Index++) {
glDrawArrays(GL_TRIANGLE_FAN, Index * 4, 4);
}
I have a giant one dimensional array filled with multiple triangles, a new triangle every 4 vertices. Are there any OpenGL functions that I can replace this entire loop with so that I don't have the overhead of each glDrawArrays call? glMultiDrawArrays is sort of what I want, except I don't need a different first (well, I need a different first, but multiplied by a constant) and count each time.
I thought something like glVertexAttribDivisor would work, but nothing was drawing after I added it, I'm not completely sure if it works without instancing either.
EDIT: I am using a triangle fan, and I am buffering the vertices every frame.
glDrawElements() + GL_PRIMITIVE_RESTART & glPrimitiveRestartIndex().

Optimal referential 2d array?

So, I'm creating a fairly basic overhead 2d game where users can "draw" a map. (Actually, they dont draw it, the manually input a list of x/y's, but the design aspect isnt important just now.)
When a new tile is added, that tile goes into an array of all tiles ingame.
The centrepoint is 0,0. Tiles can be added in all directions, so may be at 1,1 or 100,100 or -50,-50.
Sometimes I want to determine what tile is at a location. One (imho bad) way of doing this would be to get the x/y and loop through all tiles and check if they are that location.
The way I'm currently doing it is to have a seperate 2d array of null elements, and when a tile is added, its set at that array. (ie tilemap[10][10] = tile[100])
Of course, because the values can go negative, tilemap [0][0] is actually the -1000/-1000 tile. (chosen as an arbitrary limit)
Is there a better way of doing this? I feel like using a massive array of mostly empty objects could be more optimal.
Many thanks.
One possible solution would be to keep an Object or Dictionary where the key is the x/y location.
So if you're adding a tile at 10,20, you can store it in the Object like:
this.m_objs[tileX + "_" + tileY] = new Tile;
So if you want to check if something's at position 10,20 you can by using something like:
public function checkIfExists( x:int, y:int ):void
{
return ( this.m_objs[x + "_" + y] != undefined );
}
I recomend using Dictionary, as searching in it much more faster then searching array. Judhing by possible coords -1000/-1000 it would be a great advantage!

Resources