Provides an abstraction for mathematical angle. Because the angle is a type, errors such as:
- sum or subtract a degree angle with a radian angle
- pass degree angle to a trigonometric function from standard library
are either not possible or catched at compile time. Support convenience initializers, operators and functions for natural, fast and productive workflow.
Table of contents
Usage
Importing the module
You can import this module using:
import DLAngle
Creation
let degree = Degree(rawValue: 90.0)
let zeroDegrees = Degree()
let radian = Radian(rawValue: Double.pi / 2)
let zeroRadians = Radian()
Converting between Degree and Radian
From Degree to Radian:
let degree = Degree(rawValue: 90.0)
let radian = Radian(degree: degree)
From Radian to Degree:
let radian = Radian(rawValue: Double.pi)
let degree = Degree(radian: radian)
Basic Operations
Addition
let radian = Radian(degrees: 90.0)
radian += Radian(degress: 45.0)
// radian.rawValue = 3 * Double.pi / 4
Subtraction
var degrees = Degree(radians: Double.pi / 2)
degrees = degrees - Degree(radians: Double.pi / 6)
// degrees.rawValue = 60.0
Multiplcation
let half = Radian(rawValue: Double.pi / 2)
let pi = one * 2
// '2 * one' works as well
Division
let pi = Radian(rawValue: Double.pi)
let sixtyDegreesRadian = one / 3
// '1 / pi' works as well
Normalization
let degree = Degree(rawValue: 750.0)
degree.normalize()
// degree.rawValue = 30.0
let angle1 = Radian(rawvalue: 7 * Double.pi)
let angle2 = angle1.normalized()
// angle2.rawValue = Double.pi
Equality
let angle1 = Radian(rawValue: Double.pi)
let angle2 = Radian(degrees: 180.0)
angle1 == angle2 // true
Comparison
let angle1 = Radian(rawValue: Double.pi)
let angle2 = Radian(degrees: 90.0)
angle1 < angle2 // false
Foundation.Measurement conversion
let angleMeasurement = Measurement<UnitAngle>(value: 45.0, unit: .degrees)
let radian = Radian(measurement: angleMeasurement)
radian.measurement // Measurement<UnitAngle>(value: Double.pi / 4, unit: .radians)
let degrees = Degrees(measurement: angleMeasurement)
degrees.measurement // Measurement<UnitAngle>(value: 45.0, unit: .degrees)
Inverse trigonometric functions
Inverse functions are provided by using convenience constructors of Radian type.
do {
let angle = try Radian(acos: -1.0)
} catch {
// handle error
}
Trigonometric functions
let angle = Radian(degrees: 45.0)
if let sin: Double = try? angle.sin() {
// use computed sine value
}
Constants
Frequently used Radian angles have constants:
let pi = Radian.pi
let half_quadrant_II = Radian.pi_2 + Radian.pi_4
Installation
Supported Operating Systems
- macOS 10.12 and later
- iOS 10.0 and later
- watchOS 3.0 and later
- tvOS 10.0 and later
- Ubuntu - check official site to find which versions support Swift 4.0.
Carthage
1.github "davidlivadaru/DLAngle"
If you need the framework only for a single OS, then I propose to use --platform [iOS|macOS|watchOS|tvOS]
specifier when your perform carthage update
.
CocoaPods
2.Add the dependency in your Podfile
.
pod 'DLAngle'
Swift Package Manager
3.Add the the following dependecy in your Package.swift
:
dependencies: [
.package(url: "https://github.com/davidlivadaru/DLAngle.git", .upToNextMajor(from: "1.0.0"))
]
and update your target's dependencies:
targets: [
.target(
name: "YourTargetName",
dependencies: ["DLAngle"])),
]
Contribution
Module is covered by unit tests, however, bugs always slip through. If you find a bug in the module create an issue.
If you want to contribute on fixing bugs or implementing new features then create a pull request.
License
DLAngle is released under MIT license. See LICENSE for details.