-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathcenter.js
111 lines (100 loc) · 3.62 KB
/
center.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
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
/*
* Copyright (c) 2013, Javier Ayres
*
* This file is part of Lona.
*
* Lona is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Lona is distributed in the hope that it will
* be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* Lona. If not, see <http://www.gnu.org/licenses/>.
*/
function Center(x, y, direction, startRot, endRot) {
this.x = x;
this.y = y;
this.direction = direction;
this.startRot = startRot;
this.endRot = endRot;
this.growth = 0;
this.moveStart = function(rotDiff) {
if (this.direction > 0) {
this.endRot += rotDiff;
} else {
this.startRot -= rotDiff;
}
}
this.moveEnd = function(rotDiff) {
if (this.growth > 0) {
var tempGrowth = this.growth;
this.growth -= rotDiff;
rotDiff -= tempGrowth;
}
if (rotDiff > 0) {
if (this.direction > 0) {
this.startRot += rotDiff;
} else {
this.endRot -= rotDiff;
}
}
return this.startRot - this.endRot;
}
this.draw = function() {
ctx.beginPath();
ctx.arc(this.x, this.y, RADIUS, this.startRot, this.endRot, false);
ctx.stroke();
}
this.mustShift = function() {
return this.startRot >= this.endRot;
}
this.collide = function(position) {
var distance = Math.sqrt(Math.pow(position[0] - this.x, 2) + Math.pow(position[1] - this.y, 2));
if (distance >= RADIUS - LINEAR_WIDTH && distance <= RADIUS + LINEAR_WIDTH) {
var angle = Math.acos((position[0] - this.x) / distance);
if (this.y > position[1]) {
angle = 2 * Math.PI - angle;
}
nStartRot = normalizeAngle(this.startRot - CIRCULAR_WIDTH);
nEndRot = normalizeAngle(this.endRot + CIRCULAR_WIDTH);
if (nStartRot < nEndRot) {
return angle >= nStartRot && angle <= nEndRot;
} else {
return !(angle >= nEndRot && angle <= nStartRot);
}
} else {
return false;
}
}
this.collideSelf = function() {
return this.endRot - this.startRot >= 2 * Math.PI - 2 * CIRCULAR_WIDTH;
}
this.getEndPosition = function() {
if (this.direction > 0) {
return [this.x + Math.cos(this.endRot) * RADIUS, this.y + Math.sin(this.endRot) * RADIUS];
} else {
return [this.x + Math.cos(this.startRot) * RADIUS, this.y + Math.sin(this.startRot) * RADIUS];
}
}
this.getLength = function() {
return Math.abs(this.endRot - this.startRot);
}
this.isOffBorders = function() {
var endPosition = this.getEndPosition();
var distanceF1 = Math.sqrt(Math.pow(endPosition[0] - FOCI_X, 2) + Math.pow(endPosition[1] - FOCUS_1_Y, 2));
var distanceF2 = Math.sqrt(Math.pow(endPosition[0] - FOCI_X, 2) + Math.pow(endPosition[1] - FOCUS_2_Y, 2));
return distanceF1 + distanceF2 >= SCREEN_HEIGHT - LINEAR_WIDTH;
}
this.grow = function() {
if (this.growth <= 0) {
this.growth = DISTANCE;
} else {
this.growth += DISTANCE;
}
}
}