// 向量操作 BI.Vector = function (x, y) { this.x = x; this.y = y; }; BI.Vector.prototype = { constructor: BI.Vector, cross: function (v) { return (this.x * v.y - this.y * v.x); }, length: function (v) { return (Math.sqrt(this.x * v.x + this.y * v.y)); } }; BI.Region = function (x, y, w, h) { this.x = x; this.y = y; this.w = w; this.h = h; }; BI.Region.prototype = { constructor: BI.Region, // 判断两个区域是否相交,若相交,则要么顶点互相包含,要么矩形边界(或对角线)相交 isIntersects: function (obj) { if (this.isPointInside(obj.x, obj.y) || this.isPointInside(obj.x + obj.w, obj.y) || this.isPointInside(obj.x, obj.y + obj.h) || this.isPointInside(obj.x + obj.w, obj.y + obj.h)) { return true; } else if (obj.isPointInside(this.x, this.y) || obj.isPointInside(this.x + this.w, this.y) || obj.isPointInside(this.x, this.y + this.h) || obj.isPointInside(this.x + this.w, this.y + this.h)) { return true; } else if (obj.x != null && obj.y != null)// 判断矩形对角线相交 |v1 X v2||v1 X v3| < 0 { var vector1 = new BI.Vector(this.w, this.h);// 矩形对角线向量 var vector2 = new BI.Vector(obj.x - this.x, obj.y - this.y); var vector3 = new BI.Vector(vector2.x + obj.w, vector2.y + obj.h); if ((vector1.cross(vector2) * vector1.cross(vector3)) < 0) { return true; } } return false; }, // 判断一个点是否在这个区域内部 isPointInside: function (x, y) { if (this.x == null || this.y == null) { return false; } if (x >= this.x && x <= this.x + this.w && y >= this.y && y <= this.y + this.h) { return true; } return false; }, // 返回区域的重心,因为是矩形所以返回中点 getPosition: function () { var pos = []; pos.push(this.x + this.w / 2); pos.push(this.y + this.h / 2); return pos; } };