Geometry2D.jl

Basic shapes and related geometrical concepts come up frequently, this basic module defines structures and functions providing common calculations. This module is not attempting to be a 2D sketcher but rather provides clear, explicit, and verbose functions that improve users' maintainability and conceptual clarity.

Included entities/concepts:

  • Points
  • Triangles
  • Circles
  • Ellipses
  • Spirals
  • Homogeneous transforms

Many more entities and functions are under consideration, pending need. Please open an enhancement issue to help us prioritize our efforts.

Installation

using Pkg
Pkg.add(url="https://github.com/mechanomy/Geometry2D.jl.git")

Basic Example

using Unitful, Unitful.DefaultSymbols
using Plots
using Geometry2D

p = Point(1mm,2u"inch")
plot(p, label="Point", title="Basic Example", aspect_ratio=:equal)

pa = Point(1mm,1u"inch")
r = 50mm
ca = Circle(pa, r)
plot!(ca, label="Circle A")

cb = Circle(5mm, 50mm, 20mm)
plot!(cb, label="Circle B")

API

Geometry2D.Geometry2DModule

Geometry2D models basic geometric shapes and common geometric formulae. This is a flat module with entities grouped into similar files under src.

See online documentation at https://mechanomy.github.io/Geometry2D.jl/dev/ and test for examples of each function.

source
Geometry2D.CircleMethod
Circle( x::Unitful.Length, y::Unitful.Length, r::Unitful.Length )

A circle having a center at x::Unitful.Length and y::Unitful.Length with radius::Unitful.Length.

source
Geometry2D.DeltaType

A difference between two points on the cartesian plane, measured in dx and dy from the plane's origin. This is introduced as a separate type to avoid using Vector{}s with inexplicit length.

source
Geometry2D.SpiralType

Describes a spiral progressing from a0,r0 to a1,r1. If thickness is not specified it is calculated such that each layer touches neighbors, a tightly wound spiral, if it is specified then an open spiral is modeled.

  • a0::AngleOrReal – start angle
  • a1::AngleOrReal – stop angle
  • r0::LengthOrReal – start radius
  • r1::LengthOrReal – stop radius
  • pitch::PitchOrReal – pitch
source
Geometry2D.SpiralMethod

Spiral( a0::AngleOrReal, a1::AngleOrReal, r0::LengthOrReal, r1::LengthOrReal ) Create a Spiral, calculating the pitch from a0, a1, r0, and r1.

source
Geometry2D.UnitVectorType

A UnitVector type is unitless, expressing only relative magnitude. It has fields x, y, and z

  • x

    Component in the x direction.

  • y

    Component in the y direction.

  • z

    Component in the z direction.

source
Geometry2D.Vector2DType

A 2D vector from origin to tip. Functions are provided to convert from the struct's cartesian format to polar.

source
Base.:*Method
(*)(p::Point, f::Real) :: Point

Multiplies p by the given factor f.

source
Base.:+Method
(+)(p::Point, d::Delta) :: Point

Adds a d Delta to Point p.

source
Base.:-Method
(-)(p::Point, d::Delta) :: Point

Subtracts a Delta d from the Point p.

source
Base.:-Method
(-)(a::Point, b::Point) :: Delta

Finds the Delta between Points a and b.

source
Base.:-Method
(-)(a::Real, uv::UnitVector) :: UnitVector

Provides negation of UnitVectors.

source
Base.:/Method
(/)(d::Delta, f::Real) :: Delta

Divides d by the given factor f.

source
Base.:/Method
(/)(p::Point, f::Real) :: Point

Divides p by the given factor f.

source
Base.angleMethod
angle(d::Delta) :: Real

Calculate the angle of Delta d relative to global x = horizontal.

source
Base.angleMethod
angle(v::Vector2D) :: Angle
angled(v::Vector2D) :: typeof(1.0u"°")

Calculate the angle of Vector2D v relative to global x = horizontal, via atan().

source
Base.isapproxMethod
isapprox(a::Delta, b::Delta; atol=0, rtol=√eps()) :: Bool

Approximately compare Deltas a and b via absolute tolerance atol and relative tolerance rtol, as in isapprox.

source
Base.isapproxMethod
isapprox(p::Point, q::Point; atol=0, rtol=√eps()) :: Bool

Approximately compare Points p and q via absolute tolerance atol and relative tolerance rtol, as in isapprox.

source
Base.isapproxMethod
isapprox(a::UnitVector, b::UnitVector; atol=0, rtol=√eps()) :: Bool

Approximately compare UnitVectors a to b via absolute tolerance atol and relative tolerance rtol, as in isapprox.

source
Base.isapproxMethod
isapprox(a::UnitVector2D, b::UnitVector2D; atol=0, rtol=√eps()) :: Bool

