# How to get a stroke length defined by two coordinates?

The rectangle (black) has a defined width and height (e.g. 200x200). Suppose there are two (white) points (20,120) and (120,120). How do I get the width of the line crossing the two points (the blue line)?

-
So, do you want the length of the whole line? or just between the points? or what is the 'width' of the line? – Alex Oct 19 '12 at 21:22
The length of the whole line. – Gajus Oct 19 '12 at 21:24

Let $x_1,y_1$ and $x_2,y_2$ be the coordinates of the first and second point respectively. Also, let $w$ be the width of the rectangle, $h$ the height and $x$ the length of the line. Finally, let $a = |x_2-x_1|$ and $b=|y_2-y_1|$

If $bw<ah$, then $x = \frac{w}{a}\sqrt{a^2+b^2}$

If $bw>ah$, then $x = \frac{h}{b}\sqrt{a^2+b^2}$

If $bw=ah$, use either of the above.

For your example: $w=200,h=200,a=100,b=0$

So, $x=\frac{200}{100}\cdot100=200$

-
I hope this actually works :) – Alex Oct 19 '12 at 21:42
Generally it seem to work. Though, why sometime I get negative values? Does the order whether I deduct x2−x1 or x1-x2 matter? – Gajus Oct 19 '12 at 21:52
I assumed $x_2>x_1$. I guess you can use the absolute value, though. – Alex Oct 19 '12 at 21:55
@Guy so, I changed the definition of $a$ and $b$. should work now – Alex Oct 19 '12 at 22:00

I take it by "width" you mean what I would call "length". By similar triangles, Length is to 200 as distance between the two points is to 100. But with the coordinates you have given, the line is horizontal, anyway.

-

The width appears to be one pixel. The length comes from the Pythagorean theorem The picture looks more like the white dots are at $(20,140)$ and $(120,100)$ (measuring from the upper left as $(0,0)$ which seems the computer graphic standard.) The distance between the dots is then found from $d^2=(120-20)^2+(140-100)^2=11600,$ so $d=\sqrt {11600} \approx 107.7$

Added: if you want the length from edge to edge, you need to scale up the vertical length to the width of the frame. It is then $200\frac{140-100}{120-20}=80$ and the length of the blue line is $\sqrt {200^2+80^2}\approx 215.4$

-
Thank you, though this gives the distance between the two dots. I need to know the length of the blue line that is crossing through these points. – Gajus Oct 19 '12 at 21:27
What if it isn't a square? – Gajus Oct 19 '12 at 21:29
@Guy: for the first, see my addition. for the second, you still scale the direction that doesn't hit the edge, then sum the square of that with the square of the whole frame width and take the square root. – Ross Millikan Oct 19 '12 at 21:36