问题
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