forked from MattStultz/extrusiongen
-
Notifications
You must be signed in to change notification settings - Fork 1
/
IKRS.Utils.js
66 lines (45 loc) · 1.55 KB
/
IKRS.Utils.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
/**
* @author Ikaros Kappler
* @date 2013-08-21
* @version 1.0.0
**/
IKRS.Utils = {};
IKRS.Utils.inArray = function( arr, x ) {
for( var i = 0; i < arr.length; i++ ) {
if( arr[i] == x )
return true;
}
return false;
};
IKRS.Utils.rotateVectorAroundZ = function( vector, axis, angle ) {
// In Java it would look like this, where 'vector' is 'this':
// double dX = center.getX()-this.x,
// dY = center.getY()-this.y;
// double pointAngle = calculateAngle(dX, dY);
// double radius = Math.sqrt( dX*dX + dY*dY );
// this.x = center.getX()+radius*Math.cos( angle + pointAngle );
// this.y = center.getY()+radius*Math.sin( angle + pointAngle );
var dX = axis.x - vector.x;
var dY = axis.y - vector.y;
var pointAngle = IKRS.Utils.calculateAngle( dX, dY );
var radius = Math.sqrt( dX*dX + dY*dY );
vector.x = axis.x + radius*Math.cos( angle + pointAngle );
vector.y = axis.x + radius*Math.sin( angle + pointAngle );
return vector;
};
IKRS.Utils.calculateAngle = function( distA, distB ) {
var pointAngle = 0.0;
if( distA == 0 ) {
if( distB < 0 ) pointAngle = -Math.PI/2.0;
else pointAngle = Math.PI/2.0; //0.0;
} else pointAngle = Math.atan(distB/distA);
if( distA >= 0 )
pointAngle = Math.PI+pointAngle;
return pointAngle;
};
IKRS.Utils.isNumeric = function(n) {
return /^-?[\d.]+(?:e-?\d+)?$/.test(n);
};
IKRS.Utils.isHexadecimal = function( str ) {
return /^[0-9A-F]*$/i.test(str);
};