zoukankan      html  css  js  c++  java
  • 物理模型建模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

  • 相关阅读:
    导出表结构语句
    closeChannel: close the connection to remote address[] result: true
    spingboot使用rabbitmq
    服务器很卡问题排查
    docker-compose安装nginx
    Docker方式安装ShowDoc
    "docker build" requires exactly 1 argument
    Intellij IDEA常用快捷键介绍 Intellij IDEA快捷键大全汇总
    IDEA 2018 3.4 激活破解方法
    jpress:v3.2.5的docker-compose安装
  • 原文地址:https://www.cnblogs.com/aarond/p/12233366.html
Copyright © 2011-2022 走看看