Can a straight line be drawn from origin to co-ordinate X,Y?

104 Views Asked by At

Given a co-ordinate $P(X,Y)$, can a straight line be drawn from origin to $P$, if there is wall existing with end points $A(X1,Y1)$ and $B(X2,Y2)$?

My Approach:
I first of all wrote the equations from origin to $A$ and origin to $B$. Then I kept the value of $P$ in both and calculated their product, if negative or 0 then the answer is NO otherwise YES.

2

There are 2 best solutions below

0
On BEST ANSWER

(For simplicity, assume that the line through A and B does not pass through the origin or the point P.)

Any point on the line segment from A to B has the form $t\langle x_1,y_1\rangle + (1-t)\langle x_2,y_2\rangle$ where $0\le t\le 1$.

Similarly, any point on the line segment from the origin to P has the form $u\langle x,y\rangle$ where $0\le u\le1$.

Therefore the wall will block the line segment from O to P when the equation $u\langle x,y\rangle=t\langle x_1,y_1\rangle + (1-t)\langle x_2,y_2\rangle$ has a solution where $0\le u\le1$ and $0\le t\le 1$.

Then $\langle x,y\rangle=a\langle x_1,y_1\rangle + b\langle x_2,y_2\rangle$ where $a=\frac{t}{u}$ and $b=\frac{1-t}{u}$ satisfy $a\ge0$, $b\ge0$, and $a+b>1$.

Conversely, if $\langle x,y\rangle=a\langle x_1,y_1\rangle + b\langle x_2,y_2\rangle$ where $a\ge0$, $b\ge0$, and $a+b>1$, then

$u\langle x,y\rangle=t\langle x_1,y_1\rangle + (1-t)\langle x_2,y_2\rangle$ where $u=\frac{1}{a+b}$ and $t=\frac{a}{a+b}$ satisfy $0<u<1$ and $0\le t\le 1$.

Solving the equations $x=ax_1+by_1$ and $y=ay_1+by_2$ for $a$ and $b$ gives

$\displaystyle a=\frac{xy_2-yx_2}{x_1y_2-x_2y_1}$ and $\displaystyle b=\frac{yx_1-xy_1}{x_1y_2-y_1x_2}$, so $\;\;\displaystyle a+b=\frac{x(y_2-y_1)-y(x_2-x_1)}{x_1y_2-x_2y_1}.$

Therefore the wall will block the line segment from O to P when

$\displaystyle\frac{xy_2-yx_2}{x_1y_2-x_2y_1}\ge0$, $\;\;\displaystyle\frac{yx_1-xy_1}{x_1y_2-y_1x_2}\ge0$, and $\displaystyle \frac{x(y_2-y_1)-y(x_2-x_1)}{x_1y_2-x_2y_1}>1$.

0
On

The problem is equivalent to check whether the segment drawn from the origin to P crosses the segment from A to B.

The equation of the line crossing the origin and $P$ has slope $Y/X$ and intercept zero. The equation of the line crossing $A$ and $B$ has slope $\displaystyle \frac{Y_2-Y_1}{X_2-X_1}$ and intercept $\displaystyle \frac{X_2 Y_1-X_1 Y_2}{X_2-X_1}$. This allows to calculate the coordinates of their intersection point. Let us call them $X_3$ and $Y_3$. We have now to check whether this point is included in the segment from the origin to $P$ and in the segment $AB$.

Considering that each of the coordinates above can be positive or negative, the segment from the origin to $P$ cannot be drawn without crossing the wall if both these conditions are satisfied:

1) $X_3$ falls in the interval between $0$ and $X$ (or alternatively $Y_3$ falls in the interval between $0$ and $Y$);

2)$X_3$ falls in the interval between $X_1$ and $X_2$ (or, alternatively, $Y_3$ falls in the interval between $Y_1$ and $Y_2$).

If satisfied, the first condition implies that the crossing point between the two lines is included in the segment from the origin to $P$, whereas the second one implies that the crossing point between the two lines is also included in the segment $AB$. The segment from the origin to $P$ can be drawn without crossing the wall if at least one of the two conditions is not satisfied.