aboutsummaryrefslogtreecommitdiff
path: root/Plugins/rowca.cpp
blob: de839332e209b1327055519b8d7639819801288b (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
114
115
116
117
118
119
120
121
/*
	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)
{
	arguments ar(a.s);
	if(!ar.count()) {
		printf("Usage: rowca <rule of automata, or command>\n");
		printf("Example rule: 001-010-011-100\n");
		printf("Commands: draw, clear\n");
		return;
	}

	char *t = ar[0];

	if(strstr(t, "0") || strstr(t, "1")) iterate(t); else
	if(strcmp(t, "draw")==0) draw(f); else
	if(strcmp(t, "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;
}