Skip to content

Commit

Permalink
Add more shape tests and more issubsets
Browse files Browse the repository at this point in the history
  • Loading branch information
jondea committed Sep 9, 2018
1 parent ddc5410 commit 2d0f7dd
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 10 deletions.
4 changes: 4 additions & 0 deletions src/shapes/circle.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ function issubset(circle::Circle{T}, rect::Rectangle{T}) where {T}
all((origin(circle) .+ circle.radius) .<= topright(rect))
end

function issubset(rect::Rectangle{T}, circle::Circle{T}) where {T}
issubset(Circle(origin(rect),outer_radius(rect)), circle)
end

import Base.in
function in(x::AbstractVector, circle::Circle)::Bool
norm(origin(circle) .- x) <= circle.radius
Expand Down
13 changes: 13 additions & 0 deletions src/shapes/rectangle.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,25 @@ end
# Alternate constructors, where type is inferred naturally
Rectangle(origin::Tuple{T,T}, width::T, height::T) where {T} = Rectangle{T}(origin, width, height)
Rectangle(origin::Vector{T}, width::T, height::T) where {T} = Rectangle{T}(origin, width, height)
# If no position is given, assume origin is at zero
Rectangle(width::T, height::T) where {T} = Rectangle{T}(SVector(zero(T),zero(T)), width, height)

name(shape::Rectangle) = "Rectangle"

outer_radius(r::Rectangle) = sqrt((r.width/2)^2 + (r.height/2)^2)
volume(rectangle::Rectangle) = rectangle.width*rectangle.height

import Base.issubset
function issubset{T}(inner_rect::Rectangle{T}, outer_rect::Rectangle{T})
all(topright(inner_rect) .<= topright(outer_rect)) &&
all(bottomleft(inner_rect) .>= bottomleft(outer_rect))
end

import Base.in
function in(x::AbstractVector, r::Rectangle)::Bool
all(abs.(x .- r.origin) .<= SVector(r.width, r.height))
end

import Base.(==)
function ==(r1::Rectangle{T}, r2::Rectangle{T}) where T
r1.origin == r2.origin &&
Expand Down
10 changes: 5 additions & 5 deletions src/shapes/sphere.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Sphere(origin::Vector{T}, radius::T) where {T} = Sphere{T}(origin, radius)

name(shape::Sphere) = "Sphere"

outer_radius(c::Sphere) = c.radius
outer_radius(sphere::Sphere) = sphere.radius
volume(shape::Sphere) = 4//3 * π * shape.radius^3

import Base.issubset
Expand All @@ -23,10 +23,10 @@ function in(x::AbstractVector, sphere::Sphere)::Bool
norm(origin(sphere) .- x) <= sphere.radius
end

function iscongruent(c1::Sphere{T}, c2::Sphere{T}) where T
c1.radius == c2.radius
function iscongruent(s1::Sphere{T}, s2::Sphere{T}) where T
s1.radius == s2.radius
end

function congruent(c::Sphere{T}, x) where T
Sphere{T}(x, c.radius)
function congruent(s::Sphere{T}, x) where T
Sphere{T}(x, s.radius)
end
52 changes: 47 additions & 5 deletions test/shapetests.jl
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
@testset "Shape" begin
@testset "Rectangle" begin
rectangle = Rectangle((0.0,0.0), (2.0,3.0))
rectangle = Rectangle((0.0,0.0), (2.0, 3.0))
o = origin(rectangle)
@test volume(rectangle) 6.0
@test name(rectangle) == "Rectangle"

@test o rectangle
@test (o+[0.0,3.1]) rectangle
@test iscongruent(rectangle, congruent(rectangle, SVector(3.0,4.0)))

smaller_rectangle = Rectangle(o, 1.0, 2.0)
@test smaller_rectangle rectangle
@test rectangle == bounding_rectangle(rectangle)
@test rectangle Circle(o, outer_radius(rectangle))

# Test different ways to construct Rectangle produce same results
@test Rectangle((0.0,0.0), 1.0, 2.0) == Rectangle([0.0,0.0], 1.0, 2.0)
@test Rectangle((0.0,0.0), 1.0, 2.0) == Rectangle([0.0,0.0], 1.0, 2.0) # Tuple or Vector
@test Rectangle((0.0,0.0), 1.0, 2.0) == Rectangle(1.0, 2.0) # Assumes origin at zero

@testset "Boundary functions" begin
x, y = boundary_functions(rectangle)
Expand All @@ -19,13 +30,26 @@
end

@testset "Circle" begin
circle = Circle([6.7,8.9],2.0)
radius = 2.0
o = [6.7,8.9]
circle = Circle(o, radius)
circle_bounding_rectangle = bounding_rectangle(circle)
@test volume(circle)/volume(circle_bounding_rectangle) 0.7853981633974483
@test name(circle) == "Circle"

# Test different ways to construct Rectangle produce same results
@test Circle((0.0,0.0), 1.0) == Circle([0.0,0.0], 1.0)
@test outer_radius(circle) == radius
@test o circle
@test (o+[0.0,radius+0.1]) circle
@test iscongruent(circle, congruent(circle,SVector(3.0,4.0)))
@test volume(circle) 12.566370614359172

smaller_circle = Circle(o, radius-2)
@test smaller_circle circle
@test circle circle_bounding_rectangle

# Test different ways to construct Circle produce same results
@test Circle((0.0,0.0), 1.0) == Circle([0.0,0.0], 1.0) # Tuple or Vector
@test Circle((0.0,0.0), 1.0) == Circle(1.0) # Assumes origin at zero

@testset "Boundary functions" begin
x, y = boundary_functions(Circle([-1.0,2.0],3.0))
Expand Down Expand Up @@ -81,6 +105,24 @@
end
end

@testset "Sphere" begin
radius = 4.2
o = SVector(3.4, -2.1, 2.0)
sphere = Sphere(o, radius)
@test name(sphere) == "Sphere"
@test outer_radius(sphere) == radius
@test o sphere
@test (o+[0.0,radius+0.1,0.0]) sphere
@test iscongruent(sphere, congruent(sphere,SVector(3.0,4.0,1.0)))
@test volume(sphere) 310.33908869221415

smaller_sphere = Sphere(o, radius-2)
@test smaller_sphere sphere

# Test different ways to construct Rectangle produce same results
@test Sphere((0.0,0.0,0.0), 1.0) == Sphere([0.0,0.0,0.0], 1.0)
end

@testset "Plot Shapes" begin
# Just try each to see if we have any errors (yes thats a very low bar)
rectangle = Rectangle([0.0,0.0],[2.0,3.0])
Expand Down

0 comments on commit 2d0f7dd

Please sign in to comment.