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?

  • $\begingroup$ The distance between a corner and the center of a rectangle is half of the diagonal. To compute the length of the diagonal, choose two opposite corners and use the usual distance formula (i.e., Pythagoras on the coordinate differences). $\endgroup$ – Henning Makholm Feb 29 '16 at 10:23
  • $\begingroup$ It isn't necessarily a corner...I am rotating the line arbitrarily from centre and stopping at some point on the rectangle edge. Need to find distance from that stopping point to the centre. $\endgroup$ – beNerd Feb 29 '16 at 10:24
  • $\begingroup$ You state that you know the corners, don't you? Actually it seems that you know the coordinates of the corners and the center. What prevents you from simply plugging those coordinates into the distance formula? I don't get what you mean by "rotating a line arbitrarily". What does that have to do with finding the distance between the center and a corner of the rectangle? $\endgroup$ – Henning Makholm Feb 29 '16 at 10:27
  • $\begingroup$ I modified the question title! $\endgroup$ – beNerd Feb 29 '16 at 10:35
  • $\begingroup$ @beNerd Don't you need the coordinates of $a,b,c,d$ to compute the distance from center? $\endgroup$ – BLAZE Feb 29 '16 at 10:41

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.


This is not an answer to your question, but I just think it helps to visualize the situation, I need to be sure that this is the situation you describe:


To calculate the distance from say $(x,y)$ to $d$, you will need to use the distance formula: $$\text{distance}=\sqrt{(x-x_2)^2 + (y-y_2)^2}$$

  • $\begingroup$ not exactly....I cannot rotate the triangle....triangle will be at the same position. Cannot move it. Only line would start moving from centre to edge and can be at any random angle. but rectangle base would be fixed at it's position! Hope I was able to clarify... $\endgroup$ – beNerd Feb 29 '16 at 17:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.