-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmath.js
executable file
·156 lines (135 loc) · 3.27 KB
/
math.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
var rad2deg = 57.2958;
var MAX_RADS = Math.PI*2;
function vec(x,y) {
this.x = x||0;
this.y = y||0;
this.magnitude = function()
{
return Math.sqrt(this.x*this.x + this.y*this.y);
}
this.mag_sq = function()
{
return this.x*this.x + this.y*this.y;
}
this.normalize = function()
{
var mag = this.magnitude();
return new vec(this.x/mag, this.y/mag);
}
this.abs = function() {
return new vec(Math.abs(this.x), Math.abs(this.y));
}
this.add = function(other)
{
return new vec(this.x+other.x, this.y+other.y);
}
this.subtract = function(other)
{
return new vec(this.x-other.x, this.y-other.y);
}
this.scale = function(scalar)
{
return new vec(this.x*scalar, this.y*scalar);
}
this.scale_by_vec = function(v) {
return new vec(this.x*v.x, this.y*v.y);
}
this.perpendicular = function()
{
return new vec(this.y, this.x);
}
this.angle = function()
{
return Math.atan2(this.y,this.x);
}
this.rotate_by = function(by_ang)
{
var new_ang = this.angle() + by_ang;
var normal = ang2normal(new_ang);
return normal.scale(this.magnitude());
}
this.rotate_around = function(origin_vec, by_ang) {
var rel = this.subtract(origin_vec);
rel = rel.rotate_by(by_ang);
return origin_vec.add(rel);
}
this.scale_around = function(origin_vec, scale_vec) {
var rel = this.subtract(origin_vec);
rel = rel.scale_by_vec(scale_vec);
return origin_vec.add(rel);
}
this.compare = function(other) {
return this.x==other.x && this.y==other.y;
}
this.set_equal_to = function(other_vec) {
this.x = other_vec.x;
this.y = other_vec.y;
}
this.dist = function(other_vec) {
return this.subtract(other_vec).magnitude();
}
}
function copy_vec(v) {
return new vec(v.x||0, v.y||0);
}
function ang2normal(ang) {
var x = Math.cos( ang );
var y = Math.sin( ang );
return new vec(x, y);
}
function round_to_place(num, num_places) {
var factor = Math.pow(10,num_places);
return Math.round(num*factor)/factor;
}
function float2str(num, num_places) {
var rounded = round_to_place(num, num_places);
var sign = rounded<0 ? -1:1;
var fraction = Math.abs(rounded%1);
rounded = Math.floor( Math.abs(rounded) );
var num_str = rounded + "";
while(fraction%1 != 0)
fraction *= 10;
var fract_str = fraction + "";
fract_str = fract_str.substring(0,num_places);
while(fract_str.length < num_places) {
fract_str += "0";
}
var full_str = num_str;
if(fract_str.length > 0)
full_str += "." + fract_str;
if(sign == -1)
full_str = "-" + full_str;
return full_str;
}
//Mirror an angle vertically
function mirror_angle_v(ang) {
return -ang;
}
//Mirror an angle horizontally
function mirror_angle_h(ang) {
return -ang + Math.PI;
}
function normalize_ang(ang) {
ang %= MAX_RADS;
return ang < 0 ? ang+MAX_RADS : ang;
}
function make_ang_small(ang) {
ang = normalize_ang(ang);
if(ang > Math.PI)
ang = ang-MAX_RADS;
return ang;
}
// Find the smallest possible distance between 2 angles
function find_angle_difference(to, from) {
to = normalize_ang(to);
from = normalize_ang(from);
// simply take to-from, this will be correct, unless something like
// to=359 from=0 to-from = 359, we want -1
var dist1 = to-from;
if(to>from)
from += MAX_RADS;
else if(from>to)
to += MAX_RADS;
var dist2 = to-from;
return Math.abs(dist2) < Math.abs(dist1) ? dist2 : dist1;
}