在 Julia 上的像素网格中绘制点

问题描述 投票:0回答:1

我正在使用此算法来检查它是否给出连续的像素点,同时在点 (1,1) 和 (4,13) 之间绘制一条线:

using Plots
function plotline(x1,y1,x2,y2)
    a=(x2-x1)/(y2-y1)
    c=(x2*y1-x1*y2)/(y2-y1)
    y=collect(y1:y2)
    x=y.*a
    x=round.(x.-c)
    display(plot(x,y,label="plotline",shape=:circle, color=:red,))
    #println(x)

end

plotline(1,1,4,13)

绘图命令给了我一条连续的线,这并没有多大帮助。我想知道是否有一种方法可以像这样绘制点: Grid Example

我的第一个想法是我是否可以以某种方式将其变成一种热图,但我不太习惯它们。有什么想法吗?

plot graphics julia pixel line-plot
1个回答
0
投票

你确实可以使用热图来可视化——怎么样?尚未完全检查所有边缘情况(即假设 x2>x1 且 y2>y1),但这应该是一个很好的起点......有趣的问题!似乎计算机图形学一直在幕后做的事情就是找出要点亮的像素。

using Plots
x1 = 1; y1 = 1; x2 = 4; y2 = 13; pixSize=1
function genPixelLine(x1,y1,x2,y2,pixSize=1)
    m = (y2-y1)/(x2-x1); b = y2-m*x2 #slope and intercept of line
    xline = x1:x2; line = m.*xline.+b #line itself
    xpix = x1:pixSize:x2; ypix = y1:pixSize:y2 #pixel centers
    pixels = zeros(length(xpix),length(ypix)) #initialize pixel grid
    xMins = xpix.-(pixSize/2); xMaxs = xpix.+(pixSize/2) #bounding edges in x
    for (yBin,yp) in enumerate(ypix) #go through bounding horizontal lines
        yMin = yp-pixSize/2; yMax = yp+pixSize/2 #top and bottom bounds
        xMin = (yMin-b)/m; xMax = (yMax-b)/m #x values at top and bottom bounds
        xBins = (xMax.>xMins) .&& (xMin.<=xMaxs) #we want the maximum x to greater than the minimum bounding line in x and vice versa
        pixels[xBins,yBin] .= 1.0 #light pixel up where conditions are satisfied
    end
    return (xpix,ypix,pixels),(xline,line)
end
        
pix,l = genPixelLine(x1,y1,x2,y2,pixSize)
heatmap(pix[1],pix[2],pix[3]',colorbar=false) #note have to plot transpose because of how heatmap works internally, pix[1] and pix[2] are the 1D x and y arrays of pixel centers
plot!(l[1],l[2],lw=2,label="line")
vline!([pix[1].-pixSize/2,pix[1].+pixSize/2],label="",c=:purple) #visualize the bounding boxes the function calculated to determine which cells to light up
hline!([pix[2].-pixSize/2,pix[2].+pixSize/2],c=:purple,label="pixel edges")
plot!(widen=false,tickdirection=:out,aspect_ratio=:equal,
    xlims=(x1-pixSize/2,x2+pixSize/2),ylims=(y1-pixSize/2,y2+pixSize/2),
    legend=:outerright,xlabel="x",ylabel="y",size=(600,800))

输出以下图:

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.