-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmath_functions.js
More file actions
118 lines (99 loc) · 2.26 KB
/
math_functions.js
File metadata and controls
118 lines (99 loc) · 2.26 KB
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
function lerp2(a, b, x)
{
return a + x * (b - a);
}
/*
R1 = (1 - fu) * dot[0] + fu * dot[1];
R2 = (1-fu) * dot[2]+ fu * dot[3];
P = (1-fv) * R1 + fv * R2;*/
function lerp(a, b, x)
{
return (1-x)* a + x * b;
}
function fade5t(t)
{
return t*t*t*(t*(t*6-15)+10);
}
function fade3t(t)
{
return 3*t*t - 2*t*t*t;
}
function fade32t(t)
{
return (3 - 2 * t) * t * t;
}
function Vector(i, j, k)
{
this.i = i;
this.j = j;
this.k = k;
this.show = function()
{
console.log("i:" + this.i.toFixed(2) +
", j:" + this.j.toFixed(2) + ", k:" + this.k.toFixed(2));
}
}
function Vector_2D(i, j)
{
this.i = i;
this.j = j;
this.show = function()
{
console.log("i:" + this.i.toFixed(2) +
", j:" + this.j.toFixed(2));
}
}
function rndInt(min, max)
{
return Math.floor(Math.random() * (max - min)) + min;
}
function rndFloat()
{
return Math.random() *2-1;
}
function dotProduct (v1, v2)
{
return v1.i * v2.i + v1.j * v2.j + v1.k * v2.k ;
}
function dotProduct_2D (v1, v2)
{
return v1.i * v2.i + v1.j * v2.j ;
}
function distance(p1, p2)
{
return Math.sqrt( Math.pow(p1.x - p2.x , 2) + Math.pow(p1.y - p2.y , 2)) ;
}
function rotateGVector_yaw(vec, degrees) // rotate in chair, about y axis (up), xz plane altered
{
var phi = degrees * rad;
var tmpx = vec.i, tmpz = vec.k;
vec.i = tmpx * Math.cos(phi) - tmpz * Math.sin(phi);
vec.k = tmpx * Math.sin(phi) + tmpz * Math.cos(phi);
return vec;
}
function rotateGVector_pitch(vec, degrees) // pull up in plane, about x axis (side) yz plane affected
{
var theta = degrees * rad;
var tmpy = vec.j, tmpz = vec.k;
vec.j = tmpy * Math.cos(theta) - tmpz * Math.sin(theta);
vec.k = tmpy * Math.sin(theta) + tmpz * Math.cos(theta);
return vec;
}
function rotateGVector_roll(vec, degrees) // bank left / right in car, about z axis (forward) xy plane affected
{
var psi = degrees * rad;
var tmpx = vec.i, tmpy = vec.j;
vec.i = tmpx * Math.cos(psi) - tmpy * Math.sin(psi);
vec.j = tmpx * Math.sin(psi) + tmpy * Math.cos(psi);
return vec;
}
function rotateGradientVector(vector, angle)
{
return rotateGVector_roll(vector, angle); ;
}
function changeVectorAngles(angle)
{
for(y=0; y<gridSize; y++)
for(x=0; x<gridSize; x++)
gridOfValues[x][y] = rotateGradientVector(gridOfValues[x][y], angle);
}