Skip to content

Commit

Permalink
iterators
Browse files Browse the repository at this point in the history
  • Loading branch information
cormullion committed Jun 27, 2018
1 parent 7d1da79 commit b285243
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 44 deletions.
14 changes: 11 additions & 3 deletions src/BoundingBox.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
22 changes: 13 additions & 9 deletions src/Table.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
8 changes: 5 additions & 3 deletions src/basics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 11 additions & 3 deletions src/bezierpath.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)")
Expand Down
52 changes: 28 additions & 24 deletions src/tiles-grids.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions test/axes-test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b285243

Please sign in to comment.