/** * 计算从x1y1到x2y2的直线,与水平线形成的夹角 * 计算规则为顺时针从左侧0°到与该直线形成的夹角 * @param {Object} x1 * @param {Object} y1 * @param {Object} x2 * @param {Object} y2 * @param {Boolean} toAngle 是否转换为角度值,默认false */ export const getAngle = function ( x1: number, y1: number, x2: number, y2: number, toAngle = false ) { const x = x1 - x2; const y = y1 - y2; if (!x && !y) { return 0; } // 弧度 radian = 角度 * Math.PI / 180 // 角度 angle = 弧度 * 180 / Math.PI let res; // 角度 const angle = (180 + (Math.atan2(-y, -x) * 180) / Math.PI + 360) % 360; res = 360 - angle; if (!toAngle) { res = (res * Math.PI) / 180; } return res; };