Areas and Volumes

Often times in various fields of applied mathematics, we want to compute the area or volume of something.

abstract type Shape end
abstract type Parallel <: Shape end
abstract type Elliptical <: Shape end

struct Rectangle{T} <: Parallel
    a::T
    b::T
end

struct Square{T} <: Parallel
    a::T
end

struct Parallelogram{T} <: Parallel
    a::T
    b::T
end

struct Parallelepiped{T} <: Parallel
    x::T
    y::T
    z::T
end

struct Circle{T} <: Elliptical
    r::T
end

struct Sphere{T} <: Elliptical
    r::T
end

struct Annulus{T} <: Elliptical
    R::T
    r::T
end

struct Ellipse{T} <: Elliptical
    a::T
    b::T
end

The formulas for area vary based on the shape.

area(e::Ellipse) = π * e.a * e.b
area(c::Circle) = π * c.r^2
area(a::Annulus) = π * (a.R^2 - a.r^2)
area(s::Sphere) = 4 * π * s.r^2

volume(s::Sphere) = 4/3 * π * s.r^3
volume(b::Parallelepiped) = b.x * b.y * b.z