

Metron
Geometry, simplified.
Metron is a comprehensive collection of geometric functions and types that extend the 2D geometric primitives provided by CoreGraphics. Completely written in Swift, Metron allows you to express complex geometric calculations in very intuitive statements:
Example 1.
Circle(in: viewFrame).contains(touchPoint)
↳ creates a Circle
that fits (centered) inside viewFrame
, and checks if the touchPoint
is inside that circle.
Example 2.
(Angle(.pi) + Angle(270, unit: .degrees)).normalized // Angle of 0.5pi (90°)
↳ adds two Angles
, one expressed in radians (default) and one in degrees. The sum is normalized to within 0 and 2𝛑 radians (or 0 and 360 degrees).
Example 3.
LineSegment(a: startPoint, b: currentPoint).intersection(with: viewFrame.lineSegment(for: minYEdge))
↳ creates a LineSegment
between a (touch) start point and current point, and returns the intersection with the minYEdge
of a view’s frame (if these line segments indeed intersect).
Example 4.
let rotatedPoints = viewFrame.points.map { $0.applying(rotationTransform, anchorPoint: rotationPoint) }
let path: CGPath = Polygon(points: rotatedPoints).path
↳ maps each corner point of a view’s frame (a CGRect
) to a point to which a rotationTransform
is applied, taking rotationPoint
as the anchor point for the transform.
With these points, a Polygon
is initialized, representing the rotated rectangular frame of the view. From that polygon, we derive a CGPath
that can then be drawn.
And there’s much more…
Extensions
For CGPoint
For CGVector
For CGSize
For CGRect
For CGRectEdge
New Types
Line
LineSegment
Circle
Triangle
Square
Polygon
Corner
Angle
Installation
CocoaPods
Metron is available through CocoaPods. To install
it, simply add the following line to your Podfile:
pod "Metron"
Carthage
Metron is also available through Carthage. To install
it, simply add the following line to your Cartfile:
github "toineheuvelmans/Metron"
Swift Package Manager
Metron can also be used with the Swift Package Manager. Add Metron to the dependencies
value of your Package.swift
:
dependencies: [
.Package(url: "https://github.com/toineheuvelmans/metron.git", majorVersion: 1)
]
Suggestions or feedback?
Feel free to create a pull request, open an issue or find me on Twitter.
License
Metron is available under the MIT license. See the LICENSE file for more info.
Metron
Geometry, simplified.
Metron is a comprehensive collection of geometric functions and types that extend the 2D geometric primitives provided by CoreGraphics. Completely written in Swift, Metron allows you to express complex geometric calculations in very intuitive statements:
Example 1.
↳ creates a
Circle
that fits (centered) insideviewFrame
, and checks if thetouchPoint
is inside that circle.Example 2.
↳ adds two
Angles
, one expressed in radians (default) and one in degrees. The sum is normalized to within 0 and 2𝛑 radians (or 0 and 360 degrees).Example 3.
↳ creates a
LineSegment
between a (touch) start point and current point, and returns the intersection with theminYEdge
of a view’s frame (if these line segments indeed intersect).Example 4.
↳ maps each corner point of a view’s frame (a
CGRect
) to a point to which arotationTransform
is applied, takingrotationPoint
as the anchor point for the transform. With these points, aPolygon
is initialized, representing the rotated rectangular frame of the view. From that polygon, we derive aCGPath
that can then be drawn.And there’s much more…
Extensions
For CGPoint
CGPoint
CGRect
CGRect
CGRect
CGVector
CGPoints
(returns aPolygon
)For CGVector
CGPoint
CGAffineTransform
extensionsFor CGSize
CGSize
For CGRect
CGPath
For CGRectEdge
For CGAffineTransform
CGVector
New Types
Line
Line
orLineSegment
LineSegment
Line
Line
orLineSegment
CGPath
Circle
CGPath
Triangle
CGPoint
)LineSegment
)Angle
, see further on)LineSegment
)LineSegment
)CGPath
Square
CGPath
CGRect
Polygon
CGPath
Corner
CGRectEdges
)Angle
Angle
CGAffineTransform
withAngle
Installation
CocoaPods
Metron is available through CocoaPods. To install it, simply add the following line to your Podfile:
Carthage
Metron is also available through Carthage. To install it, simply add the following line to your Cartfile:
Swift Package Manager
Metron can also be used with the Swift Package Manager. Add Metron to the
dependencies
value of yourPackage.swift
:Suggestions or feedback?
Feel free to create a pull request, open an issue or find me on Twitter.
License
Metron is available under the MIT license. See the LICENSE file for more info.