summaryrefslogtreecommitdiff
path: root/js/Harmonograph.js
blob: be219b8867aaba9c798eb029a1c189768ccbcfeb (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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
var t = 0;
var pp;
var AMAX = 1000;
var DMIN = 0.0001;
var DMAX = 0.0003;
var FMAX = 0.1;
var PMAX = 6.28;
var ADIF = AMAX / 10;
var DDIF = DMAX / 10;
var FDIF = FMAX / 10;
var PDIF = PMAX / 10;
var npendulums = 0;
var started = false;

function sinExp(A, t, f, p, d)
{


	return A*sin(t*f + p) * exp(-d*t);
}

var Pendulum = function(a, d, f, p, xy)
{
	this.a = a;
	this.d = d;
	this.f = f;
	this.p = p;
	this.xy = xy;
	pendulums.push(this);
}

Pendulum.prototype.swing = function()
{
	//document.write(this.a + " " + t + " " + this.f + " " + this.p + " " + this.d + " " + this.xy + "<br>");
	if (this.xy == 0)
	{
		var x = sinExp(this.a, t, this.f, this.p, this.d);
		var y = 0;
	}
	else
	{
		var x = 0;
		var y = sinExp(this.a, t, this.f, this.p, this.d);
	}

	return [x, y];
}

var pendulums = [];


function calculate()
{
	var xsum = 0;
	var ysum = 0;
	var sw;
	for (var i = 0; i < pendulums.length; i++)
	{
		sw = pendulums[i].swing();
		//document.write(sw + "<br>");
		xsum += sw[0];
		ysum += sw[1];
	}
	return [xsum/pendulums.length, ysum/pendulums.length];
}

function addPendulum()
{
	var txt = document.createElement("p");
	txt.innerHTML = "Phase:";
	var p = document.createElement("input");
	p.type = "number";
	p.value = PI;
	p.id = "p" + npendulums;
	txt.id = "pt" + npendulums;
	document.body.appendChild(txt);
	document.body.appendChild(p);

	txt = document.createElement("p");
	txt.innerHTML = "Amplitude:";
	txt.id = "at" + npendulums;
	var a = document.createElement("input");
	a.type = "number";
	a.value = 500;
	a.id = "A" + npendulums;
	document.body.appendChild(txt);
	document.body.appendChild(a);

	txt = document.createElement("p");
	txt.innerHTML = "Damping:";
	txt.id = "dt" + npendulums;
	var d = document.createElement("input");
	d.type = "number";
	d.value = 0.0003;
	d.id = "d" + npendulums;
	document.body.appendChild(txt);
	document.body.appendChild(d);


	txt = document.createElement("p");
	txt.innerHTML = "Frequency:";
	var f = document.createElement("input");
	f.type = "number";
	f.value = 0.1;
	f.id = "f" + npendulums;
	txt.id = "ft" + npendulums;
	document.body.appendChild(txt);
	document.body.appendChild(f);

	txt = document.createElement("p");
	txt.innerHTML = "Is it a Y pendulum? (you should have at least one X and Y pendulum) ";
	var xy = document.createElement("input");
	xy.type = "checkbox";
	xy.value = 1;
	xy.id = "xy" + npendulums;
	txt.id = "xyt" + npendulums;
	document.body.appendChild(txt);
	document.body.appendChild(xy);
	var br = document.createElement("br");
	br.id = "br" + npendulums;
	document.body.appendChild(br);


	txt = document.createTextNode("Delete this pendulum");
	var delBtn = document.createElement("button");
	delBtn.setAttribute("onclick", "deletePendulum(" + npendulums + ");");
	delBtn.appendChild(txt);
	delBtn.id = "db" + npendulums;
	document.body.appendChild(delBtn);
	npendulums++;
}

function deletePendulum(x)
{
	pendulums.splice(x, 1);
	document.body.removeChild(document.getElementById("p" +x));
	document.body.removeChild(document.getElementById("d" +x));
	document.body.removeChild(document.getElementById("f" +x));
	document.body.removeChild(document.getElementById("A" +x));
	document.body.removeChild(document.getElementById("xy" +x));

	document.body.removeChild(document.getElementById("pt" +x));
	document.body.removeChild(document.getElementById("dt" +x));
	document.body.removeChild(document.getElementById("ft" +x));
	document.body.removeChild(document.getElementById("at" +x));
	document.body.removeChild(document.getElementById("xyt" +x));

	document.body.removeChild(document.getElementById("db" +x));
	document.body.removeChild(document.getElementById("br" +x));

	for (var i = x+1; i < npendulums; i++)
	{
		document.getElementById("p"+i).id = "p"+(i-1);
		document.getElementById("d"+i).id = "d"+(i-1);
		document.getElementById("f"+i).id = "f"+(i-1);
		document.getElementById("A"+i).id = "A"+(i-1);
		document.getElementById("pt"+i).id = "pt"+(i-1);
		document.getElementById("dt"+i).id = "dt"+(i-1);
		document.getElementById("ft"+i).id = "ft"+(i-1);
		document.getElementById("at"+i).id = "at"+(i-1);
		document.getElementById("xy"+i).id = "xy"+(i-1);
		document.getElementById("xyt"+i).id = "xyt"+(i-1);
		document.getElementById("db"+i).id = "db"+(i-1);
		document.getElementById("db"+(i-1)).setAttribute("onclick", "deletePendulum(" + (i-1) + ");");
		document.getElementById("br"+i).id = "br"+(i-1);
	}
	npendulums--;
}


function start()
{
	started = true;
	createCanvas(800, 800);
	frameRate(1000);
	for (var i = 0; i < npendulums; i++)
	{
		var a = $("#A" + i).value;
		var f = $("#f" + i).value;
		var p = $("#p" + i).value;
		var d = $("#d" + i).value;
		var xy = $("#xy"+i).checked;
		new Pendulum(parseFloat(a), parseFloat(d), parseFloat(f), parseFloat(p), xy);
	}

}

function saveCanvas()
{
	save("AutoHarmonograph.png");
}

function draw()
{
	if(!started)
		return;
	translate(width/2, height/2);
	curr = calculate();
	if (t !== 0)
		line(pp[0], pp[1], curr[0], curr[1]);


	t++;
	pp = curr;
}