shapes change verts to x/y; translate/scale/rotate/center; x/y scale alias fix

This commit is contained in:
Thomas Breloff 2016-05-02 16:37:10 -04:00
parent 2e2e8a2230
commit e676387c16
6 changed files with 111 additions and 43 deletions

View File

@ -139,14 +139,21 @@ export
@animate, @animate,
@gif, @gif,
# recipes
PlotRecipe, PlotRecipe,
# EllipseRecipe,
spy, spy,
arcdiagram, arcdiagram,
chorddiagram chorddiagram,
# corrplot
translate,
translate!,
rotate,
rotate!,
center,
P2,
P3,
BezierCurve,
curve_points,
directed_curve
# --------------------------------------------------------- # ---------------------------------------------------------

View File

@ -624,6 +624,11 @@ function preprocessArgs!(d::KW)
if haskey(d, tsym) && ticksType(d[tsym]) == :labels if haskey(d, tsym) && ticksType(d[tsym]) == :labels
d[tsym] = (1:length(d[tsym]), d[tsym]) d[tsym] = (1:length(d[tsym]), d[tsym])
end end
ssym = symbol(letter * "scale")
if haskey(d, ssym) && haskey(_scaleAliases, d[ssym])
d[ssym] = _scaleAliases[d[ssym]]
end
end end
# handle line args # handle line args
@ -801,12 +806,12 @@ function getPlotArgs(pkg::AbstractBackend, kw, idx::Int; set_defaults = true)
setDictValue(kwdict, d, k, idx, _plotDefaults) setDictValue(kwdict, d, k, idx, _plotDefaults)
end end
end end
#
for k in (:xscale, :yscale) # for k in (:xscale, :yscale)
if haskey(_scaleAliases, d[k]) # if haskey(_scaleAliases, d[k])
d[k] = _scaleAliases[d[k]] # d[k] = _scaleAliases[d[k]]
end # end
end # end
# handle legend/colorbar # handle legend/colorbar
d[:legend] = convertLegendValue(d[:legend]) d[:legend] = convertLegendValue(d[:legend])

View File

