# I need to help with a formula to find the length of a line.

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.

• Is the starting point always on the border of the rectangle? Jul 23, 2020 at 22:41
• Do you know about sine and cosine? This may help. Jul 23, 2020 at 22:49
• @MaximilianJanisch yes the starting point is always on the border of the rectangle. Jul 23, 2020 at 22:57
• 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.) Jul 23, 2020 at 23:15
• @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. Jul 23, 2020 at 23:23

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."

Image:

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)$$

• 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? Jul 23, 2020 at 23:02
• 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 Jul 24, 2020 at 16:54