计算平面两点角度丨技术开发分享录

计算平面两点角度

{{ detail.nickname }}

转载 翻译 {{ formatTime(detail.create_time) }} 字数 {{ detail.content && detail.content.length }} 阅读 {{ detail.read_num }} {{ formatTag(v) }}

"![](https://ss2.baidu.com/-vo3dSag_xI4khGko9WTAnF6hhy/zhidao/wh%3D450%2C600/sign=8593694dbc315c6043c063ebb881e725/d52a2834349b033b3ed8398215ce36d3d539bd12.jpg)\n\n## 一、atan实现\n\n```\nfunction calcAng(x, y) {\n  if (y == 0 && x == 0) {\n    return 0\n  } else if (y > 0 && x == 0) {\n    return 0\n  } else if (y < 0 && x == 0) {\n    return 180\n  } else if (y == 0 && x < 0) {\n    return 270\n  } else if (y == 0 && x > 0) {\n    return 90\n  } else if (y > 0 && x > 0) {\n    let angle = Math.atan(y / x) * 180 / Math.PI\n    return 90 - angle\n  } else if (y > 0 && x < 0) {\n    let angle = (Math.atan(y / x) * 180 / Math.PI) * -1\n    return 270 + angle\n  } else if (y < 0 && x > 0) {\n    let angle = Math.atan(y / x) * 180 / Math.PI * -1\n    return 90 + angle\n  } else {\n    let angle = Math.atan(y / x) * 180 / Math.PI\n    return 270 - angle\n  }\n}\ncalcAng(30, 40); // 36.86989764584402\n```\n\n## 二、acos实现\n\n```\nfunction calcAng(x, y) {\n  if(x === 0 && y === 0) {\n    return 0;\n  }\n  let temp = y / Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2))\n  let angle = Math.acos(temp) * 180 / Math.PI\n  if (x < 0) {\n    return 360 - angle\n  }\n  return angle\n}\n```"
PS:写作不易,如要转裁,请标明转载出处。

如果此篇对您有帮助,可小额赞助,以兹鼓励!

猜你想看