polarvector to edge of rectangle

22 Views Asked by At

Say I got a rectangle with its middle coordinates $(x_m,y_m)$ inline with the coordinate grid with a corner in $(x_c,y_c)$. What's the distance from $(x_m,y_m)$ to the rectangle edge at angle $a$?

Explanatory image

1

There are 1 best solutions below

0
On BEST ANSWER

First, find at what angles the corners are. You have first corner at $(x_m,y_m)$, so say the angle is $\beta_1$, such as $$\tan\beta_1=\frac{y_m-y_c}{x_m-x_c}$$ You can get the other angles by symmetry: $\beta_2=180^\circ-\beta_1$, $\beta_3=180^\circ+\beta_1$, $\beta_4=360^\circ-\beta_1$. Now you have two cases:

  • Case 1: $0^\circ\le \alpha\lt\beta_1$ or $\beta_2\le\alpha\lt\beta_3$ or $\beta_4\le\alpha\lt 360^\circ$

  • Case 2: $\beta_1\le\alpha\lt\beta_2$ or $\beta_3\le\alpha\lt\beta_4$

In the first case you intersect one of the vertical sides, in the second you intersect the top or bottom. For Case 1, you know the horizontal length ($x_c-x_m$), for Case 2 you know the vertical length ($y_m-y_c$)