diff --git a/HyperWindow/Corner.swift b/HyperWindow/Corner.swift index ddfbd8c..22c4e4d 100644 --- a/HyperWindow/Corner.swift +++ b/HyperWindow/Corner.swift @@ -21,17 +21,19 @@ enum Corner: CustomStringConvertible, Equatable { } static func corner(for position: CGPoint, in size: CGSize) -> Self { - switch (position.x / size.width, position.y / size.height) { - case let (x, y) where x < 0.5 && y < 0.5: + let x = position.x / size.width + let y = position.y / size.height + guard !x.isNaN, !y.isNaN else { return .bottomRight } + + switch (x >= 0.5, y >= 0.5) { + case (false, false): return .topLeft - case let (x, y) where x >= 0.5 && y < 0.5: + case (true, false): return .topRight - case let (x, y) where x >= 0.5 && y >= 0.5: + case (true, true): return .bottomRight - case let (x, y) where x < 0.5 && y >= 0.5: + case (false, true): return .bottomLeft - default: - return .bottomRight } } } diff --git a/HyperWindowTests/CornerTests.swift b/HyperWindowTests/CornerTests.swift new file mode 100644 index 0000000..e7a40da --- /dev/null +++ b/HyperWindowTests/CornerTests.swift @@ -0,0 +1,29 @@ +import XCTest +@testable import HyperWindow + + +final class CornerTests: XCTestCase { + func test_cornerForPositionSelectsEachQuadrant() { + let size = CGSize(width: 100, height: 100) + + XCTAssertEqual(Corner.corner(for: CGPoint(x: 25, y: 25), in: size), .topLeft) + XCTAssertEqual(Corner.corner(for: CGPoint(x: 75, y: 25), in: size), .topRight) + XCTAssertEqual(Corner.corner(for: CGPoint(x: 75, y: 75), in: size), .bottomRight) + XCTAssertEqual(Corner.corner(for: CGPoint(x: 25, y: 75), in: size), .bottomLeft) + } + + func test_cornerMidpointsPreferRightAndBottom() { + let size = CGSize(width: 100, height: 100) + + XCTAssertEqual(Corner.corner(for: CGPoint(x: 50, y: 25), in: size), .topRight) + XCTAssertEqual(Corner.corner(for: CGPoint(x: 25, y: 50), in: size), .bottomLeft) + XCTAssertEqual(Corner.corner(for: CGPoint(x: 50, y: 50), in: size), .bottomRight) + } + + func test_cornerForNaNPositionDefaultsToBottomRight() { + XCTAssertEqual( + Corner.corner(for: CGPoint(x: CGFloat.nan, y: 25), in: CGSize(width: 100, height: 100)), + .bottomRight + ) + } +}