Skip to content

Commit 1dbfafd

Browse files
authored
Merge pull request #68 from RougeWare/feature/Easier-mXmY
Made (min/max)X(min/max)Y more useful
2 parents 79c4161 + 4799ab4 commit 1dbfafd

File tree

4 files changed

+120
-2
lines changed

4 files changed

+120
-2
lines changed

Sources/RectangleTools/Synthesized Conveniences/Point2D Extensions.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,25 @@ public extension Point2D {
2424

2525

2626

27+
public extension Point2D
28+
where Length: BinaryFloatingPoint
29+
{
30+
/// Creates a new point with the given values from a different type
31+
///
32+
/// - Parameters:
33+
/// - x: The X coordinate to convert
34+
/// - y: The Y coordinate to convert
35+
@inline(__always)
36+
init<OtherLength>(x: OtherLength, y: OtherLength)
37+
where OtherLength: BinaryFloatingPoint
38+
{
39+
self.init(measurementX: x,
40+
measurementY: y)
41+
}
42+
}
43+
44+
45+
2746
public extension Point2D
2847
where Length: MultiplicativeArithmetic,
2948
Length: AdditiveArithmetic,

Sources/RectangleTools/Synthesized Conveniences/Size2D Extensions.swift

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,25 @@ public extension Size2D {
2323

2424

2525

26+
public extension Point2D
27+
where Length: BinaryFloatingPoint
28+
{
29+
/// Creates a new size with the given values from a different type
30+
///
31+
/// - Parameters:
32+
/// - width: The width measurement to convert
33+
/// - height: The height measurement to convert
34+
@inline(__always)
35+
init<OtherLength>(width: OtherLength, height: OtherLength)
36+
where OtherLength: BinaryFloatingPoint
37+
{
38+
self.init(measurementX: width,
39+
measurementY: height)
40+
}
41+
}
42+
43+
44+
2645
// MARK: - CartesianMeasurable
2746

2847
public extension Size2D
@@ -50,7 +69,7 @@ public extension Size2D
5069
var maxY: Length { max(.zero, height) }
5170

5271

53-
// MARK: Points
72+
// MARK: Generic Point extremities
5473

5574
/// The point with the smallest X and Y values in this size
5675
@inlinable
@@ -67,7 +86,30 @@ public extension Size2D
6786
/// The point with the largest X and Y values in this size
6887
@inlinable
6988
func maxXmaxY<Point: Point2D>() -> Point where Point.Length == Self.Length { Point.init(x: maxX, y: maxY) }
89+
}
90+
91+
92+
93+
public extension Size2D
94+
where Length: BinaryFloatingPoint
95+
{
96+
// MARK: CGPoint extremities
7097

98+
/// The point with the smallest X and Y values in this size
99+
@inlinable
100+
func minXminY() -> CGPoint { .init(measurementX: minX, measurementY: minY) }
101+
102+
/// The point with the smallest X and largest Y values in this size
103+
@inlinable
104+
func minXmaxY() -> CGPoint { .init(measurementX: minX, measurementY: maxY) }
105+
106+
/// The point with the largest X and smallest Y values in this size
107+
@inlinable
108+
func maxXminY() -> CGPoint { .init(measurementX: maxX, measurementY: minY) }
109+
110+
/// The point with the largest X and Y values in this size
111+
@inlinable
112+
func maxXmaxY() -> CGPoint { .init(measurementX: maxX, measurementY: maxY) }
71113
}
72114

73115

Sources/RectangleTools/Synthesized Conveniences/TwoDimensional Extensions.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,18 @@ import MultiplicativeArithmetic
1414

1515
public extension TwoDimensional where Length: BinaryFloatingPoint {
1616

17+
/// Creates a new 2D with the given values from a different type
18+
///
19+
/// - Parameters:
20+
/// - measurementX: The X measurement to convert
21+
/// - measurementY: The Y measurement to convert
22+
init<OtherLength>(measurementX: OtherLength, measurementY: OtherLength)
23+
where OtherLength: BinaryFloatingPoint
24+
{
25+
self.init(measurementX: .init(measurementX),
26+
measurementY: .init(measurementY))
27+
}
28+
1729
/// Creates a new 2D object by converting the values of the given one
1830
///
1931
/// - Parameter other: Another 2D object to convert

Tests/RectangleToolsTests/Point Tests.swift

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,57 @@ import RectangleTools
1212

1313
final class Point_Tests: XCTestCase {
1414

15-
func testDistance() {
15+
func testPointToPointDistance() {
1616
XCTAssertEqual(CGPoint(x: 0, y: 0).distance(to: CGPoint(x: 1, y: 1)), sqrt(2))
17+
XCTAssertEqual(CGPoint(x: 0, y: 0).distance(to: CGPoint(x: -1, y: 1)), sqrt(2))
18+
XCTAssertEqual(CGPoint(x: 0, y: 0).distance(to: CGPoint(x: 1, y: -1)), sqrt(2))
1719
XCTAssertEqual(CGPoint(x: 0, y: 0).distance(to: CGPoint(x: -1, y: -1)), sqrt(2))
1820
}
1921

2022

23+
func testSizeExtremitiesDistance() {
24+
var cgSize = CGSize(width: 1, height: 1)
25+
XCTAssertEqual(cgSize.minXminY().distance(to: cgSize.maxXmaxY()), sqrt(2))
26+
27+
cgSize = CGSize(width: -1, height: 1)
28+
XCTAssertEqual(cgSize.minXminY().distance(to: cgSize.maxXmaxY()), sqrt(2))
29+
30+
cgSize = CGSize(width: 1, height: -1)
31+
XCTAssertEqual(cgSize.minXminY().distance(to: cgSize.maxXmaxY()), sqrt(2))
32+
33+
cgSize = CGSize(width: -1, height: -1)
34+
XCTAssertEqual(cgSize.minXminY().distance(to: cgSize.maxXmaxY()), sqrt(2))
35+
}
36+
37+
38+
func testRectExtremitiesDistance() {
39+
var cgRect = CGRect(x: 0, y: 0, width: 1, height: 1)
40+
XCTAssertEqual(cgRect.minXminY.distance(to: cgRect.maxXmaxY), sqrt(2))
41+
42+
cgRect = CGRect(x: 0, y: 0, width: -1, height: 1)
43+
XCTAssertEqual(cgRect.minXminY.distance(to: cgRect.maxXmaxY), sqrt(2))
44+
45+
cgRect = CGRect(x: 0, y: 0, width: 1, height: -1)
46+
XCTAssertEqual(cgRect.minXminY.distance(to: cgRect.maxXmaxY), sqrt(2))
47+
48+
cgRect = CGRect(x: 0, y: 0, width: -1, height: -1)
49+
XCTAssertEqual(cgRect.minXminY.distance(to: cgRect.maxXmaxY), sqrt(2))
50+
51+
52+
cgRect = CGRect(x: .random(in: -1000 ... 1000), y: .random(in: -1000 ... 1000), width: 1, height: 1)
53+
XCTAssertEqual(cgRect.minXminY.distance(to: cgRect.maxXmaxY), sqrt(2))
54+
55+
cgRect = CGRect(x: .random(in: -1000 ... 1000), y: .random(in: -1000 ... 1000), width: -1, height: 1)
56+
XCTAssertEqual(cgRect.minXminY.distance(to: cgRect.maxXmaxY), sqrt(2))
57+
58+
cgRect = CGRect(x: .random(in: -1000 ... 1000), y: .random(in: -1000 ... 1000), width: 1, height: -1)
59+
XCTAssertEqual(cgRect.minXminY.distance(to: cgRect.maxXmaxY), sqrt(2))
60+
61+
cgRect = CGRect(x: .random(in: -1000 ... 1000), y: .random(in: -1000 ... 1000), width: -1, height: -1)
62+
XCTAssertEqual(cgRect.minXminY.distance(to: cgRect.maxXmaxY), sqrt(2))
63+
}
64+
65+
2166
func testMagnitude() {
2267
// https://www.wolframalpha.com/input?i=distance+from+%28-2%2C-1%29+to+%285%2C6%29
2368
XCTAssertEqual(CIVector(x: -2, y: -1, z: 5, w: 6).magnitude, 7 * sqrt(2))

0 commit comments

Comments
 (0)