-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcgol.scad
More file actions
executable file
·76 lines (66 loc) · 2.61 KB
/
Copy pathcgol.scad
File metadata and controls
executable file
·76 lines (66 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/**
* Conway's Game of Life in OpenSCAD
*
* To generate images of different generations:
* openscad -o gen0.png -Dgen=0 cgol.scad
* openscad -o gen1.png -Dgen=1 cgol.scad
* ...
* openscad -o gen25.png -Dgen=25 cgol.scad
*
* This version dynamically computes any requested generation.
*/
gen = 0; // generation can be overridden by -Dgen=N
// Grid dimensions
width = 190;
height = 190;
// Initial grid (Generation 0)
gen0 = [
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
];
// Function to count alive neighbors
function count_neighbors(grid, x, y) =
grid[(y-1 + height) % height][(x-1 + width) % width] +
grid[(y-1 + height) % height][x] +
grid[(y-1 + height) % height][(x+1) % width] +
grid[y][(x-1 + width) % width] +
grid[y][(x+1) % width] +
grid[(y+1) % height][(x-1 + width) % width] +
grid[(y+1) % height][x] +
grid[(y+1) % height][(x+1) % width];
// Function to compute the next generation from a given grid
function next_grid(grid) =
[
for (y = [0:height-1]) [
for (x = [0:width-1]) (
(grid[y][x] == 1 && (count_neighbors(grid, x, y) == 2 || count_neighbors(grid, x, y) == 3)) ||
(grid[y][x] == 0 && count_neighbors(grid, x, y) == 3) ? 1 : 0
)
]
];
// Recursive function to compute the nth generation from the initial grid
function compute_generation(grid, n) =
n == 0 ? grid : compute_generation(next_grid(grid), n-1);
// Compute the current generation grid dynamically
current_grid = compute_generation(gen0, gen);
// Module to render the grid
module render_grid(g) {
// Draw a cube for each alive cell
for (y = [0:height-1]) {
for (x = [0:width-1]) {
if (g[y][x] == 1)
translate([x, y, 0]) cube([1,1,1], center=false);
}
}
}
// Render the current grid
render_grid(current_grid);