Skip to content

Commit

Permalink
Merge pull request #285 from GiggleLiu/jg/closed-smoothpath
Browse files Browse the repository at this point in the history
Add keyword argument `close` to function polysmooth
  • Loading branch information
cormullion authored Oct 30, 2023
2 parents 0acb5ff + a2f55ce commit 316d061
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
19 changes: 12 additions & 7 deletions src/polygons.jl
Original file line number Diff line number Diff line change
Expand Up @@ -359,8 +359,8 @@ function drawroundedcorner(cornerpoint::Point, p1::Point, p2::Point, radius, pat
end

"""
polysmooth(points, radius, action=:action; debug=false)
polysmooth(points, radius; action=:none, debug=false)
polysmooth(points, radius, action=:action; debug=false, close=true)
polysmooth(points, radius; action=:none, debug=false, close=true)
Make a closed path from the `points` and round the corners by making them arcs with the
given radius. Execute the action when finished.
Expand All @@ -373,14 +373,14 @@ The `debug` option also draws the construction circles at each corner.
TODO Return something more useful than a Boolean.
"""
function polysmooth(points::Array{Point,1}, radius, action::Symbol; debug = false)
function polysmooth(points::Array{Point,1}, radius, action::Symbol; debug = false, close = true)
temppath = Tuple[]
l = length(points)
if l < 3
# there are less than three points to smooth
return nothing
else
@inbounds for i in 1:l
@inbounds for i in 1:(close ? l : l-2)
p1 = points[mod1(i, l)]
p2 = points[mod1(i + 1, l)]
p3 = points[mod1(i + 2, l)]
Expand All @@ -391,7 +391,12 @@ function polysmooth(points::Array{Point,1}, radius, action::Symbol; debug = fals
end
end
# need to close by joining to first point
push!(temppath, temppath[1])
if close
push!(temppath, temppath[1])
else
pushfirst!(temppath, (:line, points[1]))
push!(temppath, (:line, points[end]))
end
# draw the path
for (c, p) in temppath
if c == :line
Expand All @@ -405,8 +410,8 @@ function polysmooth(points::Array{Point,1}, radius, action::Symbol; debug = fals
do_action(action)
end

polysmooth(points::Array{Point,1}, radius; action = :none, debug = false) =
polysmooth(points, radius, action; debug = debug)
polysmooth(points::Array{Point,1}, radius; action = :none, debug = false, close = true) =
polysmooth(points, radius, action; debug = debug, close = close)

"""
offsetpoly(plist::Array{Point, 1}, d) where T<:Number
Expand Down
1 change: 1 addition & 0 deletions test/polysmooth-tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ function star_test(fname)
p = star(pos, tiles.tilewidth/2 - 10, randomsides, randomratio, 0, vertices=true)
prettypoly(p, close=true, :stroke)
polysmooth(p, randomsmoothing, :fill, debug=true)
polysmooth(p, randomsmoothing, :fill, debug=true, close=false)
end
@test finish() == true
println("...finished test: output in $(fname)")
Expand Down

0 comments on commit 316d061

Please sign in to comment.