Distance between center to any point on edge of rectangle in javascript

╄→尐↘猪︶ㄣ 提交于 2020-01-17 13:48:20

问题


As you can see in above picture, suppose that there is a ray emitted from center and collides at edge of rectangle. so I want to calculate at which point it collides, so that I could be able to calculate distance between point on edge and center.

  • What we know?
    • width and height of rectangle
    • a degree & b degree (as shown in above figure)
    • centerX, centerY

回答1:


Let's center is (0, 0) and ray angle is phi. Pseudocode:

c = Cos(phi)
s = Sin(phi)
if Width * Abs(s) < Height * Abs(c) then
   x = Sign(c) * Width / 2
   y = Tan(phi) * x
else
   y = Sign(s) * Height / 2
   x = CoTan(phi) * y


来源:https://stackoverflow.com/questions/39055985/distance-between-center-to-any-point-on-edge-of-rectangle-in-javascript

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!