-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutilities.js
148 lines (136 loc) · 3.8 KB
/
utilities.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
function get_window_size() {
var docEl = document.documentElement,
IS_BODY_ACTING_ROOT = docEl && docEl.clientHeight === 0;
// Used to feature test Opera returning wrong values
// for documentElement.clientHeight.
function isDocumentElementHeightOff () {
var d = document,
div = d.createElement('div');
div.style.height = "2500px";
d.body.insertBefore(div, d.body.firstChild);
var r = d.documentElement.clientHeight > 2400;
d.body.removeChild(div);
return r;
}
if (typeof document.clientWidth == "number") {
return { width: document.clientWidth, height: document.clientHeight };
} else if (IS_BODY_ACTING_ROOT || isDocumentElementHeightOff()) {
var b = document.body;
return { width: b.clientWidth, height: b.clientHeight };
} else {
return { width: docEl.clientWidth, height: docEl.clientHeight };
};
};
function random_color() {
return new vec3(0.1 + 0.9*Math.random(), 0.1 + 0.9*Math.random(), 0.1 + 0.9*Math.random());
}
class vec2 {
constructor(x, y) {
if(x === undefined) {
this.x = 0;
this.y = 0;
} else if(y === undefined) {
this.x = x;
this.y = x;
} else {
this.x = x;
this.y = y;
}
}
add(v) {
return new vec2(this.x+v.x, this.y+v.y);
}
subtract(v) {
return new vec2(this.x-v.x, this.y-v.y);
}
multiply(v) {
return new vec2(this.x*v.x, this.y*v.y);
}
divide(v) {
return new vec2(this.x/v.x, this.y/v.y);
}
scalar_add(x) {
return new vec2(this.x+x, this.y+x);
}
scalar_subtract(x) {
return new vec2(this.x-x, this.y-x);
}
scalar_multiply(x) {
return new vec2(this.x*x, this.y*x);
}
scalar_divide(x) {
return new vec2(this.x/x, this.y/x);
}
dot(v) {
return this.x*v.x + this.y*v.y;
}
length() {
return Math.sqrt(this.x*this.x + this.y*this.y);
}
normalize() {
return this.scalar_divide(this.length());
}
toString() {
return "x: " + this.x.toString() + " y: " + this.y.toString();
}
}
class vec3 {
constructor(x, y, z) {
if(x === undefined) {
this.x = 0;
this.y = 0;
this.z = 0;
} else if(y === undefined) {
this.x = x;
this.y = x;
this.z = x;
} else if(z === undefined) {
throw 'vec3 needs 0, 1 or 3 parameters';
} else {
this.x = x;
this.y = y;
this.z = z;
}
}
add(v) {
return new vec2(this.x+v.x, this.y+v.y, this.z+v.z);
}
subtract(v) {
return new vec2(this.x-v.x, this.y-v.y, this.z-v.z);
}
multiply(v) {
return new vec2(this.x*v.x, this.y*v.y, this.z*v.z);
}
divide(v) {
return new vec2(this.x/v.x, this.y/v.y, this.z/v.z);
}
scalar_add(x) {
return new vec2(this.x+x, this.y+x, this.z+x);
}
scalar_subtract(x) {
return new vec2(this.x-x, this.y-x, this.z-x);
}
scalar_multiply(x) {
return new vec2(this.x*x, this.y*x, this.z*x);
}
scalar_divide(x) {
return new vec2(this.x/x, this.y/x, this.z/x);
}
dot(v) {
return this.x*v.x + this.y*v.y + this.z*v.z;
}
cross(v) {
return new vec3(this.y*v.z - this.z*v.y,
this.z*v.x - this.a*v.z,
this.x*v.y - this.y*v.x);
}
length() {
return Math.sqrt(this.x*this.x + this.y*this.y + this.z*this.z);
}
normalize() {
return this.scalar_divide(this.length());
}
toString() {
return "x: " + this.x.toString() + " y: " + this.y.toString() + " z: " + this.z.toString();
}
}