Skip to content

Commit

Permalink
add vertices options to more box() methods
Browse files Browse the repository at this point in the history
  • Loading branch information
cormullion committed Jan 16, 2019
1 parent 6d71990 commit 463eb17
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 23 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

### Added

-
- a few more box functions take vertices=true/false

### Changed

Expand Down
27 changes: 21 additions & 6 deletions src/BoundingBox.jl
Original file line number Diff line number Diff line change
Expand Up @@ -199,12 +199,26 @@ function Base.convert(::Type{Vector{Point}}, bbox::BoundingBox)
end

"""
box(bbox::BoundingBox, :action)
box(bbox::BoundingBox, :action;
vertices=false)
Make a box using the bounds in `bbox`.
"""
box(bbox::BoundingBox, action::Symbol=:nothing; kwargs...) =
box(bbox.corner1, bbox.corner2, action; kwargs...)
Use `vertices=true` to return an array of the four corner points: bottom left,
top left, top right, bottom right.
"""
function box(bbox::BoundingBox, action::Symbol=:nothing;
vertices=false)
if vertices
botleft = Point(bbox.corner1.x, bbox.corner2.y)
topleft = bbox.corner1
topright = Point(bbox.corner2.x, bbox.corner1.y)
botright = bbox.corner2
return [botleft, topleft, topright, botright]
else
box(bbox.corner1, bbox.corner2, action)
end
end

"""
poly(bbox::BoundingBox, :action; kwargs...)
Expand All @@ -217,10 +231,11 @@ poly(bbox::BoundingBox, action::Symbol=:nothing; kwargs...) =
"""
prettypoly(bbox::BoundingBox, :action; kwargs...)
Make a decorated polygon around the BoundingBox in `bbox`.
Make a decorated polygon around the BoundingBox in `bbox`. The vertices are in
the order: bottom left, top left, top right, and bottom right.
"""
prettypoly(bbox::BoundingBox, action::Symbol=:nothing; kwargs...) =
prettypoly(convert(Vector{Point}, bbox), action; kwargs...)
prettypoly(box(bbox, vertices=true), action; kwargs...)

"""
boundingboxesintersect(bbox1::BoundingBox, bbox2::BoundingBox)
Expand Down
54 changes: 41 additions & 13 deletions src/shapes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,40 @@ function rect(xmin, ymin, w, h, action::Symbol=:nothing)
end

"""
rect(cornerpoint, w, h, action)
rect(cornerpoint, w, h, action;
vertices=false)
Create a rectangle with one corner at `cornerpoint` with width `w` and height `h` and do an
action.
Create a rectangle with one corner at `cornerpoint` with width `w` and height
`h` and do an action.
Use `vertices=true` to return an array of the four corner points: bottom left,
top left, top right, bottom right.
"""
rect(cornerpoint::Point, w, h, action::Symbol) =
rect(cornerpoint.x, cornerpoint.y, w, h, action)
function rect(cornerpoint::Point, w, h, action::Symbol=:nothing;
vertices=false)
if vertices
return [
Point(cornerpoint.x, cornerpoint.y + h),
Point(cornerpoint.x, cornerpoint.y),
Point(cornerpoint.x + w, cornerpoint.y),
Point(cornerpoint.x + w, cornerpoint.y + h)
]
else
rect(cornerpoint.x, cornerpoint.y, w, h, action)
end
end

"""
box(cornerpoint1, cornerpoint2, action=:nothing; vertices=false)
box(cornerpoint1, cornerpoint2, action=:nothing;
vertices=false)
Create a rectangle between two points and do an action. Use `vertices=true` to
return an array of the four corner points rather than draw the box.
Create a rectangle between two points and do an action.
Use `vertices=true` to return an array of the four corner points: bottom left,
top left, top right, bottom right.
"""
function box(corner1::Point, corner2::Point, action::Symbol=:nothing; vertices=false)
function box(corner1::Point, corner2::Point, action::Symbol=:nothing;
vertices=false)
if vertices
return [
Point(corner1.x, corner1.y),
Expand All @@ -42,13 +61,18 @@ function box(corner1::Point, corner2::Point, action::Symbol=:nothing; vertices=f
rect(corner1.x, corner1.y, corner2.x - corner1.x, corner2.y - corner1.y, action)
end
end

"""
box(points::AbstractArray, action=:nothing)
Create a box/rectangle using the first two points of an array of Points to defined
opposite corners.
Use `vertices=true` to return an array of the four corner points: bottom left,
top left, top right, bottom right.
"""
box(bbox::AbstractArray, action::Symbol=:nothing) = box(bbox[1], bbox[2], action)
box(bbox::AbstractArray, action::Symbol=:nothing; kwargs...) =
box(bbox[1], bbox[2], action; kwargs...)

"""
box(pt::Point, width, height, action=:nothing; vertices=false)
Expand Down Expand Up @@ -128,10 +152,14 @@ end
ngon(x, y, radius, sides=5, orientation=0, action=:nothing;
vertices=false, reversepath=false)
Find the vertices of a regular n-sided polygon centered at `x`, `y` with circumradius `radius`.
Find the vertices of a regular n-sided polygon centered at `x`, `y` with
circumradius `radius`.
The polygon is drawn counterclockwise, starting with the first vertex drawn
below the positive x-axis.
`ngon()` draws the shapes: if you just want the raw points, use keyword argument
`vertices=true`, which returns the array of points instead. Compare:
If you just want the raw points, use keyword argument `vertices=true`, which
returns the array of points instead. Compare:
```julia
ngon(0, 0, 4, 4, 0, vertices=true) # returns the polygon's points:
Expand Down
6 changes: 3 additions & 3 deletions test/boundingboxtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ function test_bboxes(fname)
sethue("green")
box(box4, :fill)

@test isapprox(boxaspectratio(box1), 1.1666, atol = 0.01)
@test isapprox(boxdiagonal(box1), 614.63629, atol = 0.01)
@test isapprox(boxaspectratio(box1), 1.166, atol = 0.01)
@test isapprox(boxdiagonal(box1), 614.636, atol = 0.01)

# get vertices
bv = box(box1, vertices=true)
@test isapprox(bv[1].y, -433.33333, atol = 0.01)
@test isapprox(bv[1].y, 33.333, atol = 0.1)

setline(1)
# contains
Expand Down

0 comments on commit 463eb17

Please sign in to comment.