|
12 | 12 | import Foundation |
13 | 13 | import CoreGraphics |
14 | 14 |
|
15 | | -extension Comparable |
16 | | -{ |
17 | | - func clamped(to range: ClosedRange<Self>) -> Self |
18 | | - { |
19 | | - if self > range.upperBound |
20 | | - { |
| 15 | +extension Comparable { |
| 16 | + func clamped(to range: ClosedRange<Self>) -> Self { |
| 17 | + if self > range.upperBound { |
21 | 18 | return range.upperBound |
22 | | - } |
23 | | - else if self < range.lowerBound |
24 | | - { |
| 19 | + } else if self < range.lowerBound { |
25 | 20 | return range.lowerBound |
26 | | - } |
27 | | - else |
28 | | - { |
| 21 | + } else { |
29 | 22 | return self |
30 | 23 | } |
31 | 24 | } |
32 | 25 | } |
33 | 26 |
|
34 | | -extension FloatingPoint |
35 | | -{ |
36 | | - var DEG2RAD: Self |
37 | | - { |
| 27 | +extension FloatingPoint { |
| 28 | + var DEG2RAD: Self { |
38 | 29 | return self * .pi / 180 |
39 | 30 | } |
40 | 31 |
|
41 | | - var RAD2DEG: Self |
42 | | - { |
| 32 | + var RAD2DEG: Self { |
43 | 33 | return self * 180 / .pi |
44 | 34 | } |
45 | 35 |
|
46 | | - /// - Note: Value must be in degrees |
47 | | - /// - Returns: An angle between 0.0 < 360.0 (not less than zero, less than 360) |
48 | | - var normalizedAngle: Self |
49 | | - { |
| 36 | + var normalizedAngle: Self { |
50 | 37 | let angle = truncatingRemainder(dividingBy: 360) |
51 | 38 | return (sign == .minus) ? angle + 360 : angle |
52 | 39 | } |
53 | 40 | } |
54 | 41 |
|
55 | | -extension CGSize |
56 | | -{ |
57 | | - func rotatedBy(degrees: CGFloat) -> CGSize |
58 | | - { |
| 42 | +extension CGSize { |
| 43 | + func rotatedBy(degrees: CGFloat) -> CGSize { |
59 | 44 | let radians = degrees.DEG2RAD |
60 | 45 | return rotatedBy(radians: radians) |
61 | 46 | } |
62 | 47 |
|
63 | | - func rotatedBy(radians: CGFloat) -> CGSize |
64 | | - { |
| 48 | + func rotatedBy(radians: CGFloat) -> CGSize { |
65 | 49 | return CGSize( |
66 | 50 | width: abs(width * cos(radians)) + abs(height * sin(radians)), |
67 | 51 | height: abs(width * sin(radians)) + abs(height * cos(radians)) |
68 | 52 | ) |
69 | 53 | } |
70 | 54 | } |
71 | 55 |
|
72 | | -extension Double |
73 | | -{ |
74 | | - /// Rounds the number to the nearest multiple of it's order of magnitude, rounding away from zero if halfway. |
75 | | - func roundedToNextSignificant() -> Double |
76 | | - { |
77 | | - guard |
78 | | - !isInfinite, |
79 | | - !isNaN, |
80 | | - self != 0 |
81 | | - else { return self } |
| 56 | +extension Double { |
| 57 | + func roundedToNextSignificant() -> Double { |
| 58 | + guard !isInfinite, !isNaN, self != 0 else { return self } |
82 | 59 |
|
83 | | - let d = ceil(log10(self < 0 ? -self : self)) |
| 60 | + // Use Foundation.log10 |
| 61 | + let d = ceil(Foundation.log10(self < 0 ? -self : self)) |
84 | 62 | let pw = 1 - Int(d) |
85 | | - let magnitude = pow(10.0, Double(pw)) |
| 63 | + |
| 64 | + // Use Foundation.pow |
| 65 | + let magnitude = Foundation.pow(10.0, Double(pw)) |
86 | 66 | let shifted = (self * magnitude).rounded() |
87 | 67 | return shifted / magnitude |
88 | 68 | } |
89 | 69 |
|
90 | | - var decimalPlaces: Int |
91 | | - { |
92 | | - guard |
93 | | - !isNaN, |
94 | | - !isInfinite, |
95 | | - self != 0.0 |
96 | | - else { return 0 } |
| 70 | + var decimalPlaces: Int { |
| 71 | + guard !isNaN, !isInfinite, self != 0.0 else { return 0 } |
97 | 72 |
|
98 | 73 | let i = roundedToNextSignificant() |
99 | 74 |
|
100 | | - guard |
101 | | - !i.isInfinite, |
102 | | - !i.isNaN |
103 | | - else { return 0 } |
| 75 | + guard !i.isInfinite, !i.isNaN else { return 0 } |
104 | 76 |
|
105 | | - return Int(ceil(-log10(i))) + 2 |
| 77 | + // Use Foundation.log10 |
| 78 | + return Int(ceil(-Foundation.log10(i))) + 2 |
106 | 79 | } |
107 | 80 | } |
108 | 81 |
|
109 | | -extension CGPoint |
110 | | -{ |
111 | | - /// Calculates the position around a center point, depending on the distance from the center, and the angle of the position around the center. |
112 | | - func moving(distance: CGFloat, atAngle angle: CGFloat) -> CGPoint |
113 | | - { |
| 82 | +extension CGPoint { |
| 83 | + func moving(distance: CGFloat, atAngle angle: CGFloat) -> CGPoint { |
114 | 84 | return CGPoint(x: x + distance * cos(angle.DEG2RAD), |
115 | 85 | y: y + distance * sin(angle.DEG2RAD)) |
116 | 86 | } |
|
0 commit comments