//计算几何误差修正
Math.EPS=0.00000001;
//判断x的符号
Math.cmp=function(x) {
if(Math.abs(x)<Math.EPS)return 0;
if(x>0){
return 1;
}else{
return -1;
}
}
//计算几何点类
function Point(x,y) {
if(this instanceof Point){
if(Math.cmp(x.toFixed(2)-x)==0){
x=Number(x.toFixed(2));
}
if(Math.cmp(y.toFixed(2)-y)==0){
y=Number(y.toFixed(2));
}
this.x=x;
this.y=y;
}else{
return new Point(x,y)
}
}
//向量的模长
Point.prototype.norm=function(){
return Math.sqrt(this.x*this.x+this.y*this.y);
}
// 加
Point.add=function(a,b){
return new Point(a.x+b.x,a.y+b.y)
}
// 减
Point.sub=function(a,b){
return new Point(a.x-b.x,a.y-b.y);
}
// 等于
Point.equals=function(a,b){
return Math.cmp(a.x-b.x)===0&&Math.cmp(a.y-b.y)===0;
}
//乘 向量与数字
Point.multiply=function(a,b){
if(a instanceof Point&&typeof b=='number'){
return Point(a.x*b,a.y*b)
}
if(b instanceof Point&&typeof a=='number'){
return Point(a*b.x,a*b.y)
}
}
//除 向量与数字
Point.divide=function(a,b){
return Point(a.x/b,a.y/b)
}
//向量的叉积
Point.det=function (a,b) {
return a.x*b.y-a.y*b.x;
}
//向量的点积
Point.dot=function (a,b) {
return a.x*b.x+a.y*b.y;
}
//两个点的距离
Point.dist=function (a,b) {
return Point.sub(a,b).norm()
}
//逆时针旋转,a为弧度
Point.rotate=function (p,A) {
const tx=p.x;
const ty=p.y;
return Point(tx*Math.cos(A)-ty*Math.sin(A),tx*Math.sin(A)+ty*Math.cos(A))
}
const a=Point(1,2)
const b=Point(1,2)
const c=Point.add(a,b);
console.log(c);
console.log(Point.rotate(c,Math.PI/2))