zoukankan      html  css  js  c++  java
  • 计算几何点类

    //计算几何误差修正
    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))
  • 相关阅读:
    构建工具
    Ajax跨域问题
    Flex 布局教程:语法篇
    Linux常用命令
    JavaScript正则表达式
    jQuery基础(四)动画
    前端面试问题汇总(一)
    jQuery基础(三)事件
    JavaScript中的基本数据类型
    Django积木块二——邮箱
  • 原文地址:https://www.cnblogs.com/caoke/p/10529713.html
Copyright © 2011-2022 走看看