cione-comp-lib
Version:
`$ yarn add cione-comp-lib` 或者 `$ npm i cione-comp-lib -S`
131 lines (130 loc) • 3.2 kB
JavaScript
var Point = function() {
function Point2(x, y) {
this.x = x || 0;
this.y = y || 0;
}
Point2.prototype.copy = function(other) {
this.x = other.x;
this.y = other.y;
return this;
};
Point2.prototype.clone = function() {
return new Point2(this.x, this.y);
};
Point2.prototype.set = function(x, y) {
this.x = x;
this.y = y;
return this;
};
Point2.prototype.equal = function(other) {
return other.x === this.x && other.y === this.y;
};
Point2.prototype.add = function(other) {
this.x += other.x;
this.y += other.y;
return this;
};
Point2.prototype.scale = function(scalar) {
this.x *= scalar;
this.y *= scalar;
};
Point2.prototype.scaleAndAdd = function(other, scalar) {
this.x += other.x * scalar;
this.y += other.y * scalar;
};
Point2.prototype.sub = function(other) {
this.x -= other.x;
this.y -= other.y;
return this;
};
Point2.prototype.dot = function(other) {
return this.x * other.x + this.y * other.y;
};
Point2.prototype.len = function() {
return Math.sqrt(this.x * this.x + this.y * this.y);
};
Point2.prototype.lenSquare = function() {
return this.x * this.x + this.y * this.y;
};
Point2.prototype.normalize = function() {
var len = this.len();
this.x /= len;
this.y /= len;
return this;
};
Point2.prototype.distance = function(other) {
var dx = this.x - other.x;
var dy = this.y - other.y;
return Math.sqrt(dx * dx + dy * dy);
};
Point2.prototype.distanceSquare = function(other) {
var dx = this.x - other.x;
var dy = this.y - other.y;
return dx * dx + dy * dy;
};
Point2.prototype.negate = function() {
this.x = -this.x;
this.y = -this.y;
return this;
};
Point2.prototype.transform = function(m) {
if (!m) {
return;
}
var x = this.x;
var y = this.y;
this.x = m[0] * x + m[2] * y + m[4];
this.y = m[1] * x + m[3] * y + m[5];
return this;
};
Point2.prototype.toArray = function(out) {
out[0] = this.x;
out[1] = this.y;
return out;
};
Point2.prototype.fromArray = function(input) {
this.x = input[0];
this.y = input[1];
};
Point2.set = function(p, x, y) {
p.x = x;
p.y = y;
};
Point2.copy = function(p, p2) {
p.x = p2.x;
p.y = p2.y;
};
Point2.len = function(p) {
return Math.sqrt(p.x * p.x + p.y * p.y);
};
Point2.lenSquare = function(p) {
return p.x * p.x + p.y * p.y;
};
Point2.dot = function(p0, p1) {
return p0.x * p1.x + p0.y * p1.y;
};
Point2.add = function(out, p0, p1) {
out.x = p0.x + p1.x;
out.y = p0.y + p1.y;
};
Point2.sub = function(out, p0, p1) {
out.x = p0.x - p1.x;
out.y = p0.y - p1.y;
};
Point2.scale = function(out, p0, scalar) {
out.x = p0.x * scalar;
out.y = p0.y * scalar;
};
Point2.scaleAndAdd = function(out, p0, p1, scalar) {
out.x = p0.x + p1.x * scalar;
out.y = p0.y + p1.y * scalar;
};
Point2.lerp = function(out, p0, p1, t) {
var onet = 1 - t;
out.x = onet * p0.x + t * p1.x;
out.y = onet * p0.y + t * p1.y;
};
return Point2;
}();
var Point$1 = Point;
export { Point$1 as default };