物理模型建模demo

第一次做物理建模这事,还挺有意思,由于只是demo,没有深入研究下去,所以这个目标也就比较浅显,只是:检测旋转到某1角度不要发生碰撞,不带重力

 

 

from shapely.geometry import Polygon
from shapely import affinity
import matplotlib.pyplot as plt

def make_car(leftX, leftY, width, height):
    car_lines = Polygon([(leftX, leftY), (leftX+width, leftY), (leftX+width, leftY+height), (leftX, leftY+height), (leftX, leftY)])
    return car_lines

def showCar(car):
    # x, y = car.xy
    x, y=car.exterior.xy
    plt.plot(x, y)

def showCars(cars):
    for car in cars:
        showCar(car)


car_width=4.7
car_height=2.5

car1 = make_car(1, 1, car_width, car_height)
car2 = make_car(6, 1, car_width, car_height)
car3 = make_car(11, 1, car_width, car_height)
car4 = make_car(16, 1, car_width, car_height)

car5 = make_car(1, 6, car_width, car_height)
car6 = make_car(6, 6, car_width, car_height)
car7 = make_car(11, 6, car_width, car_height)
car8 = make_car(16, 6, car_width, car_height)

plt.axis('equal')
plt.ion()
cars = [car1, car2, car3, car4, car5, car6, car7, car8]
showCars(cars)

plt.pause(1)


for i in range(1, 30):
    plt.cla()
    rotated_car3 = affinity.rotate(car3, i)
    showCars([car1, car2, rotated_car3, car4, car5, car6, car7, car8])
    #计算是否碰撞

    print('=======================', i, '====================')
    cars = [car1, car2, car4, car5, car6, car7, car8]
    conflict=False
    for car in cars:
        if rotated_car3.intersects(car):
            conflict=True
            break

    if conflict:
        print('撞到了,当前旋转角度:', i)
        break

    plt.pause(0.1)

plt.ioff()
plt.show()

  

 

 

代码比较简单,用到的有几何库:shapely  https://shapely.readthedocs.io/en/stable/manual.html

 

posted @ 2020-01-25 20:34  McKay  阅读(552)  评论(0编辑  收藏  举报