0
$\begingroup$

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

enter image description here

  • 0
    So, do you want the length of the whole line? or just between the points? or what is the 'width' of the line?2012-10-19
  • 0
    The length of the whole line.2012-10-19

3 Answers 3

1

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

  • 0
    I hope this actually works :)2012-10-19
  • 0
    Generally it seem to work. Though, why sometime I get negative values? Does the order whether I deduct `x2−x1` or `x1-x2` matter?2012-10-19
  • 0
    I assumed $x_2>x_1$. I guess you can use the absolute value, though.2012-10-19
  • 0
    @Guy so, I changed the definition of $a$ and $b$. should work now2012-10-19
0

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.

0

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
    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.2012-10-19
  • 0
    What if it isn't a square?2012-10-19
  • 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