Andrew, thanks for the link! I spent quite some time to rewrite and
optimize code for Julia, but it was worth it: for 100x100 matrix
intersection-based algorithms gives almost 10 times speedup, and the larger
the matrix, the higher is the difference.
Here's my new code:
function fillpoly!{T}(M::Matrix{T}, px::Vector{Int}, py::Vector{Int},
value::T)
@assert length(px) == length(py)
left, right = minimum(px), maximum(px)
top, bottom = minimum(py), maximum(py)
@inbounds for x=left:right
ys = Set{Int64}()
j = length(px)
for i=1:length(px)
if (px[i] <= x && x <= px[j]) || (px[j] <= x && x <= px[i])
# special case: adding the whole cut to ys
if px[i] == px[j]
push!(ys, py[i])
push!(ys, py[j])
else
y = py[i] + (x - px[i]) / (px[j] - px[i]) * (py[j] -
py[i])
push!(ys, int(y))
end
end
j = i
end
ys = sort([y for y in ys])
# if there's an odd number of intersection points, add one
imeginary point
if length(ys) % 2 == 1
push!(ys, ys[end])
end
for i=1:2:length(ys)
M[ys[i]:ys[i+1], x] = value
end
end
return M
end
function poly2mask(px::Vector{Int}, py::Vector{Int}, m::Int, n::Int)
mask = zeros(Bool, m, n)
fillpoly!(mask, px, py, true)
end
On Thursday, January 29, 2015 at 4:15:10 AM UTC+3, Patrick O'Leary wrote:
>
> On Wednesday, January 28, 2015 at 4:05:07 PM UTC-6, Andrew McLean wrote:
>>
>> [Apologies if you see this post twice, it's been a number of hours since
>> my original post and it hasn't appeared.]
>>
>
> Sorry about that--I have no idea how it sat in the queue for so long. I
> discarded the original post and kept only the repost. Further posts should
> appear immediately.
>
> Patrick
>