From 581e2e60e8d285a0a1f06ec39d71518ad6002ff3 Mon Sep 17 00:00:00 2001 From: klmp200 Date: Wed, 28 Dec 2016 02:38:48 +0100 Subject: [PATCH] left/right/top/down rules --- LibAutomaton/CellElement.c | 15 ++++++ LibAutomaton/CellElement.h | 15 ++++++ LibAutomaton/matrix.c | 98 ++++++++++++++++++++++++++++++++++++++ LibAutomaton/matrix.h | 37 ++++++++++++++ main.c | 5 +- 5 files changed, 169 insertions(+), 1 deletion(-) diff --git a/LibAutomaton/CellElement.c b/LibAutomaton/CellElement.c index 7aa444a..d9cb03b 100644 --- a/LibAutomaton/CellElement.c +++ b/LibAutomaton/CellElement.c @@ -153,3 +153,18 @@ bool AND(bool a, bool b){ return false; } } + +bool XOR(bool a, bool b){ + if (!(a && b) && (a||b)){ + return true; + } else { + return false; + } +} + +bool ErrorToFalse(bool x){ + if (x == ERROR){ + return false; + } + return x; +} diff --git a/LibAutomaton/CellElement.h b/LibAutomaton/CellElement.h index 982fa1d..b0c7ee8 100644 --- a/LibAutomaton/CellElement.h +++ b/LibAutomaton/CellElement.h @@ -161,4 +161,19 @@ bool OR(bool a, bool b); */ bool AND(bool a, bool b); +/** +* Apply XOR operator to a bool a and a bool b +* @param a a bool +* @param b a bool +* @return result a and b +*/ +bool XOR(bool a, bool b); + +/** +* Return false if the bool is ERROR +* @param x a bool +* @return result a bool +*/ +bool ErrorToFalse(bool x); + #endif diff --git a/LibAutomaton/matrix.c b/LibAutomaton/matrix.c index b04fa3a..19b1c1b 100644 --- a/LibAutomaton/matrix.c +++ b/LibAutomaton/matrix.c @@ -474,6 +474,104 @@ void FreeBooleanMatrix(BooleanMatrix matrix){ free(matrix.data); } + +Matrix basicRule(Matrix m, int incrh, int incrv){ + int i; + int j; + bool a; + bool b; + Matrix result = CreateMatrix(); + + result.colCount = m.colCount; + result.rowCount = m.rowCount; + + for (i=0; i < m.rowCount; i++){ + for (j = 0; j < m.colCount; j++){ + a = GetCellValue(m, j, i); + b = ErrorToFalse(GetCellValue(m, j + incrh, i + incrv)); + if (XOR(a, b)){ + SetCellValue(result, j, i, true); + } + } + } + return result; +} + +Matrix leftRule(Matrix m){ + Matrix result = CreateMatrix(); + int i; + int j; + + result.colCount = m.colCount; + result.rowCount = m.rowCount; + + for (i=1;i #include #include -#include int main(int argc, char **argv){ int Rule = 256; @@ -17,12 +16,16 @@ int main(int argc, char **argv){ Matrix matrix = CreateMatrix(); Matrix m2; Matrix m3; + Matrix m4; BooleanMatrix bmatrix = CreateBooleanMatrix(5, 5); bmatrix = RandomizeBooleanMatrix(bmatrix); m3 = newMatrix(bmatrix); FreeBooleanMatrix(bmatrix); BasicPrintMatrix(m3); + m4 = downRule(m3); + BasicPrintMatrix(m4); + freeMatrix(m4); freeMatrix(m3); applyRules(matrix,Rule,N);