robot.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import numpy as np
  2. class Robot():
  3. # frente
  4. # ---------
  5. # | |
  6. # | |
  7. # | LLL |
  8. # | LLL |
  9. # ---------
  10. # idd, id do robo
  11. # xsize, largura do robo
  12. # xsize, comprimento do robo
  13. # xtag, distancia horizontal do lado esquerdo ao cento do tag
  14. # ytag, distancia vertical do topo ao cento do tag
  15. # posx, posy, course, posicao do robo
  16. def __init__(self, idd, xsize, ysize, xtag, ytag, posx = 0, posy = 0, course = 0):
  17. self.idd = idd
  18. self.xsize = xsize
  19. self.ysize = ysize
  20. self.xtag = xtag
  21. self.ytag = ytag
  22. self.posx = posx
  23. self.posy = posy
  24. self.course = course
  25. def lines(self):
  26. p1 = np.array([-self.xtag, self.ytag])
  27. p2 = np.array([-self.xtag, -self.ysize+self.ytag])
  28. p3 = np.array([self.xsize-self.xtag, self.ytag])
  29. p4 = np.array([self.xsize-self.xtag, -self.ysize+self.ytag])
  30. pos = np.array([self.posx, self.posy])
  31. c, s = np.cos(self.course), np.sin(self.course)
  32. R = np.array(((c,-s), (s, c)))
  33. p1 = p1.dot(R)+pos
  34. p2 = p2.dot(R)+pos
  35. p3 = p3.dot(R)+pos
  36. p4 = p4.dot(R)+pos
  37. return np.array([[p1, p2], [p2, p4], [p4, p3], [p1, p3]])
  38. def ccw(self, A,B,C):
  39. return (C[1]-A[1]) * (B[0]-A[0]) > (B[1]-A[1]) * (C[0]-A[0])
  40. # Return true if line segments AB and CD intersect
  41. def line_intersect(self, A,B,C,D):
  42. return self.ccw(A,C,D) != self.ccw(B,C,D) and self.ccw(A,B,C) != self.ccw(A,B,D)
  43. # get robot and see if this intersect te other
  44. def intersectRobot(self, robot):
  45. thislines = self.lines()
  46. other = robot.lines()
  47. for i in thislines:
  48. for j in other:
  49. if self.line_intersect(i[0], i[1], j[0], j[1]):
  50. return True
  51. return False
  52. # get robot and see if this intersect te other
  53. def intersectLine(self, line):
  54. thislines = self.lines()
  55. for i in thislines:
  56. if self.line_intersect(i[0], i[1], (line[0], line[1]), (line[2], line[3])):
  57. return True
  58. return False