-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbernoulli.js
54 lines (44 loc) · 1.35 KB
/
bernoulli.js
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
'use strict';
var ok = require('assert').ok;
var cos = Math.cos;
var sin = Math.sin;
function Bernoulli(opts) {
ok(!!opts, 'opts required');
ok(!!opts.size, '.size required');
ok(!!opts.center, '.center required');
ok(!!opts.center.x, '.center.x required');
ok(!!opts.center.y, '.center.y required');
this.size = opts.size;
this.theta = opts.theta || 1.2;
this.direction = opts.direction || 'positive';
this.directionFn = getDirection(this.direction);
this.velocity = opts.velocity || .08;
this.center = opts.center;
}
function getDirection(dir) {
if (dir === 'positive') {
return function positive(t, vel) { return t += vel; };
}
return function negative(t, vel) { return t -= vel; };
}
function square(val) { return Math.pow(val, 2); }
function bernoulli(theta, size) {
var scale = size / (square(sin(theta), 2) + 1);
var x = (scale * cos(theta)) + this.center.x;
var y = (scale * sin(2 * theta) / 2) + this.center.y;
return [x, y];
}
var template = {
bernoulli: bernoulli,
setDirection: function setDirection(direction) {
this.direction = direction;
this.directionFn = getDirection(this.direction);
},
update: function update() {
this.theta = this.directionFn(this.theta, this.velocity);
return this;
}
};
Bernoulli.prototype = template;
module.exports = Bernoulli;
module.exports.template = template;