You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何控制Agents.jl代理绘制顺序,让UAV始终显示在Patch之上?

问题:让UAV代理始终显示在Patch地块上方

我正在制作一个包含Patch(绿色地块)和UAV代理的模型可视化/动画,需要让UAV代理始终显示在Patch代理的上方。目前部分UAV被Patch遮挡,部分正常显示,推测是代理绘制顺序导致的问题。附上当前代码及示例图,未找到相关实现案例,考虑过修改Patch的模型步骤,但六边形网格的特性让实现难度很大。

示例图

当前代码

function agent_color(a)
    if a isa Patch
        color = "#007500" # get_color(a.prob_burn)
        if a.status == :on_fire
            color = :red
        elseif a.status == :burnt
            color = :gray55
        end
    elseif a isa UAV
        color = :blue
    else
    end
    color
end

const uav_polygon = Makie.Polygon(Point2f[(-.5,-.5), (1,0), (-.5,.5)])

function uav_marker(u::UAV)
    t = atan(u.vel[2], u.vel[1])
    shape = rotate_polygon(uav_polygon, t)
    return shape
end

function agent_shape(a)
    if a isa Patch
        shape = :hexagon
        if a.status == :on_fire
            shape = :circle
        elseif a.status == :burnt
            shape = :rect
        end
        
    elseif a isa UAV
        shape = uav_marker(a)
    else

    end
    shape
end


figure, _ = Agents.abmplot(forest; ac = agent_color,as = 24, am = agent_shape, scatterkwargs = (strokewidth = 0.1,), figure = (;resolution = (500,500)))

解决方法

之前出现部分遮挡的原因是:Agents.jl默认按代理ID顺序绘制,ID小的代理先画,ID大的后画。如果UAV的ID比Patch小,就会被后面绘制的Patch挡住;反之则会显示在上方。要彻底解决,只需控制绘制顺序,让Patch先画,UAV后画。

方法1:使用agent_order参数(最简方案)

给abmplot添加agent_order参数,定义排序规则让Patch优先绘制:

# 排序函数:返回值越小,代理越先绘制
function agent_order(a)
    a isa Patch ? 0 : 1
end

figure, _ = Agents.abmplot(forest; 
    ac = agent_color,
    as = 24, 
    am = agent_shape, 
    scatterkwargs = (strokewidth = 0.1,), 
    figure = (;resolution = (500,500)),
    agent_order = agent_order  # 指定绘制顺序
)

方法2:分层绘制(更灵活)

如果需要更精细的控制,可分两次调用abmplot!,先画所有Patch,再画所有UAV:

figure = Figure(resolution=(500,500))
ax = Axis(figure[1,1])

# 第一步:绘制所有Patch代理
abmplot!(ax, forest;
    ac = agent_color,
    as = 24,
    am = agent_shape,
    scatterkwargs = (strokewidth = 0.1,),
    filter_func = a -> a isa Patch  # 仅筛选Patch
)

# 第二步:绘制所有UAV代理(会覆盖在Patch上方)
abmplot!(ax, forest;
    ac = agent_color,
    as = 24,
    am = agent_shape,
    scatterkwargs = (strokewidth = 0.1,),
    filter_func = a -> a isa UAV  # 仅筛选UAV
)

display(figure)

内容的提问来源于stack exchange,提问作者cj_cool

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.06 20:12:38