-
Notifications
You must be signed in to change notification settings - Fork 0
/
vector2.js
executable file
·273 lines (234 loc) · 5.91 KB
/
vector2.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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
"use strict";
// vector object for positions, for java use libgdx Vector2 ???
function Vector2(){
this.x=0;
this.y=0;
}
/*
set vector based on component values
*/
Vector2.prototype.setComponents=function(x,y){
this.x=x;
this.y=y;
return this;
}
/*
set vector based on another vector
*/
Vector2.prototype.set=function(v){
this.x=v.x;
this.y=v.y;
return this;
}
/*
scale the vector
*/
Vector2.prototype.scale=function(factor){
this.x*=factor;
this.y*=factor;
}
/*
set a vector to average of 2 other vectors
use for image smoothing
return the vector
*/
Vector2.prototype.average=function(one,two){
this.x=0.5*(one.x+two.x);
this.y=0.5*(one.y+two.y);
this.x=two.x;
this.y=two.y;
return this;
}
/*
symmetries
*/
/*
periodic length 1 in x-direction, basic zone is 0...1
return 0 if no change, 2 else
*/
Vector2.prototype.periodXUnit=function(){
var result=Math.floor(this.x);
this.x=this.x-result;
if (result==0){
return 0;
}
return 2;
}
/*
periodic length 1 in x-direction, basic zone is 0...1
*/
Vector2.prototype.periodYUnit=function(){
this.y=this.y-Math.floor(this.y);
}
/*
mirror from left to right if point is at right
*/
Vector2.prototype.leftToRightAt=function(x){
if (this.x>x){
this.x=x+x-this.x;
}
}
/*
mirror from top to bottom if point is above
Attention: inverted y-axis
*/
Vector2.prototype.bottomToTopAt=function(y){
if (this.y>y){
this.y=y+y-this.y;
}
}
/*
mirror at up going diagonal x=y if point is too high
Attention: inverted y-axis
*/
Vector2.prototype.upperLeftToLowerRightAt=function(centerX,centerY){
var dx=this.x-centerX;
var dy=this.y-centerY;
if (dy>dx){
this.x=centerX+dy;
this.y=centerY+dx;
}
}
/*
inversion at a circle
change vector only if point is inside the circle,
return true if inversion , false if point is outside
*/
Vector2.prototype.circleInversionInsideOut=function(centerX,centerY,radius){
var dx=this.x-centerX;
var dy=this.y-centerY;
var pointR2=dx*dx+dy*dy;
var circleR2=radius*radius;
var factor;
if (pointR2+0.0001>=circleR2){
return false;
}
else {
factor=circleR2/pointR2;
this.x=centerX+dx*factor;
this.y=centerY+dy*factor;
return true;
}
}
/*
inversion at a circle
change vector only if point is outside the circle,
return true if inversion , false if point is inside
*/
Vector2.prototype.circleInversionOutsideIn=function(centerX,centerY,radius){
var dx=this.x-centerX;
var dy=this.y-centerY;
var pointR2=dx*dx+dy*dy;
var circleR2=radius*radius;
var factor;
if (pointR2-0.0001<=circleR2){
return false;
}
else {
factor=circleR2/pointR2;
this.x=centerX+dx*factor;
this.y=centerY+dy*factor;
return true;
}
}
/*
check if a point is at left of a given line, looking from a to b
attention: inverted y-axis mirrors, left appears to be right
*/
Vector2.prototype.isAtLeftOfLine=function(ax,ay,bx,by){
return (bx-ax)*(this.y-ay)-(by-ay)*(this.x-ax)>0;
}
/*
check if point is inside a convex polygon
points as pair of coordinates
counterclockwise
attention: inverted y-axis mirrors, left appears to be right
argument is an array of coordinates, an arguments object with coordinates
or a list of coordinate values
*/
Vector2.prototype.isInsidePolygon=function(coordinates){
if (arguments.length>1){
return this.isInsidePolygon(arguments);
}
else {
var length=coordinates.length;
var lengthM3=length-3;
for (var i=0;i<lengthM3;i+=2){
if (!this.isAtLeftOfLine(coordinates[i],coordinates[i+1],coordinates[i+2],coordinates[i+3])){
return false;
}
}
if (!this.isAtLeftOfLine(coordinates[length-2],coordinates[length-1],coordinates[0],coordinates[1])){
return false;
}
return true;
}
}
// reducing the angle
Vector2.prototype.angle=function(){
return elementaryFastFunction.atan2(this.y,this.x);
}
Vector2.prototype.radius=function(){
return Math.sqrt(this.x*this.x+this.y*this.y);
}
Vector2.prototype.radius2=function(){
return this.x*this.x+this.y*this.y;
}
Vector2.prototype.setPolar=function(r,angle){
var index;
angle*=elementaryFastFunction.sinTabFactor;
index=Math.floor(angle);
angle-=index;
index=index&elementaryFastFunction.nSinIntervalsM1;
this.x=(elementaryFastFunction.cosTable[index]*(1-angle)+elementaryFastFunction.cosTable[index+1]*angle)*r;
this.y=(elementaryFastFunction.sinTable[index]*(1-angle)+elementaryFastFunction.sinTable[index+1]*angle)*r;
}
// make n-fold rotational symmetry with mirror symmetry
// returns 0 if no mapping, even number if rotation without mirror, 1 if mirror only
// by replicating the first sector phi in(0,PI/n)
Vector2.prototype.rotationMirrorSymmetry=function(n){
var angle=elementaryFastFunction.atan2(this.y,this.x);
var parity,r,index;
angle*=n*0.159154; // n/2pi
parity=Math.floor(angle);
angle=angle-parity;
if (parity!=0){
parity=2;
}
if (angle>0.5){
angle=1-angle;
parity=1;
}
angle*=6.28318/n;
r=Math.sqrt(this.x*this.x+this.y*this.y)
angle*=elementaryFastFunction.sinTabFactor;
index=Math.floor(angle);
angle-=index;
index=index&elementaryFastFunction.nSinIntervalsM1;
this.x=(elementaryFastFunction.cosTable[index]*(1-angle)+elementaryFastFunction.cosTable[index+1]*angle)*r;
this.y=(elementaryFastFunction.sinTable[index]*(1-angle)+elementaryFastFunction.sinTable[index+1]*angle)*r;
return parity;
}
// make n-fold rotational symmetry
// by replicating the first sector phi in(0,2PI/n)
Vector2.prototype.rotationSymmetry=function(n){
var angle=elementaryFastFunction.atan2(this.y,this.x);
var parity;
angle*=n*0.159154; // n/2pi
parity=Math.floor(angle);
angle=angle-parity;
if (parity!=0){
parity=2;
}
angle*=6.28318/n;
this.setPolar(this.radius(),angle);
return parity;
}
// make smooth n-fold rotational symmetry with mirror symmetry
Vector2.prototype.rotationMirrorSmooth=function(n){
var angle=this.angle();
angle*=n*0.159154; // n/2pi
angle=0.5*imageFastFunction.periodicMapping(angle);
angle*=6.28318/n;
this.setPolar(this.radius(),angle);
}