Skip to content

Commit

Permalink
Merge pull request #321 from Mattriks/ngon_forms
Browse files Browse the repository at this point in the history
ngons, stars and xgons
  • Loading branch information
tlnagy authored Oct 21, 2018
2 parents 8f69d54 + e0eef7b commit 9e8f3ef
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 1 deletion.
16 changes: 16 additions & 0 deletions docs/src/gallery/forms.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,22 @@ img = compose(context(),
)
```

## [`ngon`](@ref), [`star`](@ref), [`xgon`](@ref)
```@example
using Compose
set_default_graphic_size(14cm, 5cm)
rainbow = ["orange","green","indigo",
"darkviolet","indigo","blue","green","yellow","orange","red"]
properties = [fillopacity(0.5), fill(rainbow), stroke("black")]
npoints = [7,5,3,2,3,4,5,6,7,8]
X = range(0.06, stop=0.94, length=10)
radii = 0.035*[-ones(3); ones(7)]
p = compose(context(),
(context(), ngon(X, [0.16], radii, npoints),
star(X, [0.5], radii, npoints),
xgon(X, [0.84], radii, npoints), properties...))
```

## [`polygon`](@ref)
```@example
using Statistics, Compose
Expand Down
8 changes: 8 additions & 0 deletions examples/polygon_forms.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

using Compose

compose(context(),
ngon(0.15, 0.15, 0.08, 5),
star(0.35, 0.15, 0.08, 5, 0.3),
xgon(0.55, 0.15, 0.08, 5, 0.3)
) |> SVG("polygon_forms.svg", 3inch, 3inch)
3 changes: 2 additions & 1 deletion src/Compose.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ import Measures: resolve, w, h

export compose, compose!, Context, UnitBox, AbsoluteBoundingBox, Rotation, Mirror,
ParentDrawContext, context, ctxpromise, table, set_units!, minwidth, minheight,
text_extents, max_text_extents, polygon, line, rectangle, circle, arc, sector,
text_extents, max_text_extents, polygon, ngon, star, xgon,
line, rectangle, circle, arc, sector,
ellipse, text, curve, bitmap, stroke, fill, strokedash, strokelinecap,
strokelinejoin, linewidth, visible, fillopacity, strokeopacity, clip,
font, fontsize, svgid, svgclass, svgattribute, jsinclude, jscall, Measure,
Expand Down
98 changes: 98 additions & 0 deletions src/form.jl
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,104 @@ form_string(::Arc) = "A"



### Polygon primitive forms

"""
ngon(x, y, r, n::Int)
Define a `n`-sided polygon with its center at (`x`,`y`), and radius of `r`. For an upside-down ngon, use `-r`.
"""
function ngon(x, y, r, n::Int, tag=empty_tag)
θ = range(-π/2, stop=1.5π, length=n+1)
x1 = x_measure(x) .+ x_measure(r).*cos.(θ)
y1 = y_measure(y) .+ x_measure(r).*sin.(θ)
points = collect(Tuple{Measure, Measure}, zip(x1, y1))
return Form([PolygonPrimitive(points)], tag)
end


"""
ngon(xs::AbstractVector, ys::AbstractVector, rs::AbstractVector, ns::AbstractVector{Int})
Arguments can be passed in arrays in order to perform multiple drawing operations at once.
"""
function ngon(xs::AbstractVector, ys::AbstractVector, rs::AbstractVector, ns::AbstractVector{Int}, tag=empty_tag)
VecType = Tuple{Measure, Measure}
PrimType = PolygonPrimitive{VecType}
polyprims = PrimType[]
for (x, y, r, n) in Compose.cyclezip(xs, ys, rs, ns)
p = ngon( x, y, r, n)
push!(polyprims, PrimType(p.primitives[1].points))
end
return Form{PrimType}(polyprims, tag)
end


"""
star(x, y, r, n::Int, ratio)
Define a `n`-pointed star with its center at (`x`,`y`), outer radius of `r`, and inner radius equal to `r*ratio`. For an upside-down star, use `-r`.
"""
function star(x, y, r, n::Int, ratio::Float64=0.3, tag=empty_tag)
θ = range(-π/2, stop=1.5π, length=2*n+1)[1:end-1]
r1 = repeat([r, r*ratio], outer=n)
x1 = x_measure(x) .+ x_measure(r1).*cos.(θ)
y1 = y_measure(y) .+ x_measure(r1).*sin.(θ)
points = collect(Tuple{Measure, Measure}, zip(x1, y1))
return Form([PolygonPrimitive(points)], tag)
end


"""
star(xs::AbstractVector, ys::AbstractVector, rs::AbstractVector, ns::AbstractVector{Int}, ratios::AbstractVector{Float64})
Arguments can be passed in arrays in order to perform multiple drawing operations at once.
"""
function star(xs::AbstractVector, ys::AbstractVector, rs::AbstractVector, ns::AbstractVector{Int}, ratios::AbstractVector{Float64}=[0.3], tag=empty_tag)
VecType = Tuple{Measure, Measure}
PrimType = PolygonPrimitive{VecType}
polyprims = PrimType[]
for (x, y, r, n, ratio) in Compose.cyclezip(xs, ys, rs, ns, ratios)
p = star( x, y, r, n, ratio)
push!(polyprims, PrimType(p.primitives[1].points))
end
return Form{PrimType}(polyprims, tag)
end


"""
xgon(x, y, r, n::Int, ratio)
Define a cross with `n` arms with its center at (`x`,`y`), outer radius of `r`, and inner radius equal to `r*ratio`. For an upside-down xgon, use `-r`.
"""
function xgon(x, y, r, n::Int, ratio::Float64=0.1, tag=empty_tag)
θ₁ = range(-0.75π, stop=1.25π, length=n+1)[1:end-1]
w = 2*r*ratio*sin/n)
dₒ = abs(asin(0.5*w/r))
dᵢ = abs(asin(0.5*w/(r*ratio)))
r₂ = repeat([r*ratio,r,r], outer=n)
θ₂ = vec([θ+x for x in [-dᵢ, -dₒ, dₒ], θ in θ₁])
x1 = x_measure(x) .+ x_measure(r₂).*cos.(θ₂)
y1 = y_measure(y) .+ x_measure(r₂).*sin.(θ₂)
points = collect(Tuple{Measure, Measure}, zip(x1, y1))
return Form([PolygonPrimitive(points)], tag)
end

"""
xgon(xs::AbstractVector, ys::AbstractVector, rs::AbstractVector, ns::AbstractVector{Int}, ratios::AbstractVector{Float64})
Arguments can be passed in arrays in order to perform multiple drawing operations at once.
"""
function xgon(xs::AbstractVector, ys::AbstractVector, rs::AbstractVector, ns::AbstractVector{Int}, ratios::AbstractVector{Float64}=[0.3], tag=empty_tag)
VecType = Tuple{Measure, Measure}
PrimType = PolygonPrimitive{VecType}
polyprims = PrimType[]
for (x, y, r, n, ratio) in Compose.cyclezip(xs, ys, rs, ns, ratios)
p = xgon(x, y, r, n, ratio)
push!(polyprims, PrimType(p.primitives[1].points))
end
return Form{PrimType}(polyprims, tag)
end



Expand Down
20 changes: 20 additions & 0 deletions test/data/polygon_forms.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 9e8f3ef

Please sign in to comment.