fork(1) download
  1. import cv2
  2. import numpy as np
  3. import math
  4.  
  5.  
  6. def proj(x1, y1, x2, y2, xp, yp):
  7. x12 = x2 - x1
  8. y12 = y2 - y1
  9. dotp = x12 * (xp - x1) + y12 * (yp - y1)
  10. dot12 = x12 * x12 + y12 * y12
  11. if dot12:
  12. coeff = dotp / dot12
  13. lx = x1 + x12 * coeff
  14. ly = y1 + y12 * coeff
  15. return np.int(lx), np.int(ly)
  16. else:
  17. return None
  18.  
  19.  
  20. def perpendicular_finder(line, point):
  21. x1, y1 = line[0]
  22. x2, y2 = line[1]
  23. x3, y3 = point
  24. if ((y2 - y1) ^ 2 + (x2 - x1) ^ 2) !=0:
  25. k = ((y2 - y1) * (x3 - x1) - (x2 - x1) * (y3 - y1)) / ((y2 - y1) ^ 2 + (x2 - x1) ^ 2)
  26. x4 = x3 - k * (y2 - y1)
  27. y4 = y3 + k * (x2 - x1)
  28. x4 = np.int(x4)
  29. y4 = np.int(y4)
  30.  
  31. return x4,y4
  32.  
  33.  
  34. cnt = 0
  35.  
  36. ballCircle = (200, 200)
  37. purBall = (540,300)
  38. cueX1 = 200
  39. cueY1 = 200
  40. cueX2 = 400
  41. cueY2 = 400
  42. count = 0
  43. while True:
  44. if count < 400:
  45. cueX2 -= 1
  46. elif count < 800:
  47. cueY2 -= 1
  48. elif count < 1200:
  49. cueX2 += 1
  50. else:
  51. cueY2 += 1
  52. if count == 1600:
  53. count = 0
  54. else:
  55. count += 1
  56.  
  57. cueX2 = cueX1 + int(500 * math.cos(cnt / 1000))
  58. cueY2 = cueY1 + int(500 * math.sin(cnt / 1000))
  59. cueX3 = cueX1 - int(500 * math.cos(cnt / 1000))
  60. cueY3 = cueY1 - int(500 * math.sin(cnt / 1000))
  61. cnt += 1
  62.  
  63. blank = np.zeros((500, 900, 3), np.uint8) # Create canvas the size of table
  64.  
  65. #kek = perpendicular_finder((ballCircle, (cueX2,cueY2)), purBall)
  66. kek = proj(ballCircle[0], ballCircle[1], cueX2, cueY2, purBall[0], purBall[1])
  67.  
  68. cv2.line(blank, purBall, kek, (0, 255, 255), 1) # good path
  69. cv2.circle(blank, ballCircle, 10, (255, 255, 255), -1) # Ball
  70. cv2.circle(blank, purBall, 10, (0, 255, 255), -1) # Purple Ball
  71. cv2.arrowedLine(blank, (cueX1, cueY1), (cueX2, cueY2), (255, 255, 255), 3) # Cue
  72. cv2.arrowedLine(blank, (cueX1, cueY1), (cueX3, cueY3), (100, 100, 100), 2) # Cue
  73. cv2.circle(blank, kek, 10, (0, 0, 255), -1) # Ball
  74.  
  75. cv2.imshow("kk", blank)
  76. if cv2.waitKey(1) & 0xFF == ord('q'):
  77. break
  78. cv2.destroyAllWindows()
Runtime error #stdin #stdout #stderr 0.01s 7384KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Traceback (most recent call last):
  File "prog.py", line 1, in <module>
ImportError: No module named cv2