/* * === Geometory Rectangle Class === * * updated 2003/07/03 * * mail : peace@skipup.com * home : http://www.skipup.com/~peace/ */ /** * 四角形(2次元). *
座標系は以下.
 * ┌―→X
 * |
 * ↓
 * Y
* * インスタンス変数 * number x * number y * number width * number height */ function class__Rectangle__(window) { var classId = Object.getClassName(arguments.callee); /** * void apply(Rectangle p, Arguments args) */ function apply(o, a) { switch (a.length) { case 0 : o.x = o.y = 0.0; o.width = o.height = 0.0; return; case 1 : // Rectangle || Argument var s = a[0]; if (s.constructor === Rectangle) { // Rectangle o.x = s.x; o.y = s.y; o.width = s.width; o.height = s.height; } else { // Arguments apply(o, s); } return; case 2 : // Point, Dimesion2 || Point, Point var p0 = a[0], p1 = a[1]; if (p1.constructor === Dimension) { o.x = p0.x; o.y = p0.y; o.width = p1.width; o.height = p1.height; } else { // 左上点をx, yに設定. o.x = p0.x < p1.x ? p0.x : p1.x; o.y = p0.y < p1.y ? p0.y : p1.y; o.width = Math.abs(p1.width - p0.width); o.height = Math.abs(p1.height - p0.height); } return; case 4 : // number, number, number, number o.x = a[0]; o.y = a[1]; o.width = a[2]; o.height = a[3]; return; } } /** * コンストラクタ. * Rectangle() * Rectangle(Rectangle d) * Rectangle(Arguments args) * Rectangle(number x, number y, number width, number height) * Rectangle(Point p, Dimesion2 d) * Rectangle(Point p0, Point p1) */ var F = window[classId] = function() { if (this.constructor !== F) this.constructor = F; apply(this, arguments); }; var FP = F.prototype; /** * boolean epsilonEquals(Rectangle p, double eps) */ FP.epsilonEquals = function(p, e) { var d = this.x - p.x; if ((d >= 0.0 ? d : -d) > e) return false; d = this.y - p.y; if ((d >= 0.0 ? d : -d) > e) return false; d = this.width - p.width; if ((d >= 0.0 ? d : -d) > e) return false; d = this.height - p.height; return (d >= 0.0 ? d : -d) <= e; }; /** * boolean equals(Object p) */ FP.equals = function(p) { if (p == null || p.constructor !== F) return false; return (this.x === p.x && this.y === p.y) && (this.width === p.width && this.height === p.height); }; /** * 面積. * double getArea() */ FP.getArea = function() { return this.width * this.height; }; /** * boolean intEquals(Rectangle p) */ FP.intEquals = function(p) { return Math.floor(this.x) == Math.floor(p.x) && Math.floor(this.y) == Math.floor(p.y) && Math.floor(this.width) == Math.floor(p.width) && Math.floor(this.height) == Math.floor(p.height); }; /** * boolean isNaN() */ FP.isNaN = function() { return (isNaN(this.x) || isNaN(this.y)) || (isNaN(this.width) || isNaN(this.height)) || (this.x === "" || this.y === "") || (this.width === "" || this.height === ""); }; /** * void setLocation(Point p) * void setLocation(number x, number y) */ FP.setLocation = function(p0, p1) { if (arguments.length == 2) { this.x = p0; this.y = p1; } else { this.x = p0.x; this.y = p0.y; } }; /** * void setSize(Dimension p) * void setSize(number width, number height) */ FP.setSize = function(p0, p1) { if (arguments.length == 2) { this.width = p0; this.height = p1; } else { this.width = p0.width; this.height = p0.height; } }; /** * void set(...) * 引数の形式,型はコンストラクタに同じ. */ FP.set = function() { apply(this, arguments); }; /** * String toString() */ FP.toString = function() { return classId + "[" + ("x=" + this.x + ",y=" + this.y + ",") + ("w=" + this.width + ",h=" + this.height) + "]"; }; } class__Rectangle__(window);