-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrule110-netpbm.c
More file actions
87 lines (69 loc) · 2.42 KB
/
rule110-netpbm.c
File metadata and controls
87 lines (69 loc) · 2.42 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
77
78
79
80
81
82
83
84
85
86
87
/*
* Cellular automaton I made. Now displayed as .pbm
* https://en.wikipedia.org/wiki/Rule_110
* https://en.wikipedia.org/wiki/Netpbm
* https://github.com/r4v10l1
*/
#include <stdio.h>
// Height must be > width
#define HEIGHT 1000
#define WIDTH 999
int calc_rule(int a, int b, int c, int mode); // Rule conditionals
int calc_array();
int main() {
int array_len = calc_array();
char line[array_len];
char line_aux[array_len];
int rule_results;
int position = 0;
// Clear the arrays
for (int n = 0; n <= array_len; n++){
line[n] = 0;
line_aux[n] = 0;
}
line[1] = 1;
// Start of the .pbm file
printf("P1\n"); // Black and white
printf("# Made with https://github.com/r4v10l1/rule110\n");
printf("%d %d\n", WIDTH, HEIGHT);
// Display each line
for (int y = 1; y <= HEIGHT; y++){
for (int x = 1; x <= WIDTH; x++){
rule_results = calc_rule(line[position], line[position+1], line[position+2], 1);
putchar(rule_results+48);
// Stop printing if the lines are too long
if (x % 75 == 0) putchar('\n');
// Write into the aux array instead of the main one while reading
line_aux[x] = rule_results;
position++;
}
// After reading the array, replace the aux with the main one
for (int n = 0; n <= array_len; n++){
line[n] = line_aux[n];
}
putchar('\n');
position = 0;
}
}
int calc_array() {
if (HEIGHT >= WIDTH) return HEIGHT;
else return WIDTH;
}
int calc_rule(int a, int b, int c, int mode) {
// Vanilla rule110 or mirror
if (mode == 1) {
int temp = a;
a = c;
c = temp;
}
// Made a conditional for each rule so it's more visual
if (a == 0 && b == 0 && c == 0) return 0; // 000
else if (a == 0 && b == 0 && c == 1) return 1; // 001
else if (a == 0 && b == 1 && c == 0) return 1; // 010
else if (a == 0 && b == 1 && c == 1) return 1; // 011
else if (a == 1 && b == 0 && c == 0) return 0; // 100
else if (a == 1 && b == 0 && c == 1) return 1; // 101
else if (a == 1 && b == 1 && c == 0) return 1; // 110
else if (a == 1 && b == 1 && c == 1) return 0; // 111
else return 0; // ???
}