draw.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3. PI = np.pi
  4. class Arrow:
  5. def __init__(self, x, y, theta, L, c):
  6. angle = np.deg2rad(30)
  7. d = 0.5 * L
  8. w = 2
  9. x_start = x
  10. y_start = y
  11. x_end = x + L * np.cos(theta)
  12. y_end = y + L * np.sin(theta)
  13. theta_hat_L = theta + PI - angle
  14. theta_hat_R = theta + PI + angle
  15. x_hat_start = x_end
  16. x_hat_end_L = x_hat_start + d * np.cos(theta_hat_L)
  17. x_hat_end_R = x_hat_start + d * np.cos(theta_hat_R)
  18. y_hat_start = y_end
  19. y_hat_end_L = y_hat_start + d * np.sin(theta_hat_L)
  20. y_hat_end_R = y_hat_start + d * np.sin(theta_hat_R)
  21. plt.plot([x_start, x_end], [y_start, y_end], color=c, linewidth=w)
  22. plt.plot([x_hat_start, x_hat_end_L],
  23. [y_hat_start, y_hat_end_L], color=c, linewidth=w)
  24. plt.plot([x_hat_start, x_hat_end_R],
  25. [y_hat_start, y_hat_end_R], color=c, linewidth=w)
  26. class Car:
  27. def __init__(self, x, y, yaw, w, L):
  28. theta_B = PI + yaw
  29. xB = x + L / 4 * np.cos(theta_B)
  30. yB = y + L / 4 * np.sin(theta_B)
  31. theta_BL = theta_B + PI / 2
  32. theta_BR = theta_B - PI / 2
  33. x_BL = xB + w / 2 * np.cos(theta_BL) # Bottom-Left vertex
  34. y_BL = yB + w / 2 * np.sin(theta_BL)
  35. x_BR = xB + w / 2 * np.cos(theta_BR) # Bottom-Right vertex
  36. y_BR = yB + w / 2 * np.sin(theta_BR)
  37. x_FL = x_BL + L * np.cos(yaw) # Front-Left vertex
  38. y_FL = y_BL + L * np.sin(yaw)
  39. x_FR = x_BR + L * np.cos(yaw) # Front-Right vertex
  40. y_FR = y_BR + L * np.sin(yaw)
  41. plt.plot([x_BL, x_BR, x_FR, x_FL, x_BL],
  42. [y_BL, y_BR, y_FR, y_FL, y_BL],
  43. linewidth=1, color='black')
  44. Arrow(x, y, yaw, L / 2, 'black')
  45. # plt.axis("equal")
  46. # plt.show()
  47. if __name__ == '__main__':
  48. # Arrow(-1, 2, 60)
  49. Car(0, 0, 1, 2, 60)