Approximately compare UnitVector2Ds a and b via absolute tolerance atol and relative tolerance rtol, as in isapprox.

source
Base.isapproxMethod
isapprox(a::Vector2D, b::Vector2D; atol=0, rtol=√eps()) :: Bool

Approximately compare Vector2Ds a to b via absolute tolerance atol and relative tolerance rtol, as in isapprox.

source
Geometry2D.RzMethod
Rz(angle::Real)

Create a 2D rotation matrix effecting a rotation of angle. Homogeneous transformation matrices are multiplicative and therefore should be unitless.

source
Geometry2D.RzMethod
Rz(angle::Angle)

Create a 2D rotation matrix effecting a rotation of angle. Homogeneous transformation matrices are multiplicative and therefore should be unitless.

source
Geometry2D.TxMethod
Tx(p::Point, d::Unitful.Length)

Translate p a distance of d in the X direction. Homogeneous transformation matrices are multiplicative and therefore should be unitless.

source
Geometry2D.TxMethod

Tx(a::Real) Create a 2D translation matrix translating along local X an angle a. Homogeneous transformation matrices are multiplicative and therefore should be unitless.

source
Geometry2D.TyMethod
Ty(p::Point, d::Unitful.Length)

Translate p a distance of d in the X direction. Homogeneous transformation matrices are multiplicative and therefore should be unitless.

source
Geometry2D.TyMethod

Ty(b::Real) Create a 2D translation matrix translating along local Y by b. Homogeneous transformation matrices are multiplicative and therefore should be unitless.

source
Geometry2D.angleAdjacent2OppositeMethod
angleAdjacent2Opposite(angle::Angle, adjacent::Unitful.Length) :: Unitful.Length
angleAdjacent2Opposite(angle::Angle, adjacent::Unitful.Length) :: Unitful.Length

For a right triangle, find the length of the 'opposite' leg, given angle and adjacent.

source
Geometry2D.angleOpposite2AdjacentMethod
angleOpposite2Adjacent(angle::Angle, opposite::Unitful.Length) :: Unitful.Length
angleOpposite2Adjacent(; angle::Angle, opposite::Unitful.Length) :: Unitful.Length

For a right triangle, find the length of the 'adjacent' leg, given angle and opposite.

source
Geometry2D.calcLengthMethod
calcLength(a0::AngleOrReal, a1::AngleOrReal, r0::LengthOrReal, r1::LengthOrReal)
calcLength(;a0::AngleOrReal, a1::AngleOrReal, r0::LengthOrReal, r1::LengthOrReal)
calcLength(s::Spiral)

Length along the spiral, as if it were unrolled.

source
Geometry2D.calcPitchMethod
calcPitch(a0::AngleOrReal, a1::AngleOrReal, r0::LengthOrReal, r1::LengthOrReal)
calcPitch(; a0::AngleOrReal, a1::AngleOrReal, r0::LengthOrReal, r1::LengthOrReal)
calcPitch(s::Spiral)

Calculate a spiral's pitch from (r1-r0)/(a1-a0). The nominal units of Pitch defined to be [m/rev], but this will return Real if the arguments or Spiral are that, or a Pitch,m/rad,m/deg depending on __.

source
Geometry2D.circleArcLengthMethod
circleArcLength( radius::Unitful.Length, angle::Angle )

Compute the circlular arc length at radius over angle.

angle = 20u"°" 
radius = 5u"mm"
len = circleArcLength(angle, radius)
source
Geometry2D.deltaMethod
delta(v::Vector2D) :: Delta

Given v, return the difference in x and y as a Delta from v's tip to origin.

source
Geometry2D.distanceMethod
distance(a::Point, b::Point ) :: Unitful.Length

Finds the straight-line distance between a and b. (It is nonsensical to ask for the 'distance' of a Delta, rather Deltas have norm().)

source
Geometry2D.ellipticArcLengthMethod
ellipticArcLength(a::Number, b::Number, start::Number, stop::Number)

Calculates the arc length of an ellipse from major axis a towards minor axis b between start and stop angles, as measured from the major axis a.

source
Geometry2D.ellipticArcLengthMethod
ellipticArcLength(a::Number, b::Number, angle::Number ) :: Number

Calculates the arc length of an ellipse from major axis a towards minor axis b through angle, measured from a, via elliptic integral: L = b * elliptic_e( atan(a/b*tan(angle)), 1-a^2/b^2 )

source
Geometry2D.ellipticArcLengthMethod
ellipticArcLength(a::Unitful.Length, b::Unitful.Length, angle::Angle)

Unitful version. Calculates the arc length of an ellipse from major axis a towards minor axis b through angle, measured from major axis a, via elliptic integral:. L = b * elliptic_e( atan(a/b*tan(angle)), 1-a^2/b^2 )

