-1

Let's say you have a two-dimensional plane with 2 points (called a and N1 and N2) on it.

N1= [9 * H // 8 , 0] N2= [9 * H // 8 , W]

How do I check if my third point which is P= (int(x1), int(y1)) is intersecting the line that is crossing N1 and N2? (please note that p or third point might be moving anywhere on the plane, I only what not know when it is on the line)

So let's say

enter image description here

what I can think of is:

            N1 = np.array([9 * H // 8, 0])
            N2 = np.array([9 * H // 8, W]);

            p_aftertop = (int(x1), int(y1))

            v1 = (0,W )  # Vector 1
            v2 = (9 * H // 8 - int(x1),  W- int(y1))  # Vector 1

            xp = v1[0] * v2[1] - v1[1] * v2[0]  # Cross product
            if (xp != 0):
                status = cv2.imwrite('/img' + str(i) + '.jpg', frame)
                print('on the line')
                i +=1

I do not know why my method does not work well. How do I know when this point is on the line that crosses those two points?

nikki
  • 365
  • 4
  • 20
  • Since you have two coordinates, get the equation of the line and check if point p3 satisfies the equation. – Seyi Daniel Aug 07 '20 at 15:30
  • 1
    Have you considered using [Shapely library for this operation](https://shapely.readthedocs.io/en/latest/manual.html#object.intersects)? If this solely an algorithmic exercise, you can know that if x-coordiante of P is the same as N1 and N2 x-coordinate based on your sample which is a pretty naive solution. The comment above provides a generalised approach to any line and point. – Oluwafemi Sule Aug 07 '20 at 15:31
  • Either all `x` coordinates or the difference quotiens of the two part of the connection are the same. – Klaus D. Aug 07 '20 at 15:36
  • no, it does not, – nikki Aug 07 '20 at 17:23

2 Answers2

0

The point lies within the line if it satisfies the following conditions:

1.Slopes made by p1 to the endpoints of lines should be equal.(and opposite in direction)

2.Point p1 should be within the boundaries of x or y coordinates of endpoints of the lines.

Pseudo code

bool liesOnline:
  if (slope(N1,p1) ==slope(N1,N2) and (N1x <= p1x <= N2x  or N1y<= p1y <= N2y) 
   return true;
  else 
   return false;

where N1x is x coordinate of N1.

Note that slope of the line is a mathematical function to be computed between two points.

Leo
  • 35
  • 9
0

Your approach is fine - but small correction is needed

if (xp == 0):
MBo
  • 77,366
  • 5
  • 53
  • 86