Skip to content

Commit 14de1e3

Browse files
authored
Merge pull request #4 from chenhaiteng/develop
Develop
2 parents f55db8f + 469a8f9 commit 14de1e3

File tree

4 files changed

+37
-44
lines changed

4 files changed

+37
-44
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
/Packages
44
/*.xcodeproj
55
xcuserdata/
6+
CoreGraphicsExtension.xcscheme

Sources/CoreGraphicsExtension/CGAngle.swift

Lines changed: 7 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -8,45 +8,14 @@
88
import Foundation
99
import CoreGraphics
1010

11-
public struct CGAngle {
12-
public static var zero = CGAngle()
13-
public var radians: CGFloat
14-
15-
public var degrees: CGFloat {
16-
radians*180/CGFloat.pi
17-
}
18-
19-
@inlinable public init() {
20-
radians = 0
21-
}
11+
public typealias CGAngle = CGFloat
2212

23-
@inlinable public init<T: BinaryFloatingPoint>(radians: T) {
24-
self.radians = CGFloat(radians)
25-
}
26-
27-
@inlinable public init<T: BinaryFloatingPoint>(degrees: T) {
28-
self.radians = CGFloat(degrees)*CGFloat.pi/180
29-
}
30-
31-
@inlinable public static func radians<T: BinaryFloatingPoint>(_ radians: T) -> CGAngle {
32-
CGAngle(radians: radians)
13+
extension CGAngle {
14+
public static var zero: CGFloat = 0.0
15+
public var radians: CGFloat {
16+
self
3317
}
34-
35-
@inlinable public static func degrees<T: BinaryFloatingPoint>(_ degrees: T) -> CGAngle {
36-
CGAngle(degrees: degrees)
37-
}
38-
39-
public static func +(lhs:CGAngle, rhs: CGAngle) -> CGAngle {
40-
CGAngle(radians: lhs.radians + rhs.radians)
41-
}
42-
43-
public static func +=(lhs: inout CGAngle, rhs: CGAngle) {
44-
lhs.radians += rhs.radians
45-
}
46-
}
47-
48-
extension CGAngle: Hashable {
49-
public func hash(into hasher: inout Hasher) {
50-
hasher.combine(radians)
18+
public var degrees: CGFloat {
19+
self*180/CGFloat.pi
5120
}
5221
}

Sources/CoreGraphicsExtension/CGPolarPoint.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ public struct CGPolarPoint {
2222
@inlinable public init<T: BinaryFloatingPoint>(radius: T, angle: CGAngle) {
2323
self.radius = CGFloat(radius)
2424
cgangle = angle
25-
cgpoint = CGPoint(x: self.radius*cos(cgangle.radians), y: self.radius*sin(cgangle.radians))
25+
let _cos: CGFloat = cos(cgangle.radians)
26+
let _sin: CGFloat = sin(cgangle.radians)
27+
cgpoint = CGPoint(x: self.radius*_cos, y: self.radius*_sin)
2628
}
2729
}

Tests/CoreGraphicsExtensionTests/CoreGraphicsExtensionTests.swift

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,38 @@ import XCTest
22
@testable import CoreGraphicsExtension
33

44
final class CoreGraphicExtensionTests: XCTestCase {
5-
func testExample() {
6-
// This is an example of a functional test case.
7-
// Use XCTAssert and related functions to verify your tests produce the correct
8-
// results.
5+
6+
let __CosZero = cos(CGFloat.pi/2.0)
7+
let __Cos45 = cos(CGFloat.pi/4.0)
8+
func testCGAngle() {
9+
// Ensure zero works
910
XCTAssertEqual(CGAngle.zero.radians, 0)
11+
12+
let rightAngle = CGAngle.pi/2
13+
XCTAssertEqual(rightAngle, CGFloat.pi/2)
14+
XCTAssertEqual(rightAngle.degrees, 90)
15+
XCTAssertEqual(rightAngle.radians, CGFloat.pi/2)
16+
}
17+
18+
func testPolarPoint() {
19+
// Ensure zero works
1020
XCTAssertEqual(CGPolarPoint.zero.radius, 0)
1121
XCTAssertEqual(CGPolarPoint.zero.cgangle, CGAngle.zero)
1222
XCTAssertEqual(CGPolarPoint.zero.cgpoint, CGPoint.zero)
23+
24+
let upPoint = CGPolarPoint(radius: CGFloat(1), angle: CGAngle.pi/2)
25+
let rightUpPoint = CGPolarPoint(radius: CGFloat(sqrt(2)), angle: CGAngle.pi/4)
26+
XCTAssertEqual(upPoint.radius, 1)
27+
XCTAssertEqual(upPoint.cgangle, CGAngle.pi/2)
28+
//For floating precisly problem, it cannot compare with CGPoint(x: 0.0, y: 1.0) directly.
29+
XCTAssertEqual(upPoint.cgpoint, CGPoint(x: __CosZero, y: 1))
30+
31+
//For floating precisly problem, it cannot compare with CGPoint(x: 1.0, y: 1.0) directly.
32+
XCTAssertEqual(rightUpPoint.cgpoint, CGPoint(x: __Cos45*CGFloat(sqrt(2)), y:1))
1333
}
1434

1535
static var allTests = [
16-
("testExample", testExample),
36+
("CGAngle", testCGAngle),
37+
("CGPolarPoint", testPolarPoint),
1738
]
1839
}

0 commit comments

Comments
 (0)