2
$\begingroup$

I have two points $(0,0)$ and $(93,3)$

I'm trying to work out whether a point is on or below the line segment defined by those two point.

Currently I'm using $Ax+By+C=0$ to see if a point is on or below this line segment and this works correctly except for when the point is of the form $(0,y)$ or $(x,0)$

What am I doing wrong? Do I need to use a different form of the linear equation?

5 Answers 5

0

Put the point in the equation of line if the sign of the number you get is same as the coefficient of $y$ then point is above the line. But if the signs of coefficient of $y$ and the answer are different then point is below the line.

4

What you need is orientation test.

Here is a nice picture from Jonathan Richard Shewchuk: Adaptive Precision Floating-Point Arithmetic and Fast Robust Predicates for Computational Geometry:

In your case $(a_x, a_y) = (0, 0)$ and $(b_x, b_y) = (93, 3).$ Finally, $(c_x, c_y)$ are the coordinates of the test point. Evaluate the determinant, and the sign of the determinant indicates whether $c$ is to the {left, right, or on} the line $ab.$

3

For this particular example it would seem to be easier to write the equation for the line in slope-intercept form: $ y = \frac{1}{31} x + 0$ Then given any $(x,y)$, you can test directly whether the point lies above or below the point on the line that has the same $x$ coordinate.

2

Suppose your given points are $(x_1,y_1)$ and $(x_2,y_2)$. First get the slope $m = \frac{y_1-y_2}{x_1-x_2}$. The equation of the line joining the points is $y- y_1 = m(x-x_1)$, where you just substitute $x_1,y_1$, and $m$. In slope intercept form the equation is $ y = mx + (y_1-mx_1). $ Given a point $(p,q)$, first check that $x_1\le p\le x_2$. Then: $q > mp + (y_1-mx_1)$ means the point is above the segment, and $q< mp + (y_1-mx_1)$ means the point is below the segment.

If you already have the equation of the line joining the points as $Ax + By + C= 0$, then the equation of the line can be expressed as $y = -\frac{B}{A}x - \frac{C}{A}$, so just check to see whether $q > -\frac{B}{A}p - \frac{C}{A}$ or not.

0

Here is a solution:

A vector can be created connecting (0,0) and (93,3) called $\hat v$, Now take your point $P(x,y)$ which you need to analyze for location. Another vector can be created $\hat w = P - (0,0)$

Carry out $\hat v \times \hat w$, if the vector is positive, the w is to the left of the line else it is to the right.

Here is an example:

$v = 93i+3j$ $w=10i$ $v\times w = -30k$ Thus, this point (10,0) lies to the right of your line.