2017-01-20 1 views
0

Я пытаюсь повернуть набор прямоугольников, которые должны представлять автомобиль в Julia, используя matplotlib. Следующий код работает. Однако, когда я пытаюсь повернуть автомобиль, похоже, он становится толще, когда он приближается к 90 градусам. Вот код:Поворот нескольких прямоугольников вокруг общей точки в Julia с использованием matplotlib

using PyCall 
using PyPlot 
@pyimport matplotlib.animation as animation 
@pyimport matplotlib.patches as patches 
@pyimport matplotlib as mpl 




function drawCar(pos::Array{Float64}) 

    x = pos[1] 
    y = pos[2] 
    psi = pos[3] 
    pos = [x;y] 

    # define outer car geometry 
    length = 36.5/100.0 
    width = 20.0/100.0 
    btmLeft_vertex = pos + [-length/2,-width/2] 


    # define tire geometry 
    tire_width = 2.0/100.0 
    tire_length = 5.0/100.0 
    tire_distance = 8.0/100.0 

    # define patches that make up the car's appearance 
    # car for outer boundary and tires at tl=top left, tr=top right, bl=bottom left, br=bottom right 
    car = patches.Rectangle(btmLeft_vertex,length,width,fill=false, linewidth=3.0,color="black") 
    tl = patches.Rectangle(pos + [-tire_distance-tire_length,width/2-tire_width],tire_length,tire_width,color="black") 
    tr = patches.Rectangle(pos + [tire_distance,width/2-tire_width],tire_length,tire_width,color="black") 
    bl = patches.Rectangle(pos + [-tire_distance-tire_length,-width/2],tire_length,tire_width,color="black") 
    br = patches.Rectangle(pos + [tire_distance,-width/2],tire_length,tire_width,color="black") 

    car_parts = [car,tl,tr,bl,br] 

    return car_parts  
end 

function updateCarParts(fig,car_parts,angle) 


    ax = gca() 
    # define a rotation transformation and a transformation from data coordinate system to display coordinate system 
    t1 = mpl.transforms[:Affine2D]() 
    t1[:rotate_deg_around](0.0,0.0, angle) 
    t2 = ax[:transData] 
    # combine transformations 
    t3 = t1[:__add__](t2) 

    # apply rotation transformation to all car parts 
    for p in car_parts 
     p[:set_transform](t3) 
    end 
    return car_parts 

end 


# Play/Start the animation 
function play() 

    fig = figure() 
    hold(true) 

    # define initial position and orientation of the car 
    pos = [0.0,0.0,0.0] 

    # get the patches that make up the car and add them to the current figure 
    car_parts = drawCar(pos) 
    ax = gca() 
    for p in car_parts 
     ax[:add_patch](p) 
    end 

    # define animation function 
    function animate(frameNum) 
     frameNum += 1 
     # update the orientation of the car parts 
     angle = frameNum 
     car_parts = updateCarParts(fig,car_parts,angle) 
     return nothing 
    end 

    # define animation object 
    anim = animation.FuncAnimation(fig, animate, frames=200, interval=50,repeat=true) 
    axis([-0.5,0.5,-0.5,0.5],"equal") 
    grid(true, which="both") 

    end 

Я не вижу, что я делаю неправильно. Спасибо за помощь.

Редактировать: Вот два изображения с 0 градусами (сверху) и повернуты на 90 градусов (внизу). Автомобиль в нижнем изображении кажется более толстым. slim car (0 deg) thick car (90 deg)

ответ

0

Я считаю (не проверено), что вам нужно
axis([-0.5,0.5,-0.5,0.5], "equal") , когда вы делаете каждый участок, то есть в функции animate.

+0

Спасибо. Но это не меняет поведения. – miga89

+0

Можете ли вы показать фотографии проблемы? Что-то другое происходит в Python? –

+0

Я добавил два изображения для объяснения – miga89

0

Добавление команды равной оси в конце функции play() решило проблему. Спасибо, Дэвид П. Сандерс.

Вот обновленный код:

using PyCall 
using PyPlot 
@pyimport matplotlib.animation as animation 
@pyimport matplotlib.patches as patches 
@pyimport matplotlib as mpl 

function drawCar(pos::Array{Float64}) 
    x = pos[1] 
    y = pos[2] 
    psi = pos[3] 
    pos = [x;y] 

    # define outer car geometry 
    length = 36.5/100.0 
    width = 20.0/100.0 
    btmLeft_vertex = pos + [-length/2,-width/2] 


    # define tire geometry 
    tire_width = 2.0/100.0 
    tire_length = 5.0/100.0 
    tire_distance = 8.0/100.0 

    # define patches that make up the car's appearance 
    # car for outer boundary and tires at tl=top left, tr=top right, bl=bottom left, br=bottom right 
    car = patches.Rectangle(btmLeft_vertex,length,width,fill=false, linewidth=3.0,color="black") 
    tl = patches.Rectangle(pos + [-tire_distance-tire_length,width/2-tire_width],tire_length,tire_width,color="black") 
    tr = patches.Rectangle(pos + [tire_distance,width/2-tire_width],tire_length,tire_width,color="black") 
    bl = patches.Rectangle(pos + [-tire_distance-tire_length,-width/2],tire_length,tire_width,color="black") 
    br = patches.Rectangle(pos + [tire_distance,-width/2],tire_length,tire_width,color="black") 

    car_parts = [car,tl,tr,bl,br] 

    return car_parts  
end 

function updateCarParts(fig,car_parts,angle) 
    ax = gca() 
    # define a rotation transformation and a transformation from data coordinate system to display coordinate system 
    t1 = mpl.transforms[:Affine2D]() 
    t1[:rotate_deg_around](0.0,0.0, angle) 
    t2 = ax[:transData] 
    # combine transformations 
    t3 = t1[:__add__](t2) 

    # apply rotation transformation to all car parts 
    for p in car_parts 
     p[:set_transform](t3) 
    end 
    return car_parts 
end 

# Play/Start the animation 
function play() 

    fig = figure() 
    hold(true) 

    # define initial position and orientation of the car 
    pos = [0.0,0.0,0.0] 

    # get the patches that make up the car and add them to the current figure 
    car_parts = drawCar(pos) 
    ax = gca() 
    for p in car_parts 
     ax[:add_patch](p) 
    end 

    # define animation function 
    function animate(frameNum) 
     frameNum += 1 
     # update the orientation of the car parts 
     angle = frameNum 
     car_parts = updateCarParts(fig,car_parts,angle) 
     return nothing 
    end 

    # define animation object 
    anim = animation.FuncAnimation(fig, animate, frames=200, interval=50,repeat=true) 
    grid(true, which="both") 
    ax[:set_xlim]([-0.5,0.5]) 
    ax[:set_ylim]([-0.5,0.5]) 
    ax[:set_aspect]("equal") 
end 
Смежные вопросы