Generating Grid Coordinates

CodeNinjaMike 0 Tallied Votes 578 Views Share

So, basically im using the codea bove to generate coordinates for a grid, but I only want to generate coordinates for the "outer ring" of a square. SO imagine a grid of 7x7, I won't generate coordinates for the inner 5x5 square, only for the outer ring. Basically I drew it all out on paper, used fake numbers and based off that I came up with t his logic that can be changed around. In the code "25" is used as dummy data for the distance between the center of one square and another. The 150 that you see can be changed to be whatever the far end coordinate is. It could be 250, or even 200.

Also, I was trying to do this code in such a way that I could loop through all the spots (49), and only set data for the top row, the bottom row and the first and last spot for the rows inbetween.

SOme other logic i gathered up, a table goes from 0,0 to 6,6. If (x is between 0 and 6 and y is 0) or (x is between 0 and 6 and y is always 6) then do data for the top and bottom row respectively. And then for the rows in between (if x is 0 OR 6 and y is between 1 and 5), do data. Id loop through 49 times and using the following logic:

x=((i%7)+1)*25
y=((i/7)+1)25

#include <iostream>
#include <iomanip>
using namespace std;


struct Board
{
	int x;
	int y;
};
void main()
{
	//Declare an array to store the board data
	Board m_Board[24];
	//These help tell when the spot is according to x and z (or row and column)
	int xRows=0;int zRows=0;
	for (int i=0;i<24;i++)
	{
		//Bottom line of 7 squares
		if (i<7)
		{
			//Set the x to 150 base, and decrement it as it goes to the left by the space difference between two squares (25)
			m_Board[i].x=(150-(xRows*25));
			//Y never changes
			m_Board[i].y=150;
			//Make sure zRows is always 0
			zRows=0;
		}
		else if (i>6 && i<13)
		{
			//make sure xRows is always 0
			xRows=0;
			//x is always 0
			m_Board[i].x=0;
			//Y changes by 25 each time it goes up the board on the left side
			m_Board[i].y=150-(zRows*25);
		}
		else if (i>12 && i < 19)
		{
			//make sure zRows is always 0
			zRows=0;
			//X goes up by 25 as it goes to the right on the top side
			m_Board[i].x=0+(xRows*25);
			//Y stays 0
			m_Board[i].y=0;
		}
		else
		{
			//Make sure XRows is always 0
			xRows=0;
			//Make sure x is always 150
			m_Board[i].x=150;
			//y goes up 25 as the spots go back down towards "start", on the right side of the board
			m_Board[i].y=25+(zRows*25);
		}
		//Increment the values when data is set for a spot
		xRows++;
		zRows++;


	}
	//Print out the data
	for (int i=0;i<24;i++)
		{
			//It's set to do a new line every 4 outputs
			if (i%4==0)
			{
				cout << endl;
			}
			//Output the data
			cout << left <<setw(2) <<"("<<m_Board[i].x << ", " << m_Board[i].y <<")" << " ";
		}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.