0
\$\begingroup\$

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?

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

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.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Could the information the data be stored in a matrix like structure, instead of printing it.. \$\endgroup\$ – Lamda May 15 '16 at 12:04
  • \$\begingroup\$ @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.. \$\endgroup\$ – Greffin28 May 15 '16 at 12:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.