js 判断当前点到起点是否是顺时针

  1 /* Point
  2 parameter: 
  3     x = 0, y = 0;
  4 
  5 attribute
  6     x, y: Number;
  7 
  8 method:
  9     set(x, y): this;
 10     angle(origin): Number;
 11     copy(point): this;
 12     clone(): Point;
 13     distance(point): Number;            //获取欧几里得距离
 14     distanceMHD(point): Number;            //获取曼哈顿距离
 15     distanceCompare(point): Number;        //获取用于比较的距离(相对于.distance() 效率更高)
 16     equals(point): Bool;                //是否恒等
 17     reverse(): this;                    //取反值
 18     rotate(origin: Object{x,y}, angle): this;    //旋转点
 19     normalize(): this;                    //归一
 20     isClockwise(startPoint): Bool;        //startPoint 到自己是否是顺时针旋转
 21 */
 22 class Point{
 23 
 24     get isPoint(){return true;}
 25 
 26     constructor(x = 0, y = 0){
 27         this.x = x;
 28         this.y = y;
 29     }
 30 
 31     set(x = 0, y = 0){
 32         this.x = x;
 33         this.y = y;
 34 
 35         return this;
 36     }
 37 
 38     angle(origin){
 39 
 40         return Math.atan2(this.y - origin.y, this.x - origin.x);
 41 
 42     }
 43 
 44     copy(point){
 45         
 46         this.x = point.x;
 47         this.y = point.y;
 48         return this;
 49         //return Object.assign(this, point);
 50 
 51     }
 52     
 53     clone(){
 54 
 55         return new this.constructor().copy(this);
 56         //return Object.assign(new this.constructor(), this);
 57         
 58     }
 59 
 60     distance(point){
 61         
 62         return Math.sqrt(Math.pow(point.x - this.x, 2) + Math.pow(point.y - this.y, 2));
 63 
 64     }
 65 
 66     distanceMHD(point){
 67 
 68         return Math.abs(this.x - point.x) + Math.abs(this.y - point.y);
 69 
 70     }
 71 
 72     distanceCompare(point){
 73     
 74         return Math.pow(this.x - point.x, 2) + Math.pow(this.y - point.y, 2);
 75 
 76     }
 77 
 78     equals(point){
 79 
 80         return point.x === this.x && point.y === this.y;
 81 
 82     }
 83 
 84     reverse(){
 85         this.x = -this.x;
 86         this.y = -this.y;
 87 
 88         return this;
 89     }
 90 
 91     rotate(origin, angle){
 92         const c = Math.cos(angle), s = Math.sin(angle), 
 93         x = this.x - origin.x, y = this.y - origin.y;
 94 
 95         this.x = x * c - y * s + origin.x;
 96         this.y = x * s + y * c + origin.y;
 97 
 98         return this;
 99     }
100 
101     normalize(){
102         const len = 1 / (Math.sqrt(this.x * this.x + this.y * this.y) || 1);
103         this.x *= len;
104         this.y *= len;
105 
106         return this;
107     }
108 
109     isClockwise(startPoint){
110 
111         return this.x * startPoint.y - this.y * startPoint.x < 0;
112 
113     }
114 
115 /*     add(point){
116         this.x += point.x;
117         this.y += point.y;
118         return this;
119     }
120 
121     sub(point){
122         this.x -= point.x;
123         this.y -= point.y;
124         return this;
125     }
126 
127     multiply(point){
128         this.x *= point.x;
129         this.y *= point.y;
130         return this;
131     }
132 
133     divide(point){
134         this.x /= point.x;
135         this.y /= point.y;
136         return this;
137     } */
138 
139 }
Utils.js

使用:

const sPoint = new Point(10, 0), nPoint = new Point(0, 10);
console.log(nPoint.isClockwise(sPoint)) // false

 

posted @ 2022-10-20 21:15  鸡儿er  阅读(29)  评论(0编辑  收藏  举报