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)?
How to get a stroke length defined by two coordinates?
-
0The length of the whole line. – 2012-10-19
3 Answers
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
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$
-
0@Guy so, I changed the definition of $a$ and $b$. should work now – 2012-10-19
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$
-
0@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. – 2012-10-19