source
Geometry2D.isSegmentMutuallyTangentMethod
isSegmentMutuallyTangent(; cA::Circle, cB::Circle, thA::Radian, thB::Radian, tol::Number=1e-3) :: Bool

Given Circles circleA and circleB, tests whether the points on their edges at angles thA and thB define a line segment that is tangent to both circles. tol is the tolerance on the tangency criterion.

source
Geometry2D.isSegmentPerpendicularToParallelVectorsFunction
isSegmentPerpendicularToParallelVectors( vA::Vector2D, vB::Vector2D, tol::Number=1e-3) :: Bool

Tests whether a segment connecting the tips of vA and vB is perpendicular to both, which implies parallelity of vA and vB. The calculation compares the direction of the cross products of vA and the tip-connecting segment, and vB and the segment, that these are within tol of each other.

source
Geometry2D.lawOfCosinesMethod
lawOfCosines(legA::Unitful.Length, legB::Unitful.Length, angleAB::Angle) :: Unitful.Length
lawOfCosines(legA::Unitful.Length, legB::Unitful.Length, angleAB::Angle) :: Unitful.Length

For any triangle, finds the length of the unknown legC from legA, legB, and angleAB between them.

source
Geometry2D.lawOfSinesMethod
lawOfSines(legA::Unitful.Length, angleBC::Angle, angleCA::Angle) :: Unitful.Length
lawOfSines(; legA::Unitful.Length, angleBC::Angle, angleCA::Angle) :: Unitful.Length

For any triangle with legs A, B, C and angles AB, BC, CA, so that legA is the open side of angleBC, finds legB corresponding to angleCA.

source
Geometry2D.legHypotenuse2LegMethod
legHypotenuse2Leg( leg::Unitful.Length, hyp::Unitful.Length  ) :: Unitful.Length
legHypotenuse2Leg(; leg::Unitful.Length, hyp::Unitful.Length  ) :: Unitful.Length

For a right triangle, find the length of the third leg from hypotenuse hyp and leg.

source
Geometry2D.legLeg2HypotenuseMethod
legLeg2Hypotenuse( a::Unitful.Length, b::Unitful.Length ) :: Unitful.Length
legLeg2Hypotenuse(; a::Unitful.Length, b::Unitful.Length ) :: Unitful.Length

For a right triangle, find the length of the hypotenuse from the shorter legs a and b.

source
Geometry2D.point2hvecMethod
point2hvec(p::Point)

Convert p into a 1-terminated Vector for multiplication with some homogeneous transformation matrix.

source
Geometry2D.seriesCartesianFunction
seriesCartesian(s::Spiral, n::Int=1000)
seriesCartesian(; s::Spiral, n::Int=1000)

Spread n points over spiral s, returning an (xs,ys) tuple

source
Geometry2D.seriesPolarFunction
seriesPolar(s::Spiral, n::Int=1000)
seriesPolar(; s::Spiral, n::Int=1000)

Spread n points over spiral s, returning an (angles,radii) tuple.

source
KeywordDispatch.kwcallMethod
Point(;x::Unitful.Length, y::Unitful.Length)

Keyword constructor for a point on the cartesian plane, measured in x and y from the plane's origin.

source
KeywordDispatch.kwcallMethod
Circle(; center::Point, radius::Unitful.Length )

A circle having a center::Point and a radius::Unitful.Length.

source
KeywordDispatch.kwcallMethod
Spiral(; a0::AngleOrReal, a1::AngleOrReal, r0::LengthOrReal, r1::LengthOrReal, pitch::PitchOrReal) = Spiral(a0, a1, r0, r1, pitch)

Keyword create a Spiral with a0, a1, r0, r1, and pitch.

source
KeywordDispatch.kwcallMethod
Spiral( a0::AngleOrReal, a1::AngleOrReal, r0::LengthOrReal, r1::LengthOrReal )

Keyword create a Spiral, calculating the pitch from a0, a1, r0, and r1.

source
KeywordDispatch.kwcallMethod
Spiral(; a0::Real, a1::Real, r0::Real, r1::Real) = Spiral(a0, a1, r0, r1, calcPitch(a0,a1,r0,r1)::Real )

Keyword create a Spiral, calculating the pitch from a0[rad], a1[rad], r0, and r1.

source
LinearAlgebra.normMethod
norm( uv::UnitVector; p=2 ) :: Real

Returns the UnitVector's 2-norm, which should always be 1.

source
RecipesBase.apply_recipeMethod

A plot recipe for plotting Circles under Plots.jl.

c = Circle( 3mm,4mm, 5mm )
plot(c) #plot(c, linecolor=:red, ...kwArgs... )
source