Matrix and Rectangle modules

This commit is contained in:
Tim Mickel 2016-01-13 17:34:02 -05:00
parent fdd74a411c
commit 535e61f628
2 changed files with 214 additions and 214 deletions

View file

@ -2,65 +2,66 @@
// Basic Matrix
////////////////////////////////////////
var Matrix = function () {
export default class Matrix {
constructor () {
this.a = 1;
this.b = 0;
this.c = 0;
this.d = 1;
this.e = 0;
this.f = 0;
};
}
Matrix.prototype.identity = function () {
identity () {
this.a = 1;
this.b = 0;
this.c = 0;
this.d = 1;
this.e = 0;
this.f = 0;
};
}
Matrix.prototype.setMatrix = function (mtx) { // webKitMtrx
setMatrix (mtx) { // webKitMtrx
this.a = mtx.a;
this.b = mtx.b;
this.c = mtx.c;
this.d = mtx.d;
this.e = mtx.e;
this.f = mtx.f;
};
}
Matrix.prototype.isIdentity = function () {
isIdentity () {
return (this.a == 1 && this.b == 0 && this.c == 0 && this.d == 1 && this.e == 0 && this.f == 0);
};
}
Matrix.prototype.rotate = function (angle) {
rotate (angle) {
var cos = Math.cos(angle * Math.PI / 180);
var sin = Math.sin(angle * Math.PI / 180);
this.a = cos;
this.b = sin;
this.c = -sin;
this.d = cos;
};
}
Matrix.prototype.scale = function (scalex, scaley) {
scale (scalex, scaley) {
this.a = scalex;
this.d = scaley ? scaley : scalex;
};
}
Matrix.prototype.translate = function (dx, dy) {
translate (dx, dy) {
this.e = dx;
this.f = dy;
};
}
Matrix.prototype.transformPoint = function (pt) {
transformPoint (pt) {
return {
x: this.a * pt.x + this.c * pt.y + this.e,
y: this.b * pt.x + this.d * pt.y + this.f
};
};
}
Matrix.prototype.multiply = function (m2) {
multiply (m2) {
var zero = 1e-14;
var m = new Matrix();
m.a = this.a * m2.a + this.c * m2.b;
@ -88,4 +89,5 @@ Matrix.prototype.multiply = function (m2) {
m.f = 0;
}
return m;
};
}
}

View file

@ -1,15 +1,12 @@
////////////////////////////////////////
// Basic Matrix
////////////////////////////////////////
var Rectangle = function (x, y, w, h) {
export default class Rectangle {
constructor (x, y, w, h) {
this.x = x;
this.y = y;
this.width = w;
this.height = h;
};
}
Rectangle.prototype.hitRect = function (pt) {
hitRect (pt) {
var x = pt.x;
var y = pt.y;
if (x < this.x) {
@ -25,9 +22,9 @@ Rectangle.prototype.hitRect = function (pt) {
return false;
}
return true;
};
}
Rectangle.prototype.intersects = function (r) {
intersects (r) {
var x0 = Math.max(this.x, r.x);
var x1 = Math.min(this.x + this.width, r.x + r.width);
if (x0 <= x1) {
@ -38,13 +35,13 @@ Rectangle.prototype.intersects = function (r) {
}
}
return false;
};
}
Rectangle.prototype.overlapElemBy = function (box2, percent) {
overlapElemBy (box2, percent) {
return this.overlapElem(box2) >= percent;
};
}
Rectangle.prototype.overlapElem = function (box2) {
overlapElem (box2) {
var boxi = this.intersection(box2);
if (boxi.isEmpty()) {
return 0;
@ -56,9 +53,9 @@ Rectangle.prototype.overlapElem = function (box2) {
return 1;
}
return (boxi.width * boxi.height) / (box2.width * box2.height);
};
}
Rectangle.prototype.intersection = function (box2) {
intersection (box2) {
var dx = Math.max(this.x, box2.x);
var dw = Math.min(this.x + this.width, box2.x + box2.width);
if (dx <= dw) {
@ -70,9 +67,9 @@ Rectangle.prototype.intersection = function (box2) {
return new Rectangle(dx, dy, dw - dx, dh - dy);
}
return new Rectangle(0, 0, 0, 0);
};
}
Rectangle.prototype.union = function (box2) {
union (box2) {
var box = new Rectangle(0, 0, 0, 0);
box.x = (this.x < box2.x) ? this.x : box2.x;
box.y = (this.y < box2.y) ? this.y : box2.y;
@ -93,17 +90,17 @@ Rectangle.prototype.union = function (box2) {
};
}
return box;
};
}
Rectangle.prototype.expandBy = function (sw) {
expandBy (sw) {
this.x -= sw / 2;
this.y -= sw / 2;
this.width += sw;
this.height += sw;
return this;
};
}
Rectangle.prototype.crop = function (box) {
crop (box) {
if (this.x < box.x) {
this.x = box.x;
}
@ -116,33 +113,34 @@ Rectangle.prototype.crop = function (box) {
if ((this.height + this.y) > (box.height + box.y)) {
this.height += ((box.height + box.y) - (this.height + this.y));
}
};
}
Rectangle.prototype.getArea = function () {
getArea () {
return this.width * this.height;
};
}
Rectangle.prototype.rounded = function () {
rounded () {
return new Rectangle(
Math.floor(this.x),
Math.floor(this.y),
Math.round(this.width) + 1,
Math.round(this.height) + 1
);
};
}
Rectangle.prototype.isEqual = function (box2) {
isEqual (box2) {
return (this.x == box2.x) && (this.y == box2.y) &&
(this.width == box2.width) && (this.height == box2.height);
};
}
Rectangle.prototype.isEmpty = function () {
isEmpty () {
return (this.x == 0) && (this.y == 0) && (this.width == 0) && (this.height == 0);
};
}
Rectangle.prototype.scale = function (sx, sy) {
scale (sx, sy) {
this.x *= sx;
this.y *= sy;
this.width *= sx;
this.height *= sy;
};
}
}