__author__ = 'Lenovo' import math from math import pi import numpy as np import matplotlib.pyplot as plt def makePath(alpha, v0, k, eps, xmax, ymax): # print alpha alpha = math.radians(alpha) # print alpha vx = v0 * np.cos(alpha) vy = v0 * np.sin(alpha) xcur = xmax*0.5 ycur = ymax*0.5 while vx**2 + vy**2 > eps: plt.bar(0, ymax, xmax, 0, color = 'w') if alpha <= pi/2: if ycur + (xmax - xcur) * np.tan(alpha) > ymax: #top t1 = np.linspace(xcur, xcur + (ymax - ycur)/ np.tan(alpha), 2) plt.plot(t1, np.tan(alpha)*t1 + ycur) xcur = xcur + (ymax - ycur)/ np.tan(alpha) ycur = ymax alpha = 2*pi - alpha else: #right t1 = np.linspace(xcur, xmax, 2) plt.plot(t1, np.tan(alpha)*t1 + ycur) ycur = ycur + xmax * np.tan(alpha) xcur = xmax alpha = pi - alpha elif alpha <= pi: if ycur + abs(xcur * np.tan(alpha)) > ymax: #top t1 = np.linspace(xcur - (ymax - ycur)/ np.tan(alpha), xcur, 2) plt.plot(t1, np.tan(alpha)*t1 + ycur + abs(xcur * np.tan(alpha))) xcur = xcur - (ymax - ycur)/ np.tan(alpha) ycur = ymax alpha = 2*pi - alpha else: #left t1 = np.linspace(0, xcur, 2) plt.plot(t1, np.tan(alpha)*t1 + ycur + abs(xcur * np.tan(alpha))) ycur = ycur + abs(xcur * np.tan(alpha)) xcur = 0 alpha = pi - alpha elif alpha <= 3*pi/2: if ycur - abs(xcur * np.tan(alpha)) < 0: #bottom t1 = np.linspace(xcur - ycur/ np.tan(alpha), xcur, 2) plt.plot(t1, np.tan(alpha)*t1 + ycur - abs(xcur * np.tan(alpha))) xcur = xcur - ycur/ np.tan(alpha) ycur = 0 alpha = 2*pi - alpha else: #left t1 = np.linspace(0, xcur, 2) plt.plot(t1, np.tan(alpha)*t1 + ycur - abs(xcur * np.tan(alpha))) ycur = ycur - abs(xcur * np.tan(alpha)) xcur = 0 alpha = 2*pi - alpha else: if ycur - abs(xcur * np.tan(alpha)) < 0: #bottom t1 = np.linspace(xcur - ycur/ np.tan(alpha), xcur, 2) plt.plot(t1, np.tan(alpha)*t1 + ycur - abs(xcur * np.tan(alpha))) xcur = xcur - ycur/ np.tan(alpha) ycur = 0 alpha = 2*pi - alpha else: #left t1 = np.linspace(0, xcur, 2) plt.plot(t1, np.tan(alpha)*t1 + ycur - abs(xcur * np.tan(alpha))) ycur = ycur - abs(xcur * np.tan(alpha)) xcur = 0 alpha = 2*pi - alpha vx = vx*k vy = vy*k plt.show() makePath(25, 15, 0.5, 0.1, 10, 20)