aboutsummaryrefslogtreecommitdiff
path: root/Plugins/rowca.cpp
blob: 5c776ac9b7cce59b862c42496422c5f13dc48c74 (plain)
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
	One dimensional cellular automata, where each iteration
	adds a new row.

	Each cell has three neighbourns, top left, top and top right,
	as illustrated below:

	123
	 O

	Patterns that lead to a white cell are given as parameter,
	forming the rule of the automata.

	Example: parameter "001-010-011-100"

	Starting from a singe cell, this would lead to:

		O
	       OOO
	      OO  O
	     OO OOOO
	    OO  O   O
	   OO OOOO OOO
	  OO  O    O  O

	etc.
*/

#include <iostream>
#include <string>
#include <vector>

#include "plugin.h"
#include "pixels.h"

using namespace std;

vector<string> sv;

void iterate(const char *arg);
void draw(const _frame &f);

void perform_effect(_frame f, _args a)
{
	if(!a.s || !a.s[0]) return;

	if(strstr(a.s, "0") || strstr(a.s, "1")) iterate(a.s); else
	if(strcmp(a.s, "draw")==0) draw(f); else
	if(strcmp(a.s, "clear")==0) sv.clear();
}

void iterate(const char *arg)
{
	if(sv.empty())
	{
		sv.push_back("00100");	// start with a single cell
	}

	string s("00"), last=sv.back(), rule(arg);
	int i;

	for(i=0; i<=last.size()-3; i++)
	{
		if( rule.find(last.substr(i, 3)) != rule.npos )
			s.append("1");
		else
			s.append("0");
	}

	s.append("00");

	sv.push_back(s);
}

void draw(const _frame &f)
{
	if(sv.empty()) return;

	int x1=0, y1=0, x2=sv.back().size(), y2=sv.size();
	int i;

	pixels p(f);
	int prevy=-1;

	float sourcex, sourcey;
	string s;

	while(!p.eof())
	{
		if(p.y != prevy)
		{
			sourcey = p.y / (float)p.height();
			s = sv[sourcey * y2];

			// make s the size of x2
			i = (x2-s.size()) / 2;
			s.insert(1, i, '0');
			s.append(i, '0');

			prevy = p.y;
		}

		sourcex = p.x / (float)p.width();

		if( !s.compare(sourcex * x2, 1, "1") )
			p.putrgb(255, 255, 255);
		else
			p.putrgb(0, 0, 0);

		p.next();
	}
	cout << "rows: " << y2 << " size of last row: " << x2 << endl;
}