Skip to content

Commit

Permalink
Draw on pixels that can be drawn (using checkbounds) (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
Codyk12 authored and timholy committed Jun 4, 2019
1 parent 1eeffda commit 51602dc
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 35 deletions.
22 changes: 19 additions & 3 deletions src/core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,24 @@ Point(τ::Tuple{Int, Int}) = Point(τ...)
Point(p::CartesianIndex) = Point(p[2], p[1])

function draw!(img::AbstractArray{T,2}, point::Point, color::T) where T<:Colorant
if checkbounds(Bool, img, point.y, point.x)
img[point.y, point.x] = color
end
drawifinbounds!(img, point, color)
end

"""
img_new = drawifinbounds!(img, y, x, color)
img_new = drawifinbounds!(img, Point, color)
img_new = drawifinbounds!(img, CartesianIndex, color)
Draws a single point after checkbounds() for coordinate in the image.
Color Defaults to oneunit(T)
"""

drawifinbounds!(img::AbstractArray{T,2}, p::Point, color::T = oneunit(T)) where {T<:Colorant} = drawifinbounds!(img, p.y, p.x, color)
drawifinbounds!(img::AbstractArray{T,2}, p::CartesianIndex{2}, color::T = oneunit(T)) where {T<:Colorant} = drawifinbounds!(img, Point(p), color)

function drawifinbounds!(img::AbstractArray{T,2}, y::Int, x::Int, color::T) where {T<:Colorant}
if checkbounds(Bool, img, y, x) img[y, x] = color end
img
end
4 changes: 2 additions & 2 deletions src/cross.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ Cross(c, arm::Int) = Cross(c, -arm:arm)

function draw!(img::AbstractArray{T, 2}, cross::Cross, color::T) where T<:Colorant
for Δx in cross.range
img[cross.c.y, cross.c.x + Δx] = color
drawifinbounds!(img, cross.c.y, cross.c.x + Δx, color)
end
for Δy in cross.range
img[cross.c.y + Δy, cross.c.x] = color
drawifinbounds!(img, cross.c.y + Δy, cross.c.x, color)
end
img
end
8 changes: 4 additions & 4 deletions src/ellipse2d.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ function draw!(img::AbstractArray{T, 2}, ellipse::Ellipse, color::T) where T<:Co
end
end
for (yi, xi) in zip(ys, xs)
img[yi, xi] = color
img[2 * ellipse.center.y - yi, xi] = color
img[yi, 2 * ellipse.center.x - xi] = color
img[2 * ellipse.center.y - yi, 2 * ellipse.center.x - xi] = color
drawifinbounds!(img, yi, xi, color)
drawifinbounds!(img,2 * ellipse.center.y - yi, xi, color)
drawifinbounds!(img,yi, 2 * ellipse.center.x - xi, color)
drawifinbounds!(img, 2 * ellipse.center.y - yi, 2 * ellipse.center.x - xi, color)
end
img
end
14 changes: 7 additions & 7 deletions src/line2d.jl
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ function bresenham(img::AbstractArray{T, 2}, y0::Int, x0::Int, y1::Int, x1::Int,
err = (dx > dy ? dx : -dy) / 2

while true
img[y0, x0] = color
drawifinbounds!(img, y0, x0, color)
(x0 != x1 || y0 != y1) || break
e2 = err
if e2 > -dx
Expand Down Expand Up @@ -125,9 +125,9 @@ function xiaolin_wu(img::AbstractArray{T, 2}, y0::Int, x0::Int, y1::Int, x1::Int
xpxl0 = xend
ypxl0 = trunc(Int, yend)
index = swapped ? CartesianIndex(xpxl0, ypxl0) : CartesianIndex(ypxl0, xpxl0)
if checkbounds(Bool, img, index) img[index] = T(rfpart(yend) * xgap) end
drawifinbounds!(img, index, T(rfpart(yend) * xgap))
index = swapped ? CartesianIndex(xpxl0, ypxl0 + 1) : CartesianIndex(ypxl0 + 1, xpxl0)
if checkbounds(Bool, img, index) img[index] = T(fpart(yend) * xgap) end
drawifinbounds!(img, index, T(fpart(yend) * xgap))
intery = yend + gradient

xend = round(Int, x1)
Expand All @@ -136,15 +136,15 @@ function xiaolin_wu(img::AbstractArray{T, 2}, y0::Int, x0::Int, y1::Int, x1::Int
xpxl1 = xend
ypxl1 = trunc(Int, yend)
index = swapped ? CartesianIndex(xpxl1, ypxl1) : CartesianIndex(ypxl1, xpxl1)
if checkbounds(Bool, img, index) img[index] = T(rfpart(yend) * xgap) end
drawifinbounds!(img, index, T(rfpart(yend) * xgap))
index = swapped ? CartesianIndex(xpxl1, ypxl1 + 1) : CartesianIndex(ypxl1 + 1, xpxl1)
if checkbounds(Bool, img, index) img[index] = T(fpart(yend) * xgap) end
drawifinbounds!(img, index, T(fpart(yend) * xgap))

for i in (xpxl0 + 1):(xpxl1 - 1)
index = swapped ? CartesianIndex(i, trunc(Int, intery)) : CartesianIndex(trunc(Int, intery), i)
if checkbounds(Bool, img, index) img[index] = T(rfpart(intery)) end
drawifinbounds!(img, index, T(rfpart(intery)))
index = swapped ? CartesianIndex(i, trunc(Int, intery) + 1) : CartesianIndex(trunc(Int, intery) + 1, i)
if checkbounds(Bool, img, index) img[index] = T(fpart(intery)) end
drawifinbounds!(img, index, T(fpart(intery)))
intery += gradient
end
img
Expand Down
16 changes: 2 additions & 14 deletions src/paths.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,10 @@ Path(v::AbstractVector{CartesianIndex{2}}) = Path([Point(p) for p in v])

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, axes(img)))
l = CartesianIndex(map(r->last(r), axes(img)))

if min(f,vertices[1])!=f || max(l,vertices[1])!=l
println(vertices[1])
error("Point coordinates out of range.")
end

for i in 1:length(vertices)-1
if min(f,vertices[i+1])==f && max(l,vertices[i+1])==l
draw!(img, LineSegment(vertices[i], vertices[i+1]), color)
else
println(vertices[i+1])
error("Point coordinates out of range.")
end
draw!(img, LineSegment(vertices[i], vertices[i+1]), color)
end
img
end

#Polygon methods
Expand Down
12 changes: 7 additions & 5 deletions test/paths.jl
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,14 @@ end
@test all(x->x==RGB{N0f8}(0,0,0), img[2,1:3])==true
@test img[2,4]==RGB{N0f8}(1,1,1)

img = zeros(Gray{N0f8},5,5)
invalid_points = [(6,1), (4,4), (1,7), (7,6)]
@test_throws ErrorException draw!(img, Path(invalid_points))
# No longer invalid, the program will draw what it can on the image

invalid_points = [(4,4), (1,7), (7,6)]
@test_throws ErrorException draw!(img, Path(invalid_points))
# img = zeros(Gray{N0f8},5,5)
# invalid_points = [(6,1), (4,4), (1,7), (7,6)]
# @test_throws ErrorException draw!(img, Path(invalid_points))
#
# invalid_points = [(4,4), (1,7), (7,6)]
# @test_throws ErrorException draw!(img, Path(invalid_points))
end

@testset "RegularPolygon" begin
Expand Down

2 comments on commit 51602dc

@timholy
Copy link
Member

@timholy timholy commented on 51602dc Jun 4, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator register()

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/1185

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if Julia TagBot is installed, or can be done manually through the github interface, or via:

git tag -a v0.2.2 -m "<description of version>" 51602dce8e43d3446b61d81faa19b20fa9cba21a
git push origin v0.2.2

Please sign in to comment.