@ -67,11 +67,11 @@ function Gadfly.render(geom::ShapeGeometry, theme::Gadfly.Theme, aes::Gadfly.Aes
end end
function gadflyshape(sv::Shape) function gadflyshape(sv::Shape)
ShapeGeometry(Any[sv.vertices]) ShapeGeometry(Any[vertices(sv)])
end end
function gadflyshape(sv::AVec{Shape}) function gadflyshape(sv::AVec{Shape})
ShapeGeometry(Any[s.vertices for s in sv]) ShapeGeometry(Any[vertices(s) for s in sv])
end end

View File

@ -88,7 +88,7 @@ function gr_setmarkershape(p)
if haskey(p, :markershape) if haskey(p, :markershape)
shape = p[:markershape] shape = p[:markershape]
if isa(shape, Shape) if isa(shape, Shape)
p[:vertices] = shape.vertices p[:vertices] = vertices(shape)
else else
GR.setmarkertype(gr_markertype[shape]) GR.setmarkertype(gr_markertype[shape])
p[:vertices] = :none p[:vertices] = :none

View File

@ -124,11 +124,12 @@ function getPyPlotLineStyle(linetype::Symbol, linestyle::Symbol)
end end
function getPyPlotMarker(marker::Shape) function getPyPlotMarker(marker::Shape)
n = length(marker.vertices) x, y = shape_coords(marker)
n = length(x)
mat = zeros(n+1,2) mat = zeros(n+1,2)
for (i,vert) in enumerate(marker.vertices) for i=1:n
mat[i,1] = vert[1] mat[i,1] = x[i]
mat[i,2] = vert[2] mat[i,2] = y[i]
end end
mat[n+1,:] = mat[1,:] mat[n+1,:] = mat[1,:]
pypath.pymember("Path")(mat) pypath.pymember("Path")(mat)
@ -142,8 +143,8 @@ const _path_CLOSEPOLY = UInt8(79)
# and http://matplotlib.org/api/path_api.html#matplotlib.path.Path # and http://matplotlib.org/api/path_api.html#matplotlib.path.Path
function buildPyPlotPath(x, y) function buildPyPlotPath(x, y)
n = length(x) n = length(x)
mat = zeros(n, 2) mat = zeros(n+1, 2)
codes = zeros(UInt8, n) codes = zeros(UInt8, n+1)
lastnan = true lastnan = true
for i=1:n for i=1:n
mat[i,1] = x[i] mat[i,1] = x[i]
@ -156,6 +157,7 @@ function buildPyPlotPath(x, y)
end end
lastnan = nan lastnan = nan
end end
codes[n+1] = _path_CLOSEPOLY
pypath.pymember("Path")(mat, codes) pypath.pymember("Path")(mat, codes)
end end

View File

@ -1,10 +1,4 @@
export
P2,
P3,
BezierCurve,
curve_points,
directed_curve
typealias P2 FixedSizeArrays.Vec{2,Float64} typealias P2 FixedSizeArrays.Vec{2,Float64}
typealias P3 FixedSizeArrays.Vec{3,Float64} typealias P3 FixedSizeArrays.Vec{3,Float64}
@ -18,37 +12,36 @@ compute_angle(v::P2) = (angle = atan2(v[2], v[1]); angle < 0 ? 2π - angle : ang
# ------------------------------------------------------------- # -------------------------------------------------------------
immutable Shape immutable Shape
vertices::AVec # vertices::AVec
x::AVec
y::AVec
end end
Shape(x, y) = Shape(collect(zip(x, y))) # Shape(x, y) = Shape(collect(zip(x, y)))
Shape(verts::AVec) = Shape(unzip(verts)...)
get_xs(shape::Shape) = Float64[v[1] for v in shape.vertices] # get_xs(shape::Shape) = Float64[v[1] for v in shape.vertices]
get_ys(shape::Shape) = Float64[v[2] for v in shape.vertices] # get_ys(shape::Shape) = Float64[v[2] for v in shape.vertices]
get_xs(shape::Shape) = shape.x
get_ys(shape::Shape) = shape.y
vertices(shape::Shape) = collect(zip(shape.x, shape.y))
function scale(shape::Shape, x, y=x)
sx, sy = shape_coords(shape)
Shape(sx .* x, sy .* y)
end
function translate(shape::Shape, x, y=x)
sx, sy = shape_coords(shape)
Shape(sx .+ x, sy .+ y)
end
function shape_coords(shape::Shape) function shape_coords(shape::Shape)
unzip(shape.vertices) # unzip(shape.vertices)
shape.x, shape.y
end end
function shape_coords(shapes::AVec{Shape}) function shape_coords(shapes::AVec{Shape})
length(shapes) == 0 && return zeros(0), zeros(0) length(shapes) == 0 && return zeros(0), zeros(0)
xs = map(get_xs, shapes) xs = map(get_xs, shapes)
ys = map(get_ys, shapes) ys = map(get_ys, shapes)
x, y = unzip(shapes[1].vertices) # x, y = shapes[1].x, shapes[1].y #unzip(shapes[1].vertices)
x, y = map(copy, shape_coords(shapes[1]))
for shape in shapes[2:end] for shape in shapes[2:end]
tmpx, tmpy = unzip(shape.vertices) # tmpx, tmpy = unzip(shape.vertices)
nanappend!(x, tmpx) nanappend!(x, shape.x)
nanappend!(y, tmpy) nanappend!(y, shape.y)
# x = vcat(x, NaN, tmpx) # x = vcat(x, NaN, tmpx)
# y = vcat(y, NaN, tmpy) # y = vcat(y, NaN, tmpy)
end end
@ -135,6 +128,67 @@ end
# ----------------------------------------------------------------------- # -----------------------------------------------------------------------
center(shape::Shape) = (mean(shape.x), mean(shape.y))
function Base.scale!(shape::Shape, x::Real, y::Real = x, c = center(shape))
sx, sy = shape_coords(shape)
cx, cy = c
for i=1:length(sx)
sx[i] = (sx[i] - cx) * x + cx
sy[i] = (sy[i] - cy) * y + cy
end
shape
end
function Base.scale(shape::Shape, x::Real, y::Real = x, c = center(shape))
shapecopy = deepcopy(shape)
scale!(shape, x, y, c)
end
function translate!(shape::Shape, x::Real, y::Real = x)
sx, sy = shape_coords(shape)
for i=1:length(sx)
sx[i] += x
sy[i] += y
end
shape
end
function translate(shape::Shape, x::Real, y::Real = x)
shapecopy = deepcopy(shape)
translate!(shape, x, y)
end
function rotate_x(x::Real, y::Real, Θ::Real, centerx::Real, centery::Real)
(x - centerx) * cos(Θ) - (y - centery) * sin(Θ) + centerx
end
function rotate_y(x::Real, y::Real, Θ::Real, centerx::Real, centery::Real)
(y - centery) * cos(Θ) + (x - centerx) * sin(Θ) + centery
end
function rotate(x::Real, y::Real, θ::Real, c = center(shape))
cx, cy = c
rotate_x(x, y, Θ, cx, cy), rotate_y(x, y, Θ, cx, cy)
end
function rotate!(shape::Shape, Θ::Real, c = center(shape))
x, y = shape_coords(shape)
cx, cy = c
for i=1:length(x)
x[i] = rotate_x(x[i], y[i], Θ, cx, cy)
y[i] = rotate_y(x[i], y[i], Θ, cx, cy)
end
shape
end
function rotate(shape::Shape, Θ::Real, c = center(shape))
shapecopy = deepcopy(shape)
rotate!(shapecopy, Θ, c)
end
# -----------------------------------------------------------------------
immutable Font immutable Font
family::AbstractString family::AbstractString