线段平移。
function Line(from, to, color, dasharray){
this.from = from;
this.to = to;
this.color = typeof(color)=='undefined'?'black':color;
this.dasharray = typeof(dasharray)=='undefined'?false:dasharray;
}
移动方法
Line.prototype.move = function(d){
var r = Math.atan2(this.to.x-this.from.x, this.to.y-this.from.y)+Math.PI/2;
var x = Math.sin(r)*d, y = Math.cos(r)*d;
var x1 = this.from.x+x, y1 = this.from.y+y;
var x2 = this.to.x+x, y2 = this.to.y+y;
return new Line(new Point(x1, y1),new Point(x2, y2));
};
旋转
Line.prototype.rotate = function(alpha){
var l = Math.round(Math.sqrt(
Math.pow((this.to.y-this.from.y), 2) +
Math.pow((this.to.x-this.from.x), 2)));
var r = Math.atan2(this.to.y-this.from.y, this.to.x-this.from.x)+Math.PI*alpha/180;
var x = Math.cos(r)*l, y = Math.sin(r)*l;
//var x2 = this.
console.log('r:' + r + ', beta: ' + Math.round(180*(r/Math.PI)) + ', length: ' + l);
var x2 = this.from.x+x, y2 = this.from.y+y;
return new Line(this.from, new Point(x2, y2));
};
根据起点和角度、长度创建线段
var LineMaker = {
make: function(from, alpha, l){
var to = new Point(
from.x+l*Math.cos(Math.PI*alpha/180),
from.y+l*Math.sin(Math.PI*alpha/180)
);
return new Line(from, to);
}
}