Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Boundary checks #18

Closed
mschauer opened this issue Feb 6, 2018 · 4 comments
Closed

Boundary checks #18

mschauer opened this issue Feb 6, 2018 · 4 comments

Comments

@mschauer
Copy link

mschauer commented Feb 6, 2018

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
@Codyk12
Copy link
Contributor

Codyk12 commented May 30, 2019

Is there a particular reason this hasn't been added to the library yet (Besides the fact that it might need to be generalized maybe a little more)?

@timholy
Copy link
Member

timholy commented Jun 1, 2019

Thanks a ton @mschauer for documenting this. Someone needs to polish this up and make a PR. Care to try, @Codyk12?

@Codyk12
Copy link
Contributor

Codyk12 commented Jun 3, 2019

@timholy
Sure,
Let me know what you think
#26

@donm
Copy link

donm commented Sep 1, 2019

Can this be closed now with the merge of #26, or are there still issues?

@mschauer mschauer closed this as completed Sep 1, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants