Skip to content

Commit 0256407

Browse files
committedMay 4, 2016
Initial version.
Conceptual version supports cubes generation and layers rotation.
0 parents  commit 0256407

File tree

4 files changed

+231
-0
lines changed

4 files changed

+231
-0
lines changed
 

‎.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
*~
2+
*.bak
3+
*.png

‎LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2016 Evgeny Gagauz
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

‎README.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
POV-Ray Rubik's Cube
2+
====================
3+
4+
A set of [POV-Ray](http://povray.org) macros to generate Rubik's Cubes
5+
of different sizes.

‎rubik-cube.pov

+202
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
#include "colors.inc"
2+
#include "transforms.inc"
3+
#include "shapes.inc"
4+
5+
6+
camera {
7+
location <2, 2, -7>
8+
look_at <0, 0, 0>
9+
}
10+
11+
12+
light_source {
13+
<2, 4, -3>
14+
color White
15+
}
16+
17+
light_source {
18+
<4, 2, -3>
19+
color White
20+
}
21+
22+
23+
// Axis
24+
cylinder {
25+
<0, 0, 0>, <5, 0, 0>, 0.01
26+
open
27+
pigment { Red }
28+
}
29+
30+
cylinder {
31+
<0, 0, 0>, <0, 5, 0>, 0.01
32+
open
33+
pigment { Green }
34+
}
35+
36+
cylinder {
37+
<0, 0, 0>, <0, 0, 5>, 0.01
38+
open
39+
pigment { Blue }
40+
}
41+
42+
43+
// Cube
44+
#declare classic_colors = array[6] {Blue, Red, Yellow, Green, Orange, White};
45+
46+
47+
#macro create_part(colors)
48+
object {
49+
Round_Box(<-0.5, -0.5, -0.5>, <0.5, 0.5, 0.5>, 0.1, 1)
50+
pigment {
51+
cubic colors[0], colors[1], colors[2], colors[3], colors[4], colors[5]
52+
}
53+
translate <0.5, 0.5, 0.5>
54+
}
55+
#end
56+
57+
58+
#macro get_all_parts(dim_x, dim_y, dim_z, colors)
59+
#local parts = array[dim_x * dim_y * dim_z];
60+
#for (pos_x, 0, dim_x - 1)
61+
#for (pos_y, 0, dim_y - 1)
62+
#for (pos_z, 0, dim_z - 1)
63+
#local part_colors = colors;
64+
#if (pos_x != 0) #local part_colors[3] = Black; #end
65+
#if (pos_x != dim_x - 1) #local part_colors[0] = Black; #end
66+
#if (pos_y != 0) #local part_colors[4] = Black; #end
67+
#if (pos_y != dim_y - 1) #local part_colors[1] = Black; #end
68+
#if (pos_z != 0) #local part_colors[5] = Black; #end
69+
#if (pos_z != dim_z - 1) #local part_colors[2] = Black; #end
70+
71+
#local parts[pos_x + pos_y * dim_x + pos_z * dim_x * dim_y] =
72+
object {
73+
create_part(part_colors)
74+
transform {
75+
translate <pos_x, pos_y, pos_z> - <dim_x / 2, dim_y / 2, dim_z / 2>
76+
}
77+
}
78+
#end
79+
#end
80+
#end
81+
parts
82+
#end
83+
84+
85+
#macro rotate_by_x(dim_x, dim_y, dim_z, parts, layer, a)
86+
#local new_parts = parts;
87+
88+
#for (pos_y, 0, dim_y - 1)
89+
#for (pos_z, 0, dim_z - 1)
90+
#local new_pos_y = dim_y - pos_z - 1;
91+
#local new_pos_z = pos_y;
92+
#local new_parts[layer + new_pos_y * dim_x + new_pos_z * dim_x * dim_y] =
93+
object {
94+
parts[layer + pos_y * dim_x + pos_z * dim_x * dim_y]
95+
transform {
96+
rotate a * x
97+
}
98+
}
99+
#end
100+
#end
101+
102+
new_parts
103+
#end
104+
105+
106+
#macro rotate_by_y(dim_x, dim_y, dim_z, parts, layer, a)
107+
#local new_parts = parts;
108+
109+
#for (pos_x, 0, dim_x - 1)
110+
#for (pos_z, 0, dim_z - 1)
111+
#local new_pos_x = dim_x - pos_z - 1;
112+
#local new_pos_z = pos_x;
113+
#local new_parts[new_pos_x + layer * dim_x + new_pos_z * dim_x * dim_y] =
114+
object {
115+
parts[pos_x + layer * dim_x + pos_z * dim_x * dim_y]
116+
transform {
117+
rotate a * y
118+
}
119+
}
120+
#end
121+
#end
122+
123+
new_parts
124+
#end
125+
126+
127+
#macro rotate_by_z(dim_x, dim_y, dim_z, parts, layer, a)
128+
#local new_parts = parts;
129+
130+
#for (pos_x, 0, dim_x - 1)
131+
#for (pos_y, 0, dim_y - 1)
132+
#local new_pos_x = dim_x - pos_y - 1;
133+
#local new_pos_y = pos_x;
134+
#local new_parts[new_pos_x + new_pos_y * dim_x + layer * dim_x * dim_y] =
135+
object {
136+
parts[pos_x + pos_y * dim_x + layer * dim_x * dim_y]
137+
transform {
138+
rotate a * z
139+
}
140+
}
141+
#end
142+
#end
143+
144+
new_parts
145+
#end
146+
147+
148+
#macro move_parts(dim_x, dim_y, dim_z, parts, movement)
149+
// todo check that movement is valid.
150+
151+
#if (movement.x != 0)
152+
#switch (movement.x)
153+
#case (-90)
154+
#local parts = rotate_by_x(dim_x, dim_y, dim_z, parts, movement.t, 90);
155+
#case (180) #case (-180)
156+
#local parts = rotate_by_x(dim_x, dim_y, dim_z, parts, movement.t, 90);
157+
#case (90)
158+
#local parts = rotate_by_x(dim_x, dim_y, dim_z, parts, movement.t, 90);
159+
#end
160+
#elseif (movement.y != 0)
161+
#switch (movement.y)
162+
#case (-90)
163+
#local parts = rotate_by_y(dim_x, dim_y, dim_z, parts, movement.t, 90);
164+
#case (180) #case (-180)
165+
#local parts = rotate_by_y(dim_x, dim_y, dim_z, parts, movement.t, 90);
166+
#case (90)
167+
#local parts = rotate_by_y(dim_x, dim_y, dim_z, parts, movement.t, 90);
168+
#end
169+
#elseif (movement.z != 0)
170+
#switch (movement.z)
171+
#case (-90)
172+
#local parts = rotate_by_z(dim_x, dim_y, dim_z, parts, movement.t, 90);
173+
#case (180) #case (-180)
174+
#local parts = rotate_by_z(dim_x, dim_y, dim_z, parts, movement.t, 90);
175+
#case (90)
176+
#local parts = rotate_by_z(dim_x, dim_y, dim_z, parts, movement.t, 90);
177+
#end
178+
#end
179+
180+
parts
181+
#end
182+
183+
184+
#macro create_cube(dim_x, dim_y, dim_z, colors, movements)
185+
#local parts = get_all_parts(dim_x, dim_y, dim_z, colors)
186+
187+
#for (movementIx, 0, dimension_size(movements, 1) - 1)
188+
#local parts = move_parts(dim_x, dim_y, dim_z, parts, movements[movementIx]);
189+
#end
190+
191+
union {
192+
#for (item, 0, dim_x * dim_y * dim_z - 1)
193+
object { parts[item] }
194+
#end
195+
}
196+
#end
197+
198+
199+
create_cube(
200+
3, 3, 3, classic_colors,
201+
array[4] {<90, 0, 0, 1>, <0, 0, 90, 1>, <-90, 0, 0, 1>, <0, 0, -90, 1>}
202+
)

0 commit comments

Comments
 (0)
Please sign in to comment.