I have a rectangle which I know the width and height of it. I need to draw a line inside the rectangle and the information that I have include knowing the starting point of the line as well as the angle of the line.

My question is how can I calculate the length of the line given the informaiton that I have?

I don't know where the line will end up, I just have a starting point, the angle and the general width and height of the rectangle.

I tried to draw some sample lines to show you what I'm trying to do, I need to find the length of the red line so I can draw it in my App.

enter image description here

  • 1
    $\begingroup$ Is the starting point always on the border of the rectangle? $\endgroup$ Jul 23, 2020 at 22:41
  • $\begingroup$ Do you know about sine and cosine? This may help. $\endgroup$
    – saulspatz
    Jul 23, 2020 at 22:49
  • $\begingroup$ @MaximilianJanisch yes the starting point is always on the border of the rectangle. $\endgroup$ Jul 23, 2020 at 22:57
  • $\begingroup$ If the numbers $30^\circ$, $35^\circ$, and $15^\circ$ are meant to refer to the obvious angle on the other side of the red line, the first number should probably be $60^\circ$ instead of $30$, or else the line should be redrawn. (The other two figures look roughly correct.) $\endgroup$ Jul 23, 2020 at 23:15
  • $\begingroup$ @BarryCipra that was my mistake to write the 30∘ before the line. It was meant to be in front of the line as 30∘ is the correct angle of the line. $\endgroup$ Jul 23, 2020 at 23:23

2 Answers 2


Since the problem remains the same when the rectangle is rotated/flipped, we can assume for simplicity that the starting point is on the bottom border of the rectangle. (When rotating the problem, make sure to get the angle and width/height of the rotated problem correct.)

Let $w$ denote the width and $h$ the height of the rectangle. Let the starting point have distance $x$ from the left border of the rectangle. Let $\alpha$ be the angle between your line and the bottom line that you get by "rotating your line to the left."


enter image description here

We want to compute the point $\overline{EF}$. If $\operatorname{arcsec}$ denotes the inverse function of $x\mapsto\frac1{\cos(x)}$ and $$\alpha=\operatorname{arcsec}\left(\frac{\overline{CE}}{\overline{AE}}\right)=\operatorname{arcsec}\left(\frac{\sqrt{x^2+h^2}}{x}\right),$$

then your line goes straight to the top left corner and the length is $\sqrt{x^2+h^2}$. If $\alpha$ is less than that, then you will "bump" into the left border and we have $\overline{AF}=\tan(\alpha) x$. Hence the length of the line is $$\sqrt{(\tan(\alpha)^2+1) x^2}=\lvert\sec(\alpha)x\rvert.$$

Let $\beta=\pi-\alpha$. You get the same thing: If $$\beta=\operatorname{arcsec}\left(\frac{\overline{ED}}{\overline{BE}}\right)=\operatorname{arcsec}\left(\frac{\sqrt{(w-x)^2+h^2}}{(w-x)}\right),$$

then the line goes straight up to the top right corner. So the length is $\sqrt{(w-x)^2+h^2}$. If $\beta$ is less than that, then analogously to before, the length of the line is $$\lvert\sec(\beta)(w-x)\rvert.$$

Now, if $\alpha$ is big enough such that you don't bump into the left border and $\beta$ is big enough such that you don't bump into the right border, you will bump into the top border (as in my image). In this case, let $F^\top$ be the orthogonal projection of $F$ onto the bottom border. Then $\overline{F^\top E}=h\cot(\alpha)$ so that the length of the line is $$\sqrt{h^2\cot(\alpha)^2+h^2}=\lvert h\csc(\alpha)\rvert.$$

Example (30°): After flipping we see $h=10, w=6, \beta=\frac\pi6, x=2$. We compute $$\operatorname{arcsec}\left(\frac{\sqrt{(w-x)^2+h^2}}{(w-x)}\right)=\operatorname{arcsec}(\sqrt{29}/2)\approx1.19>\beta.$$

So we will bump into the right border with a line length of $$\lvert\sec(\beta)(w-x)\rvert=4\sec(\pi/6)=\frac{8}{\sqrt 3}.$$


Assuming the lines begin and end on the rectangle, use trigonometric functions sin, cos. For example, in the third rectangle, denoting the line length $\ell$ we have:

$$\ell = 6/\cos(35^\circ)$$

  • 1
    $\begingroup$ Thank you for your answer. Does this work for all the lines that I drew? If yes can you please mention how it will be for the one with a 30-degree angle? $\endgroup$ Jul 23, 2020 at 23:02
  • $\begingroup$ Yeah, so long as you know the angle and the side lengths you can use the trig functions. Look up the definitions of sin, cos, tan and you'll be able to solve the 30 degree one and any others yourself $\endgroup$
    – diracsum
    Jul 24, 2020 at 16:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .