计算屏幕两点间的距离可以使用勾股定理,即:

distance = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));

其中,x1、y1为第一个点的坐标,x2、y2为第二个点的坐标。

计算屏幕两点间的角度需要用到反正切函数,即:

angle = Math.atan2(y2 - y1, x2 - x1) * 180 / Math.PI;

其中,Math.atan2(y, x)返回角度[-π, π],需要将弧度转化为角度,即乘以180/π。

完整的代码如下:

var distance = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2)); var angle = Math.atan2(y2 - y1, x2 - x1) * 180 / Math.PI;

console.log('距离:' + distance); console.log('角度:' + angle);

JavaScript 计算屏幕两点间距离和角度

原文地址: https://www.cveoy.top/t/topic/n7md 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录