Well, i was bored in math class today...
I'm working on creating a 4D object viewer, and i figured i'd start out with a standard 4 dimensional "cube". It's easy enough to come up with all of the points for it, but another problem is what points to connect the other points to (by drawing a line). So, i started working my way up from 1 dimension to 3 dimensions, and it seemed apparent that there's a pattern. I decided to make an algorithm for generating an n dimensional hypercube, width 2.
n is the number of dimensions of the hypercube.
Each point P is connected to n other lines.
There are 2^n points in the hypercube
Start out with a zeroed binary number, n digits long.
IE
n n-1 ..... 2 1 <- dimension
0 0 ..... 0 0 <- digit
This number is L
For each point P, increase L by 1, and use this number as an ID (identifier) for point P, starting from ID 0.
The locations of each point on each axis
IE
n n-1 ...... w z y x
is -1 if the corresponding binary digit is 0, and 1 if the corresponding binary digit is 1.
EX:
n=4
ID=5 (0101 base 2)
0 1 0 1 <- corresponding binary digit
w y z x <- dimension
-1 1 -1 1 <- location on the corresponding axis.
In programmer\\\'s words, where a is the ID, and b is the dimension number (from 0 to n-1):
location=-1*(-1)^((a>>b)&1);
Now you have the location of each point P, but you still need to find out which other points, from any point P, to draw lines to.
For every digit of every point P that is 0, that point hooks up to ID+2^digit. This will give you n points on the first ID number (00...00) and 0 points on the last (11...11).
In programmer talk, where a and b are as described above, and \\\"hook\\\" is a variable storing an ID that point a hooks up to:
if(((a>>b)&1)==0)
{
hook=a+2^b;
P[a].hook=hook;
P[hook].hook=a;//*
}
The line with the asterisk makes sure that every point has n points it hooks up to, otherwise P[hook] would remain empty.
And there you have it! Easy, right?
Here are the results i got for dimensions 1-5. Notice how for the \\\"Hooks\\\", each number only occurs N times.
n=1 (line)
0: [-1,, Hooks=1
1: [1,, Hooks=0
^^little glitch when n=1 with the text output
n=2 (square)
0: [-1,-1], Hooks=1, 2
1: [-1,1], Hooks=0, 3
2: [1,-1], Hooks=3, 0
3: [1,1], Hooks=2, 1
n=3 (cube)
0: [-1,-1,-1], Hooks=1, 2, 4
1: [-1,-1,1], Hooks=0, 3, 5
2: [-1,1,-1], Hooks=3, 0, 6
3: [-1,1,1], Hooks=2, 1, 7
4: [1,-1,-1], Hooks=5, 6, 0
5: [1,-1,1], Hooks=4, 7, 1
6: [1,1,-1], Hooks=7, 4, 2
7: [1,1,1], Hooks=6, 5, 3
n=4 (hypercube)
0: [-1,-1,-1,-1], Hooks=1, 2, 4, 8
1: [-1,-1,-1,1], Hooks=0, 3, 5, 9
2: [-1,-1,1,-1], Hooks=3, 0, 6, 10
3: [-1,-1,1,1], Hooks=2, 1, 7, 11
4: [-1,1,-1,-1], Hooks=5, 6, 0, 12
5: [-1,1,-1,1], Hooks=4, 7, 1, 13
6: [-1,1,1,-1], Hooks=7, 4, 2, 14
7: [-1,1,1,1], Hooks=6, 5, 3, 15
8: [1,-1,-1,-1], Hooks=9, 10, 12, 0
9: [1,-1,-1,1], Hooks=8, 11, 13, 1
10: [1,-1,1,-1], Hooks=11, 8, 14, 2
11: [1,-1,1,1], Hooks=10, 9, 15, 3
12: [1,1,-1,-1], Hooks=13, 14, 8, 4
13: [1,1,-1,1], Hooks=12, 15, 9, 5
14: [1,1,1,-1], Hooks=15, 12, 10, 6
15: [1,1,1,1], Hooks=14, 13, 11, 7
n=5 (UBERcube!!!!!)
0: [-1,-1,-1,-1,-1], Hooks=1, 2, 4, 8, 16
1: [-1,-1,-1,-1,1], Hooks=0, 3, 5, 9, 17
2: [-1,-1,-1,1,-1], Hooks=3, 0, 6, 10, 18
3: [-1,-1,-1,1,1], Hooks=2, 1, 7, 11, 19
4: [-1,-1,1,-1,-1], Hooks=5, 6, 0, 12, 20
5: [-1,-1,1,-1,1], Hooks=4, 7, 1, 13, 21
6: [-1,-1,1,1,-1], Hooks=7, 4, 2, 14, 22
7: [-1,-1,1,1,1], Hooks=6, 5, 3, 15, 23
8: [-1,1,-1,-1,-1], Hooks=9, 10, 12, 0, 24
9: [-1,1,-1,-1,1], Hooks=8, 11, 13, 1, 25
10: [-1,1,-1,1,-1], Hooks=11, 8, 14, 2, 26
11: [-1,1,-1,1,1], Hooks=10, 9, 15, 3, 27
12: [-1,1,1,-1,-1], Hooks=13, 14, 8, 4, 28
13: [-1,1,1,-1,1], Hooks=12, 15, 9, 5, 29
14: [-1,1,1,1,-1], Hooks=15, 12, 10, 6, 30
15: [-1,1,1,1,1], Hooks=14, 13, 11, 7, 31
16: [1,-1,-1,-1,-1], Hooks=17, 18, 20, 24, 0
17: [1,-1,-1,-1,1], Hooks=16, 19, 21, 25, 1
18: [1,-1,-1,1,-1], Hooks=19, 16, 22, 26, 2
19: [1,-1,-1,1,1], Hooks=18, 17, 23, 27, 3
20: [1,-1,1,-1,-1], Hooks=21, 22, 16, 28, 4
21: [1,-1,1,-1,1], Hooks=20, 23, 17, 29, 5
22: [1,-1,1,1,-1], Hooks=23, 20, 18, 30, 6
23: [1,-1,1,1,1], Hooks=22, 21, 19, 31, 7
24: [1,1,-1,-1,-1], Hooks=25, 26, 28, 16, 8
25: [1,1,-1,-1,1], Hooks=24, 27, 29, 17, 9
26: [1,1,-1,1,-1], Hooks=27, 24, 30, 18, 10
27: [1,1,-1,1,1], Hooks=26, 25, 31, 19, 11
28: [1,1,1,-1,-1], Hooks=29, 30, 24, 20, 12
29: [1,1,1,-1,1], Hooks=28, 31, 25, 21, 13
30: [1,1,1,1,-1], Hooks=31, 28, 26, 22, 14
31: [1,1,1,1,1], Hooks=30, 29, 27, 23, 15
I tried to post this with an example of a ten dimensional cube, but it was 1024 lines, and i think it broke the internet...