CellularAutomata
Loading...
Searching...
No Matches
states.inl
1#include "states.hpp"
2#include "../core/rng.hpp"
3#include "../core/exceptions.hpp"
4
5namespace CellularAutomata
6{
7 namespace States
8 {
9 template <typename T>
10 void normal(T* array, const unsigned int h, const unsigned int w)
11 {
12 for (int i = 0; i < h * w; i++)
13 {
14 array[i] = CellularAutomata::random::random<T>(0, 1);
15 }
16 }
17
18 template <typename T>
19 void binary(T* array, const unsigned int h, const unsigned int w)
20 {
21 for (int i = 0; i < h * w; i++)
22 {
23 if (CellularAutomata::random::random<float>(0, 1) > 0.5)
24 {
25 array[i] = static_cast<T>(1);
26 }
27 else
28 {
29 array[i] = static_cast<T>(0);
30 }
31 }
32 }
33
34 namespace Objects
35 {
36 template <typename T>
37 inline void copyInto(T* src, const unsigned int src_h, const unsigned int src_w, T* dst, const unsigned int dst_h, const unsigned int dst_w, const unsigned int y, const unsigned int x)
38 {
39 if (src_h + y > dst_h || src_w + x > dst_w)
41
42 int i = 0;
43 for (int _y = y; _y < y + src_h; _y++)
44 {
45 for (int _x = x; _x < x + src_w; _x++)
46 {
47 dst[_y * dst_w + _x] = src[i];
48 i++;
49 }
50 }
51 }
52
53 template <typename T>
54 void Glider(State<T>* state, const unsigned int x, const unsigned int y)
55 {
56 T pattern[] = {
57 0, 1, 0,
58 0, 0, 1,
59 1, 1, 1 };
60
61 copyInto(pattern, 3, 3, state->curr, state->height, state->width, y, x);
62 }
63
64 template <typename T>
65 void Spaceship(State<T>* state, const unsigned int x, const unsigned int y)
66 {
67 T pattern[] = {
68 0, 1, 1, 1, 1,
69 1, 0, 0, 0, 1,
70 0, 0, 0, 0, 1,
71 1, 0, 0, 1, 0 };
72
73 copyInto(pattern, 4, 5, state->curr, state->height, state->width, y, x);
74 }
75
76 template <typename T>
77 void Bipole1(State<T>* state, const unsigned int x, const unsigned int y)
78 {
79 T pattern[] = {
80 1, 1, 0, 0,
81 1, 0, 0, 0,
82 0, 0, 0, 1,
83 0, 0, 1, 1 };
84
85 copyInto(pattern, 4, 4, state->curr, state->height, state->width, y, x);
86 }
87
88 template <typename T>
89 void Bipole2(State<T>* state, const unsigned int x, const unsigned int y)
90 {
91 T pattern[] = {
92 1, 1, 0, 0, 0,
93 1, 0, 0, 0, 0,
94 0, 1, 0, 1, 0,
95 0, 0, 0, 0, 1,
96 0, 0, 0, 1, 1 };
97
98 copyInto(pattern, 5, 5, state->curr, state->height, state->width, y, x);
99 }
100
101 template <typename T>
102 void r_Pentomino(State<T>* state, const unsigned int x, const unsigned int y)
103 {
104 T pattern[] = {
105 0, 1, 1,
106 1, 1, 0,
107 0, 1, 0 };
108
109 copyInto(pattern, 3, 3, state->curr, state->height, state->width, y, x);
110 }
111 }
112 }
113}
2D buffered matrices
Definition common.hpp:56
const unsigned int width
Width of 2D Matrix.
Definition common.hpp:76
T * curr
Current 2D Matrix buffer.
Definition common.hpp:61
const unsigned int height
Height of 2D Matrix.
Definition common.hpp:71
Accessed a value outside the bounds of matrix.
Definition exceptions.hpp:46
void Bipole2(State< T > *state, const unsigned int x, const unsigned int y)
Copies a (alternative) bipole (5x5) into given position (https://conwaylife.com/wiki/Bipole)
Definition states.inl:89
void Bipole1(State< T > *state, const unsigned int x, const unsigned int y)
Copies a bipole (4x4) into given position (https://conwaylife.com/wiki/Bipole)
Definition states.inl:77
void r_Pentomino(State< T > *state, const unsigned int x, const unsigned int y)
Copies a R-pentomino (3x3) into given position (https://conwaylife.com/wiki/R-pentomino)
Definition states.inl:102
void copyInto(T *src, const unsigned int src_h, const unsigned int src_w, T *dst, const unsigned int dst_h, const unsigned int dst_w, const unsigned int y, const unsigned int x)
Copy an (small) array into another (large) array at a given position.
Definition states.inl:37
void Glider(State< T > *state, const unsigned int x, const unsigned int y)
Copies a glider (3x3) into given position (https://conwaylife.com/wiki/Glider)
Definition states.inl:54
void Spaceship(State< T > *state, const unsigned int x, const unsigned int y)
Copies a spaceship (4x5) into given position (https://conwaylife.com/wiki/Spaceship)
Definition states.inl:65
void binary(T *array, const unsigned int h, const unsigned int w)
Fills the kernel with either 0 or 1.
Definition states.inl:19
void normal(T *array, const unsigned int h, const unsigned int w)
Fills the kernel with values between [0, 1].
Definition states.inl:10
Main namespace.
Definition common.hpp:33