Distance between center to any point on edge of rectangle

7.4k Views Asked by At

https://i.stack.imgur.com/025KM.png

As you can see in the above picture, suppose that there is a ray emitted from center which intersects the edge of the rectangle.

How do I calculate at which point the ray intersects the edge?

(So that I could be able to calculate the distance between the center and a point on the edge.)

The following is known:
- width and height of rectangle
- a degree & b degree (as shown in above figure)
- centerX, centerY

2

There are 2 best solutions below

3
On

if you only need distance then,

$$\cos b^{\circ} = {\text{half width}\over \text {distance you want to find} }$$


enter image description here

The slope of line $A$ is $0$.

let the slope of red line be $m$.

$$\tan a^{\circ} = {|0 - m| \over |1+ 0*m|} = |m|$$

you now know the slope of the red line and one point on it. Can you find the $(x2 , y2)$ now ?

1
On

There are two possibilities: The line intersects the top first, or the line intersects the left first.

Using your notation, but making $a$ and $b$ in radians, the two distances are:

If $u$ is the distance to the top, $\dfrac{B}{u} =\cos(b) $, so that $u =\dfrac{B}{\cos(b)} $.

Similarly, if $v$ is the distance to the left, $\dfrac{A}{v} =\cos(a) $, so that $v =\dfrac{A}{\cos(a)} $.

The distance we want is the smaller of these, which is $\min\left(\dfrac{A}{\cos(a)}, \dfrac{B}{\cos(b)}\right) $.

Since $a+b =\dfrac{\pi}{2} $, $\cos(b) =\sin(a) $, so we can write this as $\min\left(\dfrac{A}{\cos(a)}, \dfrac{B}{\sin(a)}\right) $.

Without numerical values, we can not decide which value is smaller.