/**
*
* @author mi_kuncoro@yahoo.co.id
* @include k.js
*
*/
if (!K) alert("You must include k.js in HTMLElement");
/*
One degree in radian
*/
K.P1 = Math.PI / 180;
/*
* Convert radian to degree
*/
K.toDegree = function(n) {
return n * K.P1;
};
/*
* Convert degree to radian
*/
K.toRadian = function(n) {
return n / K.P1
};
/*
** Point 2D
*/
K.Point = function(xx, yy) {
this.x = xx || 0;
this.y = yy || 0;
};
K.Point.prototype = {
constructor:K.Point,
x:0, y:0,
toAngle:function() { return Math.atan2(this.y, this.x); },
angleTo:function(x, y) { return Math.atan2(y - this.y, x - this.x); },
getLength:function() { return Math.sqrt(this.x * this.x + this.y * this.y); },
add:function(x, y) { this.x += x; this.y += y; return this; },
clone:function() { return new Point(this.x, this.y); },
dispose:function() {},
distanceTo:function(x, y) {
var dx = x - this.x;
var dy = y - this.y;
return Math.sqrt(dx * dx + dy * dy);
},
subtract:function(x, y) { this.x -= x; this.y -= y; return this; },
multiply:function(v) { this.x *= v; this.y *= v; return this; },
normalize:function() {
var d = this.length;
if (d > 0 || d != 1){
this.x /= d;
this.y /= d;
}
return this;
},
toString:function() {
return "[Point (x="+this.x+" y="+this.y+")]";
}
};
K.Point.addPoint = function(p1, p2) {
return new K.Point(p1.x + p2.x, p1.y + p2.y);
};
K.Point.anglePoint = function(p1, p2) {
var dx = p1.x - p1.x;
var dy = p2.y - p1.y;
return Math.atan2(dy, dx);
};
K.Point.clonePoint = function(p) {
return new K.Point(p.x, p.y);
};
K.Point.distancePoint = function(p1, p2) {
var dx = p1.x - p1.x;
var dy = p2.y - p1.y;
return Math.sqrt(dx * dx + dy * dy);
};
K.Point.multiplyPoint = function(p, n) {
return new K.Point(p.x * n, p.y * n);
};
K.Point.normalizePoint = function(p) {
var d = Math.sqrt(p.x * p.x + p.y * p.y);
if (d > 1)
{
p.x /= d;
p.y /= d;
}
};
K.Point.subtractPoint = function(p1, p2) {
return new K.Point(p1.x - p2.x, p1.y - p2.y);
};
/*
* Rectangle
*/
K.Rectangle = function(xx, yy, ww, hh, offsetx, offsety){
this.x = xx || 0;
this.y = yy || 0;
this.width = ww || 0;
this.height = hh || 0;
this.offsetX = offsetx || 0;
this.offsetY = offsety || 0;
};
K.Rectangle.prototype = {
constructor:K.Rectangle,
x:0, y:0, width:0, height:0, offsetX:0, offsetY:0,
getLeft:function() { return this.x; },
getTop:function() { return this.y; },
getRight:function() { return this.x + this.width; },
getBottom:function() { return this.y + this.height; },
getTopLeft:function() { return {x:this.x, y:this.y}; },
getBottomRight:function() { return {x:this.x + this.width, y:this.y + this.height}; },
clone:function() {
var ret = new K.Rectangle(this.x, this.y, this.width, this.height);
ret.offsetX = this.offsetX;
ret.offsetY = this.offsetY;
return ret;
},
containsPoint:function(xx, yy) {
if (xx < this.x) return false;
if (yy < this.y) return false;
if (xx > this.right) return false;
if (yy > this.bottom) return false;
return true;
},
containsRect:function(v){
if (v.x < this.x) return false;
if (v.y < this.y) return false;
if (v.right > this.right) return false;
if (v.bottom > this.bottom) return false;
return true;
},
dispose:function() {},
intersects:function(v) {
if (v.x > this.right) return false;
if (v.y > this.bottom) return false;
if (v.right < this.x) return false;
if (v.bottom < this.y) return false;
return true;
},
intersections:function(v){
var ret = new K.Rectangle(
Math.max(this.x, v.x),
Math.max(this.y, v.y),
0, 0);
ret.width = Math.min(this.right, v.right) - ret.x;
ret.height = Math.min(this.bottom, v.bottom) - ret.y;
return ret;
},
toString:function() {
return "[Rectangle (x="+this.x+" y="+this.y+" width="+this.width+" height="+this.height+")]";
}
};
/*
* Matrix 2D
*/
K.Matrix = function(a, b, c, d, e, f) {
this.a = a || 1; this.b = b || 0; this.e = e || 0;
this.c = c || 0; this.d = d || 1; this.f = f || 0;
};
K.Matrix.prototype = {
constructor:K.Matrix,
a:1, b:0, c:0, d:1, e:0, f:0,
setValues:function (aa, bb, cc, dd, ee, ff) {
this.a = aa;
this.b = bb;
this.c = cc;
this.d = dd;
this.e = ee;
this.f = ff;
return this;
},
rotate:function (rads) {
var nCos = Math.cos(rads);
var nSin = Math.sin(rads);
var tmp = this.a;
this.a = nCos * tmp - nSin * this.b;
this.b = nSin * tmp + nCos * this.b;
tmp = this.c;
this.c = nCos * tmp - nSin * this.d;
this.d = nSin * tmp + nCos * this.d;
tmp = this.e;
this.e = nCos * tmp - nSin * this.f;
this.f = nSin * tmp + nCos * this.f;
return this;
},
scale:function (sx, sy) {
this.a *= sx;
this.b *= sy;
this.c *= sx;
this.d *= sy;
this.e *= sx;
this.f *= sy;
return this;
},
translate:function (dx, dy) {
this.e += this.a * dx + this.c * dy;
this.f += this.b * dx + this.d * dy;
return this;
},
prependRotation:function(rads){
var nCos = Math.cos(rads);
var nSin = Math.sin(rads);
var tmp = this.a;
this.a = nCos * tmp - nSin * this.b;
this.b = nSin * tmp + nCos * this.b;
tmp = this.c;
this.c = nCos * tmp - nSin * this.d;
this.d = nSin * tmp + nCos * this.d;
return this;
},
prependTranslation:function (dx, dy) {
this.e += dx;
this.f += dy;
return this;
},
prependScale:function (sx, sy) {
this.a *= sx;
this.b *= sy;
this.c *= sx;
this.d *= sy;
return this;
},
multiply:function(m){
var tmp = this.a;
this.a = tmp * m.a + this.b * m.c;
this.b = tmp * m.b + this.b * m.d;
tmp = this.c;
this.c = tmp * m.a + this.d * m.c;
this.d = tmp * m.b + this.d * m.d;
tmp = this.e;
this.e = tmp * m.a + this.f * m.c + m.e;
this.f = tmp * m.b + this.f * m.d + m.f;
return this;
},
transformPoint:function(xx, yy){
return {
x : xx * this.a + yy * this.c + this.e,
y : xx * this.b + yy * this.d + this.f
};
},
transformRectangle:function(rect) {
var tl = this.transformPoint(rect.x, rect.y);
var br = this.transformPoint(rect.x + rect.width, rect.y + rect.height);
var xx = Math.min(tl.x, br.x);
var yy = Math.min(tl.y, br.y);
var ww = Math.abs(br.x - tl.x);
var hh = Math.abs(br.y - tl.y);
return new Rectangle(xx, yy, ww, hh);
},
invert:function () {
var det = this.determinant();
if (det !== 0) {
var old = {
a: this.a,
b: this.b,
c: this.c,
d: this.d,
e: this.e,
f: this.f
};
this.a = old.d / det;
this.b = -old.b / det;
this.c = -old.c / det;
this.d = old.a / det;
this.e = (old.c * old.f - old.e * old.d) / det;
this.f = (old.e * old.b - old.a * old.f) / det;
}
return this;
},
clone:function(){
return new K.Matrix(this.a, this.b, this.c, this.d, this.e, this.f);
},
determinant:function(){
return this.a * this.d - this.b * this.c;
},
dispose:function(){},
equal:function(m){
return
this.a == m.a && this.b == m.b && this.c == m.c &&
this.d == m.d && this.e == m.e && this.f == m.f;
},
identity:function(){
this.a = this.d = this.i = 1;
this.b = this.c = this.e = this.f = this.g = this.h = 0;
},
isIdentity:function () {
return this.a === 1 && this.b === 0 && this.c === 0 && this.d === 1 && this.e === 0 && this.f === 0;
},
isInvertible:function () {
return this.determinant() !== 0;
}
};
Tidak ada komentar:
Posting Komentar