首页 > 其他分享 >js 判断当前点到起点是否是顺时针

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

时间:2022-10-20 21:23:49浏览次数:43  
标签:origin 顺时针 return point 是否是 js angle startPoint Math

  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

 

标签:origin,顺时针,return,point,是否是,js,angle,startPoint,Math
From: https://www.cnblogs.com/weihexinCode/p/16811345.html

相关文章

  • os 模块 sys模块 json模块
    今日内容详细os模块(重要)os模块主要与代码运行所在的操作系统打交道importos1.创建目录1.创建目录(文件夹)os.mkdir(r'L4')#只可以创建单级目录相对路......
  • python进阶之路18 os、sys、json模块
    os模块与sys模块os模块主要与操作系统打交道sys模块主要与python解释器打交道os模块(重要)os模块主要与代码运行所在的操作系统打交道importos#1.创建目录(......
  • Dva.js 快速上手指南
    先说些废话最近在开发React技术栈的项目产品,对于数据状态的管理使用了Dva.js,作为一个资深的ow玩家,我看到这个名字第一反应就是————这不是ow里的一个女英雄吗?仔细阅读......
  • 常用内置模块之os、sys、json简介
    昨日内容回顾包的使用包与普通模块的使用相同。导入包实际是导入了包内的双下iter文件,可以使用该文件内的名称。软件开发目录规范启动相关文件夹bin......
  • 【java json基础】字符串转json json转字符串 json数组转换 【java基础知识】【实用】
    读书就是:从薄读厚,再从厚读薄的过程。(前部分:问答,后部分:测试举例)         数据测试:举例:      ......
  • Day19 Os.sys.json模块
    目录os模块(重要)1.创建目录(文件夹)2.删除目录(文件夹)3.列举指定路径下内容名称4.删除/重命名文件5.获取/切换当前工作目录6.动态获取项目根路径(重要)7.判断路径是否存......
  • os,sys,json模块
    目录os,sys,json模块今日内容概要os模块(重要)sys模块json模块json模块实战os,sys,json模块今日内容概要os与sys模块os模块主要与操作系统打交道sys模块主要与python解释......
  • js 文件大小bytes自动格式化为kb,mb单位。
    文件大小格式化工具函数 functionformatBytes(sizeBytes){ letmemoryUnits=[ { unitName:'bytes', threshold:1024, }, { unitName......
  • python内置模块之os sys 与json
    os模块os模块主要与代码运行所在的操作系统打交道importos1,创建目录(文件夹)os.mkdir(r'd1')#相对路径在执行文件所在的路径下创建目录可以创建单级目录os.mk......
  • vue.config.js 常用的属性
    //vue.config.js文件是脚手架的配置文件const{defineConfig}=require("@vue/cli-service");module.exports=defineConfig({transpileDependencies:true,......