# Draw a square given 2 corners

I am at the moment trying to come up with a simple method to extract some data points, given two corners.

A***************
****************
***************B


I tried to illustrate my situation above.
Given corner A and B (x,y,z) how do generate the (x,y,z) coordinate for all the * in the square?

• "sphere" and "square" are different things. – tkausl May 15 '16 at 11:06
• square - corrected. – Lamda May 15 '16 at 11:15
• Is that a discrete grid? Otherwise the set of those is infinitely big. – Bartek Banachewicz May 15 '16 at 12:41

I assume you use integer for the coordinates. You could do something like the following:

for (int z = A.z; z <= B.z; z++) {
for (int y = A.y; y <= B.y; y++) {
for (int x = A.x; x <= B.x; x++) {
// Do things here.. You can get the coordinates from the x, y, and z variables.
}
}
}


This will basically iterate through every point (*) including the point given (A and B).

I don't know what you're looking for but it seems to me this is what you ask.

Here's a fast example without using z (tested with cpp.sh):

// Example program
#include <iostream>

struct point {
int x, y, z;
} A, B;

int main()
{
A.x = 3;
A.y = 4;
//A.z = 0;

B.x = 7;
B.y = 5;
//B.z = 0;
//for (int z = A.z; z <= B.z; z++) {
for (int y = A.y; y <= B.y; y++) {
for (int x = A.x; x <= B.x; x++) {
// Do things here.. You can get the coordinates from the x, y, and z variables.
if (x == A.x && y == A.y) printf("A[%i, %i]", A.x, A.y);
else if (x == B.x && y == B.y) printf("B[%i, %i]", B.x, B.y);
else printf("[%i, %i]", x, y);
}
printf("\n");
}
//}
}


Example above will print all the coordinates like so: [x, y]. Oh, and also the above example won't work if B is smaller than A.. Need to do something about that, probably a check or even a dynamic approach.

• Could the information the data be stored in a matrix like structure, instead of printing it.. – Lamda May 15 '16 at 12:04
• @Lamda Wouldn't you just put it into a vector (for example: Vector4 or Vector3 or i could say a 4x1 or 3x1 matrix)? As you can easily multiply a matrix with a vector (4x4 * 4x1 or 3x3 * 3x1). Sorry if i'm getting the wrong idea here.. – Greffin28 May 15 '16 at 12:14