本文介绍了javascript中中心到矩形边缘上任意点的距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如上图所示,假设有一束光线从中心发出并在矩形的边缘发生碰撞.所以我想计算它在哪一点碰撞,以便能够计算出边缘上的点与中心之间的距离.

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.

  • 我们知道什么?
    • 矩形的宽度和高度
    • 学位& b度(如上图所示)
    • centerX,centerY
    • What we know?
      • width and height of rectangle
      • a degree & b degree (as shown in above figure)
      • centerX, centerY

      推荐答案

      让我们的中心为(0,0),射线角度为phi.伪代码:

      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
      

      这篇关于javascript中中心到矩形边缘上任意点的距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 14:02