forked from fanruan/fineui
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
63 lines
2.1 KiB
63 lines
2.1 KiB
7 years ago
|
// 向量操作
|
||
8 years ago
|
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,
|
||
7 years ago
|
// 判断两个区域是否相交,若相交,则要么顶点互相包含,要么矩形边界(或对角线)相交
|
||
8 years ago
|
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;
|
||
7 years ago
|
} else if (obj.x != null && obj.y != null)// 判断矩形对角线相交 |v1 X v2||v1 X v3| < 0
|
||
8 years ago
|
{
|
||
7 years ago
|
var vector1 = new BI.Vector(this.w, this.h);// 矩形对角线向量
|
||
8 years ago
|
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;
|
||
|
},
|
||
7 years ago
|
// 判断一个点是否在这个区域内部
|
||
8 years ago
|
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;
|
||
|
},
|
||
7 years ago
|
// 返回区域的重心,因为是矩形所以返回中点
|
||
8 years ago
|
getPosition: function () {
|
||
|
var pos = [];
|
||
|
pos.push(this.x + this.w / 2);
|
||
|
pos.push(this.y + this.h / 2);
|
||
|
return pos;
|
||
|
}
|
||
|
};
|