Line(计算类)
Line的计算类,有用于数学计算的方法
内部属性:
方法:
_getEndpointDots:传入线的起点,斜路与线的长度(半径),计算出线的终点所在的坐标
getRenderPolygon:从方法来看,似乎是返回根据这这条直线为中轴,2*radius为另外边长的矩形
getIntersectionPoint:传入一条线,传入线和当前线的交掉point
getProjectionPoint:获取传入point在这条直线上的映射点
getDependent:获取传入point的映射点在这条线切出的新线段和原线段的长度比值
getDependentPoint:根据传入的长度壁纸返回直线上依赖点的point
Line(一)
和之前分析过的Dot2D一样,也是一个图形类,代表一根线
继承自element
私有属性:
方法:
getLine:获取Line的计算实例类
checkCollide:检查碰撞,实际返回的是distance
Line2D.prototype.checkCollide = function(point){
//检查碰撞
//获取碰撞manager中离鼠标最近的dot
let collide_list = CollideManager.getCollideList(Dot2D.TAG, 1);
if(collide_list.length > 0) return -1;
//获取线的起点和终点
let line = Graphics.getRenderLine(this.getLine());
let start = line.start;
let end = line.end;
let radius = 3;
if(Math.abs(start.x - end.x) < 0.01){
//start和end的x值差值很小,认为是一根竖线
if((start.y < end.y && (start.y < point.y && point.y < end.y)) ||
(start.y > end.y && (start.y > point.y && point.y > end.y)))
//point的y值在在start和end之间
{
//distance是point与start的x差值,垂直距离
let distance = Math.abs(point.x - start.x)
return distance <= radius + 2 ? distance : -1;
}
}else if(Math.abs(start.y - end.y) < 0.01){
//start和end的y值差值很小,认为是一根横线
if((start.x < end.x && (start.x < point.x && point.x < end.x)) ||
(start.x > end.x && (start.x > point.x && point.x > end.x))){
//与上方同理point的x值在在start和end之间
let distance = Math.abs(point.y - start.y)
return distance <= radius + 2 ? distance : -1;
}
}else{
//不是横线也不是竖线
let k1 = (end.y - start.y)/(end.x - start.x);//直线的斜率
let k2 = -1 / k1;//从点发出的垂直线的斜率
let x = (k2 * point.x - point.y + end.y - k1 * end.x) / (k2 - k1);
let y = k2 * (x - point.x) + point.y;//计算交点的x,y
if((start.x < end.x && (start.x < x && x < end.x)) || (start.x > end.x && (start.x > x && x > end.x))){
if((start.y < end.y && (start.y < y && y < end.y)) || (start.y > end.y && (start.y > y && y > end.y))){
//对于点在直线之间的情况,直接计算垂直到直线的距离
let distance = point.distance(new Point(x, y));
return distance <= radius + 2 ? distance : -1;
}
}
}
//对于点不在直线之间,取到直线两头距离的最小值
let distance = Math.min(point.distance(start), point.distance(end));
return distance <= radius + 2 ? distance : -1;
};