I'm using c# in Unity (though this question isn't specific to either) and need to get all possible combinations of the two vectors:
(x, y, z) and (-x, -y, -z)
so the result would be: (x, y, z) (-x, y, z) (-x, -y, z) (-x, -y, -z) (x, -y, z) (x, -y, -z) (x, y, -z) (-x, y, -z)
I know x, y and z. The origin is in the centre of the cube all I need is to get each corner of the cube's x, y, z position.
The points I need to get:
The absolutely simplest, fastest, and easiest to understand way to make the eight combinations is hard-coding the eight combinations of signs:
IList<int[]> MakeCombinations(int x, int y, int z) {
return new[] {
new[] { x, y, z}
, new[] { x, y, -z}
, new[] { x, -y, z}
, new[] { x, -y, -z}
, new[] {-x, y, z}
, new[] {-x, y, -z}
, new[] {-x, -y, z}
, new[] {-x, -y, -z}
};
}
If you prefer a solution with a little more programming, you can do it like this (I am using arrays for vectors; you could use your specific class):
int[] data = new[] {x, y, z};
IList<int[]> all = new List<int[]>();
for (int mask = 0 ; mask != 8 ; mask++) {
int[] next = new[] {data[0], data[1], data[2]};
if ((mask & 0x01) != 0) next[0] *= -1;
if ((mask & 0x02) != 0) next[1] *= -1;
if ((mask & 0x04) != 0) next[2] *= -1;
all.add(next);
}
The combination of pluses and minuses in front of the vector component is modeled with a single three-bit number mask
. When bit at position N
is set, the N
-th component is taken with the negative sign; otherwise, it is taken with a positive sign.