-
Notifications
You must be signed in to change notification settings - Fork 0
/
hammerlib.js
122 lines (90 loc) · 2.97 KB
/
hammerlib.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
"use strict";
(function() {
var floor=Math.floor
var ceil=Math.ceil
var abs=Math.abs
var radians = function(degrees) {
return degrees * Math.PI / 180
}
class Vector {
constructor(x, y, z, sbrackets) {
this.x = parseFloat(x) || 0
this.y = parseFloat(y) || 0
this.z = parseFloat(z) || 0
this.sbrackets = sbrackets
}
add(v) {
if (v instanceof Vector) return new Vector(this.x + v.x, this.y + v.y, this.z + v.z, this.sbrackets)
else return new Vector(this.x + v, this.y + v, this.z + v, this.sbrackets)
}
subtract(v) {
if (v instanceof Vector) return new Vector(this.x - v.x, this.y - v.y, this.z - v.z, this.sbrackets)
else return new Vector(this.x - v, this.y - v, this.z - v, this.sbrackets)
}
multiply(v) {
if (v instanceof Vector) return new Vector(this.x * v.x, this.y * v.y, this.z * v.z, this.sbrackets)
else return new Vector(this.x * v, this.y * v, this.z * v, this.sbrackets)
}
divide(v) {
if (v instanceof Vector) return new Vector(this.x / v.x, this.y / v.y, this.z / v.z, this.sbrackets)
else return new Vector(this.x / v, this.y / v, this.z / v, this.sbrackets)
}
dot(v) {
return this.x * v.x + this.y * v.y + this.z * v.z;
}
distance(v) {
return Math.sqrt(Math.pow(this.x - v.x,2) + Math.pow(this.y - v.y,2) + Math.pow(this.z - v.z,2));
}
length() {
return Math.sqrt(this.dot(this));
}
equal(v) {
return this.x == v.x && this.y == v.y && this.z == v.z
}
floor() {
return new Vector(floor(this.x),floor(this.y),floor(this.z), this.sbrackets)
}
ceil() {
return new Vector(ceil(this.x),ceil(this.y),ceil(this.z), this.sbrackets)
}
abs() {
return new Vector(abs(this.x),abs(this.y),abs(this.z), this.sbrackets)
}
toString(){
return (this.sbrackets ? '(' : '')+this.x+' '+this.y+' '+this.z+(this.sbrackets ? ')' : '')
}
fix(){
return new Vector(this.x.toFixed(4),this.y.toFixed(4),this.z.toFixed(4))
}
rotateX(a){
a = radians(a)
return new Vector(this.x,(this.y*Math.cos(a)-(this.z*Math.sin(a))).toFixed(4),(this.y*Math.sin(a)+this.z*Math.cos(a)).toFixed(4), this.sbrackets)
}
rotateY(a){
a = radians(a)
return new Vector((this.x*Math.cos(a)+(this.z*Math.sin(a))).toFixed(4),this.y,(-this.x*Math.sin(a)+this.z*Math.cos(a)).toFixed(4), this.sbrackets)
}
rotateZ(a){
a = radians(a)
return new Vector((this.x*Math.cos(a)-(this.y*Math.sin(a))).toFixed(4),(this.x*Math.sin(a)+this.y*Math.cos(a)).toFixed(4),this.z, this.sbrackets)
}
}
module.exports.Vector = Vector
class UVMap {
constructor (x, y, z, shift, scale) {
this.x = parseFloat(x) || 0
this.y = parseFloat(y) || 0
this.z = parseFloat(z) || 0
this.shift = parseFloat(shift) || 0
this.scale = parseFloat(scale) || 0
}
rotateZ(a){
a = radians(a)
return new UVMap((this.x*Math.cos(a)-(this.y*Math.sin(a))).toFixed(4),(this.x*Math.sin(a)+this.y*Math.cos(a)).toFixed(4),this.z, this.shift, this.scale)
}
toString(){
return '['+this.x+' '+this.y+' '+this.z+' '+this.shift+'] '+this.scale
}
}
module.exports.UVMap = UVMap
})()