cione-comp-lib
Version:
`$ yarn add cione-comp-lib` 或者 `$ npm i cione-comp-lib -S`
303 lines (302 loc) • 7.23 kB
JavaScript
import vec2 from "../glmatrix/vec2.mjs";
var Vector2 = function(x, y) {
x = x || 0;
y = y || 0;
this.array = vec2.fromValues(x, y);
this._dirty = true;
};
Vector2.prototype = {
constructor: Vector2,
add: function(b) {
vec2.add(this.array, this.array, b.array);
this._dirty = true;
return this;
},
set: function(x, y) {
this.array[0] = x;
this.array[1] = y;
this._dirty = true;
return this;
},
setArray: function(arr) {
this.array[0] = arr[0];
this.array[1] = arr[1];
this._dirty = true;
return this;
},
clone: function() {
return new Vector2(this.x, this.y);
},
copy: function(b) {
vec2.copy(this.array, b.array);
this._dirty = true;
return this;
},
cross: function(out, b) {
vec2.cross(out.array, this.array, b.array);
out._dirty = true;
return this;
},
dist: function(b) {
return vec2.dist(this.array, b.array);
},
distance: function(b) {
return vec2.distance(this.array, b.array);
},
div: function(b) {
vec2.div(this.array, this.array, b.array);
this._dirty = true;
return this;
},
divide: function(b) {
vec2.divide(this.array, this.array, b.array);
this._dirty = true;
return this;
},
dot: function(b) {
return vec2.dot(this.array, b.array);
},
len: function() {
return vec2.len(this.array);
},
length: function() {
return vec2.length(this.array);
},
lerp: function(a, b, t) {
vec2.lerp(this.array, a.array, b.array, t);
this._dirty = true;
return this;
},
min: function(b) {
vec2.min(this.array, this.array, b.array);
this._dirty = true;
return this;
},
max: function(b) {
vec2.max(this.array, this.array, b.array);
this._dirty = true;
return this;
},
mul: function(b) {
vec2.mul(this.array, this.array, b.array);
this._dirty = true;
return this;
},
multiply: function(b) {
vec2.multiply(this.array, this.array, b.array);
this._dirty = true;
return this;
},
negate: function() {
vec2.negate(this.array, this.array);
this._dirty = true;
return this;
},
normalize: function() {
vec2.normalize(this.array, this.array);
this._dirty = true;
return this;
},
random: function(scale) {
vec2.random(this.array, scale);
this._dirty = true;
return this;
},
scale: function(s) {
vec2.scale(this.array, this.array, s);
this._dirty = true;
return this;
},
scaleAndAdd: function(b, s) {
vec2.scaleAndAdd(this.array, this.array, b.array, s);
this._dirty = true;
return this;
},
sqrDist: function(b) {
return vec2.sqrDist(this.array, b.array);
},
squaredDistance: function(b) {
return vec2.squaredDistance(this.array, b.array);
},
sqrLen: function() {
return vec2.sqrLen(this.array);
},
squaredLength: function() {
return vec2.squaredLength(this.array);
},
sub: function(b) {
vec2.sub(this.array, this.array, b.array);
this._dirty = true;
return this;
},
subtract: function(b) {
vec2.subtract(this.array, this.array, b.array);
this._dirty = true;
return this;
},
transformMat2: function(m) {
vec2.transformMat2(this.array, this.array, m.array);
this._dirty = true;
return this;
},
transformMat2d: function(m) {
vec2.transformMat2d(this.array, this.array, m.array);
this._dirty = true;
return this;
},
transformMat3: function(m) {
vec2.transformMat3(this.array, this.array, m.array);
this._dirty = true;
return this;
},
transformMat4: function(m) {
vec2.transformMat4(this.array, this.array, m.array);
this._dirty = true;
return this;
},
toString: function() {
return "[" + Array.prototype.join.call(this.array, ",") + "]";
},
toArray: function() {
return Array.prototype.slice.call(this.array);
}
};
if (Object.defineProperty) {
var proto = Vector2.prototype;
Object.defineProperty(proto, "x", {
get: function() {
return this.array[0];
},
set: function(value) {
this.array[0] = value;
this._dirty = true;
}
});
Object.defineProperty(proto, "y", {
get: function() {
return this.array[1];
},
set: function(value) {
this.array[1] = value;
this._dirty = true;
}
});
}
Vector2.add = function(out, a, b) {
vec2.add(out.array, a.array, b.array);
out._dirty = true;
return out;
};
Vector2.set = function(out, x, y) {
vec2.set(out.array, x, y);
out._dirty = true;
return out;
};
Vector2.copy = function(out, b) {
vec2.copy(out.array, b.array);
out._dirty = true;
return out;
};
Vector2.cross = function(out, a, b) {
vec2.cross(out.array, a.array, b.array);
out._dirty = true;
return out;
};
Vector2.dist = function(a, b) {
return vec2.distance(a.array, b.array);
};
Vector2.distance = Vector2.dist;
Vector2.div = function(out, a, b) {
vec2.divide(out.array, a.array, b.array);
out._dirty = true;
return out;
};
Vector2.divide = Vector2.div;
Vector2.dot = function(a, b) {
return vec2.dot(a.array, b.array);
};
Vector2.len = function(b) {
return vec2.length(b.array);
};
Vector2.lerp = function(out, a, b, t) {
vec2.lerp(out.array, a.array, b.array, t);
out._dirty = true;
return out;
};
Vector2.min = function(out, a, b) {
vec2.min(out.array, a.array, b.array);
out._dirty = true;
return out;
};
Vector2.max = function(out, a, b) {
vec2.max(out.array, a.array, b.array);
out._dirty = true;
return out;
};
Vector2.mul = function(out, a, b) {
vec2.multiply(out.array, a.array, b.array);
out._dirty = true;
return out;
};
Vector2.multiply = Vector2.mul;
Vector2.negate = function(out, a) {
vec2.negate(out.array, a.array);
out._dirty = true;
return out;
};
Vector2.normalize = function(out, a) {
vec2.normalize(out.array, a.array);
out._dirty = true;
return out;
};
Vector2.random = function(out, scale) {
vec2.random(out.array, scale);
out._dirty = true;
return out;
};
Vector2.scale = function(out, a, scale) {
vec2.scale(out.array, a.array, scale);
out._dirty = true;
return out;
};
Vector2.scaleAndAdd = function(out, a, b, scale) {
vec2.scaleAndAdd(out.array, a.array, b.array, scale);
out._dirty = true;
return out;
};
Vector2.sqrDist = function(a, b) {
return vec2.sqrDist(a.array, b.array);
};
Vector2.squaredDistance = Vector2.sqrDist;
Vector2.sqrLen = function(a) {
return vec2.sqrLen(a.array);
};
Vector2.squaredLength = Vector2.sqrLen;
Vector2.sub = function(out, a, b) {
vec2.subtract(out.array, a.array, b.array);
out._dirty = true;
return out;
};
Vector2.subtract = Vector2.sub;
Vector2.transformMat2 = function(out, a, m) {
vec2.transformMat2(out.array, a.array, m.array);
out._dirty = true;
return out;
};
Vector2.transformMat2d = function(out, a, m) {
vec2.transformMat2d(out.array, a.array, m.array);
out._dirty = true;
return out;
};
Vector2.transformMat3 = function(out, a, m) {
vec2.transformMat3(out.array, a.array, m.array);
out._dirty = true;
return out;
};
Vector2.transformMat4 = function(out, a, m) {
vec2.transformMat4(out.array, a.array, m.array);
out._dirty = true;
return out;
};
var Vector2$1 = Vector2;
export { Vector2$1 as default };