title: Python OpenCV: Getting a x/y point with a slope and another coordinate
body: <p>I am trying to draw a straight line representing the path of a linear moving object. I work out the slope by getting the first point <code>(oX,oY)</code> and the second point <code>(x3, y3)</code> and doing <code>((y3-oY)/(x3-oX))</code>. I now have a point <code>(x3, y3)</code> and the slope. Depending on where the line is going e.g if it intersects the left, right, up or down border gives me another point based on the height and length of the window (640px, 480px). So if the line intersects the left side, the x value is 0 and all I would need to do is find the y value. How can I do this with all 4 sides? My code is below and currently draws the line fine but does not stop at the top or bottom. e.g the y value is not either 0 or 480. The reason I would need the line to stop at the edge of the window is to then draw an identical line with an inverted slope to represent if the linear object were to bounce. Thank you!</p>
  
  <pre><code>import cv2
  import numpy as np
  import math
  
  lower_red = np.array([-10,160,160])
  upper_red = np.array([10,255,255])
  
  oX, oY = 0,0
  
  cap = cv2.VideoCapture(0)
  if not cap.isOpened():
      print("Cannot open camera")
      exit()
  while(1):
      ret, frame = cap.read()
      if not ret:
          print("Can't receive frame (stream end?). Exiting ...")
          break
  
      hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
      mask = cv2.inRange(hsv, lower_red, upper_red)
      #ret, thresh = cv2.threshold(mask, 80, 255, cv2.THRESH_BINARY)
      contours, _ = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
  
      if len(contours) != 0:
          c = max(contours, key = cv2.contourArea)
          x1, y1, w, h = cv2.boundingRect(c)
          x2, y2 = x1 + w, y1 + h
          cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
          x3, y3 = round((x1+x2)/2), round((y1+y2)/2)
          cv2.circle(frame, (x3,y3), 4, (255,0,0), 2)
          #print(x3, y3)
  
          def slope(oX,oY,x3,y3):
          ###finding slope
              if x3!=oX:
                  return((y3-oY)/(x3-oX))
              else:
                  return 'NA'
  
          slope(oX,oY,x3,y3)
  
      def drawLine(frame,oX,oY,x3,y3):
          m=slope(oX,oY,x3,y3)
          h,w=frame.shape[:2]
          if m!='NA':
              ### here we are essentially extending the line to x=0 and x=width
              ### and calculating the y associated with it
              ##starting point
              px=0
              py=-(oX-0)*m+oY
              ##ending point
              qx=w
              qy=-(x3-w)*m+y3
          else:
          ### if slope is zero, draw a line with x=x1 and y=0 and y=height
              px,py=oX,0
              qx,qy=oX,h
  
          if x3 &lt; oX and y3 &lt; oY:
              qx, qy = oX, oY
              cv2.line(frame, (int(px), int(py)), (int(qx), int(qy)), (0, 255, 0), 2)
          elif x3 &lt; oX and y3 &gt; oY:
              qx, qy = oX, oY
              cv2.line(frame, (int(px), int(py)), (int(qx), int(qy)), (0, 255, 0), 2)
          elif x3 &gt; oX and y3 &lt; oY:
              px, py = oX, oY
              cv2.line(frame, (int(px), int(py)), (int(qx), int(qy)), (0, 255, 0), 2)
          elif x3 &gt; oX and y3 &gt; oY:
              px, py = oX, oY
              cv2.line(frame, (int(px), int(py)),(int(qx), int(qy)), (0, 255, 0), 2)
          #print(int(px),int(py), int(qx), int(qy))
  
  
      drawLine(frame,oX,oY,x3,y3)
      oX, oY = x3, y3
      cv2.imshow('frame', frame)
      cv2.imshow('mask', mask)
  
  
      if cv2.waitKey(1) == ord('q'):
          break
  # When everything done, release the capture
  cap.release()
  cv2.destroyAllWindows()
  </code></pre>