Skip to content

Boundary checks #18

@mschauer

Description

@mschauer

Drawing ellipses and paths can produce out of bound errors. I am working on something else a.t.m but this is what fixed it for me:

function setifinbounds!_(A::AbstractArray{T,2}, i, j, c::T) where {T}
    if checkbounds(Bool, A, i, j)
        @inbounds A[i, j] = c
    end
    c
end
function draw!(img::AbstractArray{T, 2}, ellipse::Ellipse, color::T) where T<:Colorant
    ps = Tuple{Int,Int}[]
    for i in ellipse.center.y : ellipse.center.y + ellipse.ρy
        for j in ellipse.center.x : ellipse.center.x + ellipse.ρx
            val = ((i - ellipse.center.y) / ellipse.ρy) ^ 2 + ((j - ellipse.center.x) / ellipse.ρx) ^ 2
            if val < 1
                push!(ps, (i, j))
            end
        end
    end
    for (yi, xi) in ps
        setifinbounds!_(img, yi, xi, color)
        setifinbounds!_(img, 2 * ellipse.center.y - yi, xi, color)
        setifinbounds!_(img, yi, 2 * ellipse.center.x - xi, color)
        setifinbounds!_(img, 2 * ellipse.center.y - yi, 2 * ellipse.center.x - xi, color)
    end
    img
end
function draw!(img::AbstractArray{T, 2}, path::Path, color::T) where T<:Colorant
    vertices = [CartesianIndex(p.y, p.x) for p in path.vertices]
    f = CartesianIndex(map(r->first(r)-1, indices(img)))
    l = CartesianIndex(map(r->last(r), indices(img)))

    inrange1 = min(f, vertices[1])==f && max(l, vertices[1])==l
    for i in 1:length(vertices)-1
        inrange2 = min(f,vertices[i+1])==f && max(l,vertices[i+1])==l
        if inrange1 && inrange2
            draw!(img, LineSegment(vertices[i], vertices[i+1]), color)
        end
        inrange1 = inrange2
    end
end

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions