[Math] How to you find the distance between the center and edges of a rectangle – a line from centre to a edge at an angle $\theta$

coordinate systemsgeometryrectanglestrigonometry

I have a case where I know the coordinates $(x,y)$ of the center of the rectangle and its edges where the line is dropped anywhere on the edges $a(x_1,y_1),b(x_2,y_1),c(x_1,y_2),d(x_2,y_2)$.

Say I start drawing a line from center towards any edge of the rectangle and rotate it by a arbitrary angle $ \theta$. I need to know that line vector i.e distance from center to the point on the edge or corner vertex which is connected by the line. Any takers?

Best Answer

If I understand you right, then this may help, though it is far from a complete formula:

If we consider that a triangle is formed by the centre point, the point of intersection and the centre of the top line cd, and that the angle of the line is measured from that centre point, then the length of the line is the hypotenuse of an equilateral triangle formed by these three points.

If we now assume that the height of the rectangle is the adjacent, we can calculate the length of the hypotenuse using trigonometry as h = a / cos(t) where t is the angle of the line and o is half the rectangle's height. (Depending on software you may need to take an absolute value.)

We can repeat this formula with the width of the rectangle using cosine, hence the hypotenuse according to the width is h = o / sin(t) where a is half the rectangle's width.

The actual length of the hypotenuse must be whichever of these two values is lower.

There are other methods, such as calculating the intersection point of the line with each line that makes up the rectangle and taking the closest to the point of origin, but I think this is the more efficient.