forked from robb/Cartography
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Coefficients.swift
61 lines (45 loc) · 1.33 KB
/
Coefficients.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//
// Coefficients.swift
// Cartography
//
// Created by Robert Böhnke on 17/06/14.
// Copyright (c) 2014 Robert Böhnke. All rights reserved.
//
import Foundation
public struct Coefficients {
var multiplier: CGFloat = 1
var constant: CGFloat = 0
init() { }
init(_ multiplier: CGFloat, _ constant: CGFloat) {
self.constant = constant
self.multiplier = multiplier
}
}
// MARK: Addition
public func + (c: CGFloat, rhs: Coefficients) -> Coefficients {
return Coefficients(rhs.multiplier, rhs.constant + c)
}
public func + (lhs: Coefficients, rhs: CGFloat) -> Coefficients {
return rhs + lhs
}
// MARK: Subtraction
public func - (c: CGFloat, rhs: Coefficients) -> Coefficients {
return Coefficients(rhs.multiplier, rhs.constant - c)
}
public func - (lhs: Coefficients, rhs: CGFloat) -> Coefficients {
return rhs - lhs
}
// MARK: Multiplication
public func * (m: CGFloat, rhs: Coefficients) -> Coefficients {
return Coefficients(rhs.multiplier * m, rhs.constant * m)
}
public func * (lhs: Coefficients, rhs: CGFloat) -> Coefficients {
return rhs * lhs
}
// MARK: Division
public func / (m: CGFloat, rhs: Coefficients) -> Coefficients {
return Coefficients(rhs.multiplier / m, rhs.constant / m)
}
public func / (lhs: Coefficients, rhs: CGFloat) -> Coefficients {
return rhs / lhs
}