diff --git a/src/BoundingBox.jl b/src/BoundingBox.jl index 34d634b7..84a09dc6 100644 --- a/src/BoundingBox.jl +++ b/src/BoundingBox.jl @@ -65,9 +65,17 @@ function BoundingBox(str::String) return BoundingBox(lcorner, ocorner) end -Base.start(bbox::BoundingBox) = 1 -Base.next(bbox::BoundingBox, state) = state == 1 ? (bbox.corner1, state + 1) : state == 2 ? (bbox.corner2, state + 1) : error("this should never happen") -Base.done(bbox::BoundingBox, state) = state > 2 +#state is either 1 or 2! +function Base.iterate(bbox::BoundingBox) + return (bbox.corner1, 2) +end + +function Base.iterate(bbox::BoundingBox, state) + if state > 2 + return + end + state == 1 ? (bbox.corner1, state + 1) : state == 2 ? (bbox.corner2, state + 1) : error("this should never happen") +end Base.last(bbox::BoundingBox) = bbox.corner2 diff --git a/src/Table.jl b/src/Table.jl index a60a5b7c..8c5d0ded 100644 --- a/src/Table.jl +++ b/src/Table.jl @@ -176,15 +176,23 @@ Table(rowheights::AbstractRange{T1}, colwidths::Array{T2, 1}, center=O) where T1 # interfaces -# basic iteration -function Base.start(t::Table) - # return the initial state +function Base.iterate(t::Table) x = t.leftmargin + (t.colwidths[1]/2) y = t.topmargin + (t.rowheights[1]/2) - return ( Point(x, y), 1) + cellnumber = 2 + t.currentrow = div(cellnumber - 1, t.ncols) + 1 + t.currentcol = mod1(cellnumber, t.ncols) + x1 = t.leftmargin + sum(t.colwidths[1:t.currentcol - 1]) + t.colwidths[t.currentcol]/2 + y1 = t.topmargin + sum(t.rowheights[1:t.currentrow - 1]) + t.rowheights[t.currentrow]/2 + nextpoint = Point(x1, y1) + return ((Point(x, y), 1), (nextpoint, 2)) end -function Base.next(t::Table, state) +# v0.7 iteration +function Base.iterate(t::Table, state) + if state[2] > t.nrows * t.ncols + return + end # state[1] is the Point x = state[1].x y = state[1].y @@ -199,10 +207,6 @@ function Base.next(t::Table, state) return ((nextpoint, cellnumber), (nextpoint, cellnumber + 1)) end -function Base.done(t::Table, state) - state[2] > t.nrows * t.ncols -end - function Base.size(t::Table) return (t.nrows, t.ncols) end diff --git a/src/basics.jl b/src/basics.jl index ff24750e..47436dc2 100644 --- a/src/basics.jl +++ b/src/basics.jl @@ -531,11 +531,13 @@ function rulers() [text(string(x), Point(x, w/3), halign=:right) for x in 10:10:n] end fontsize(15) - text("x", O + (n + w, -w/2), halign=:right, valign=:middle) - text("y", O + (-w/2, n), halign=:right, valign=:middle, angle=pi/2) + text("X", O + (n-w/2, w), halign=:right, valign=:middle) + text("Y", O + (-3w/2, n-w), halign=:right, valign=:middle, angle=pi/2) + sethue("white") + text("X", O + (w, -w/2), halign=:right, valign=:middle) + text("Y", O + (-w/3, w/3), halign=:right, valign=:middle, angle=pi/2) #center circle(O, 2, :strokepreserve) - sethue("white") setopacity(0.5) fillpath() end diff --git a/src/bezierpath.jl b/src/bezierpath.jl index c9731393..957a87ed 100644 --- a/src/bezierpath.jl +++ b/src/bezierpath.jl @@ -12,9 +12,17 @@ Base.getindex(bps::BezierPathSegment, i::Int64) = [bps.p1, bps.cp1, bps.cp2, bps Base.setindex!(bp::BezierPathSegment, v, i::Int64) = [bps.p1, bps.cp1, bps.cp2, bps.p2][i] = v Base.IndexStyle(::Type{<:BezierPathSegment}) = IndexLinear() -Base.start(bps::BezierPathSegment) = 1 -Base.next(bps::BezierPathSegment, s::Int) = bps[s], s+1 -Base.done(bps::BezierPathSegment, s::Int) = (s > length(bps)) +# state is integer referring to one of the four points +function Base.iterate(bps::BezierPathSegment) + return (bps.p1, 2) +end + +function Base.iterate(bps::BezierPathSegment, s) + if (s > length(bps)) + return + end + return bps[s], s+1 +end function Base.show(io::IO, bps::BezierPathSegment) println(io, "p1 $(bps.p1) cp1 $(bps.cp1)") diff --git a/src/tiles-grids.jl b/src/tiles-grids.jl index 55ccd80d..5365b036 100644 --- a/src/tiles-grids.jl +++ b/src/tiles-grids.jl @@ -62,19 +62,26 @@ mutable struct Tiler end end -function Base.start(pt::Tiler) - # return the initial state +function Base.iterate(pt::Tiler) x = -(pt.areawidth/2) + pt.margin + (pt.tilewidth/2) y = -(pt.areaheight/2) + pt.margin + (pt.tileheight/2) - return (Point(x, y), 1) + tilenumber = 1 + x1 = x + pt.tilewidth + y1 = y + if x1 > (pt.areawidth/2) - pt.margin + y1 += pt.tileheight + x1 = -(pt.areawidth/2) + pt.margin + (pt.tilewidth/2) + end + pt.currentrow, pt.currentcol = (div(tilenumber-1, pt.ncols)+1, mod1(tilenumber, pt.ncols)) + return ((Point(x, y), tilenumber), (Point(x1, y1), tilenumber + 1)) end -function Base.next(pt::Tiler, state) - # Returns the item and the next state - # state[1] is the Point +function Base.iterate(pt::Tiler, state) + if state[2] > (pt.nrows * pt.ncols) + return + end x = state[1].x y = state[1].y - # state[2] is the tilenumber tilenumber = state[2] x1 = x + pt.tilewidth y1 = y @@ -86,11 +93,6 @@ function Base.next(pt::Tiler, state) return ((Point(x, y), tilenumber), (Point(x1, y1), tilenumber + 1)) end -function Base.done(pt::Tiler, state) - # Tests if there are any items remaining - state[2] > (pt.nrows * pt.ncols) -end - function Base.length(pt::Tiler) pt.nrows * pt.ncols end @@ -289,19 +291,26 @@ mutable struct Partition end end -function Base.start(pt::Partition) - # return the initial state +function Base.iterate(pt::Partition) x = -(pt.areawidth/2) + (pt.tilewidth/2) y = -(pt.areaheight/2) + (pt.tileheight/2) - return (Point(x, y), 1) + tilenumber = 1 + x1 = x + pt.tilewidth + y1 = y + if (x1 + pt.tilewidth/2) > (pt.areawidth/2) + y1 += pt.tileheight + x1 = -(pt.areawidth/2) + (pt.tilewidth/2) + end + pt.currentrow, pt.currentcol = (div(tilenumber-1, pt.ncols)+1, mod1(tilenumber, pt.ncols)) + return ((Point(x, y), tilenumber), (Point(x1, y1), tilenumber + 1)) end -function Base.next(pt::Partition, state) - # Returns the item and the next state - # state[1] is the Point +function Base.iterate(pt::Partition, state) + if state[2] > (pt.nrows * pt.ncols) + return + end x = state[1].x y = state[1].y - # state[2] is the tilenumber tilenumber = state[2] x1 = x + pt.tilewidth y1 = y @@ -313,11 +322,6 @@ function Base.next(pt::Partition, state) return ((Point(x, y), tilenumber), (Point(x1, y1), tilenumber + 1)) end -function Base.done(pt::Partition, state) - # Tests if there are any items remaining - state[2] > (pt.nrows * pt.ncols) -end - function Base.length(pt::Partition) pt.nrows * pt.ncols end diff --git a/test/axes-test.jl b/test/axes-test.jl index d93d25f4..442d1995 100644 --- a/test/axes-test.jl +++ b/test/axes-test.jl @@ -17,9 +17,9 @@ end fname = "rulers-test.pdf" width, height = 2000, 2000 Drawing(width, height, fname) -origin(1000, 1000) +origin(1200, 1200) background("ivory") -pagetiles = Tiler(width, height, 5, 5, margin=50) +pagetiles = Tiler(width, height, 5, 5, margin=10) for (pos, n) in pagetiles test_rulers(pos.x, pos.y, rand() * 2pi, pagetiles.tilewidth) end