Skip to content

Commit

Permalink
Changed Size2DCollection to Collection2D
Browse files Browse the repository at this point in the history
And We did it without breaking existing code! Woo!

This change makes that protocol more generic, so it can be applied to sizes, rectangles, anything which conforms.

Part of this was removing the requirement for conforming types to also conform to `Size2D`, and creating a new conformance requirement: `CartesianMeasurable`. We then changed `Size2D` to require `CartesianMeasurable`, and that enabled this broadening.
  • Loading branch information
KyNorthstar committed Nov 11, 2023
1 parent f168105 commit dcb344f
Show file tree
Hide file tree
Showing 13 changed files with 409 additions and 198 deletions.
4 changes: 2 additions & 2 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
"repositoryURL": "https://github.com/RougeWare/Swift-MultiplicativeArithmetic.git",
"state": {
"branch": null,
"revision": "38eee08151bbbefbbb422d02ff2c8da1a8b8bfe1",
"version": "1.3.0"
"revision": "c45199953ac680dfdcaefff5f8743f1126fe8a4e",
"version": "1.4.1"
}
}
]
Expand Down
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ let package = Package(
],
dependencies: [
// Dependencies declare other packages that this package depends on.
.package(url: "https://github.com/RougeWare/Swift-MultiplicativeArithmetic.git", from: "1.0.0"),
.package(url: "https://github.com/RougeWare/Swift-MultiplicativeArithmetic.git", from: "1.4.1"),
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
Expand Down
99 changes: 99 additions & 0 deletions Sources/RectangleTools/Basic Protocols/CartesianMeasurable.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
//
// CartesianMeasurable.swift
//
//
// Created by The Northstar✨ System on 2023-11-06.
//

import Foundation



/// Represents something that can be measured on a Cartesian coordinate plane
public protocol CartesianMeasurable {

/// The type to use for measurements of length (x/y, width/height, etc.).
///
/// This is typically a numeric type like `Int`, `Float64`, or `Decimal`.
associatedtype Length



/// The lowest x value when measuring this
///
/// This will always be less than or equal to ``maxX``
///
/// ```
/// 6 │
/// 5.0 maxY → 5 │ ╭─╮
/// 4 │ ╰╮╰───┬╮
/// 3 │ │ ╭╮ │╵
/// 2.0 minY → 2 │ ╰─╯╰─╯
/// 1 │
/// 0 ┼────────────
/// 0 1 2 3 4 5 6
/// ↑ ↑
/// minX maxX
/// 1.5 5.0
/// ```
var minX: Length { get }

/// The lowest y value when measuring this
///
/// This will always be less than or equal to ``maxY``
///
/// ```
/// 6 │
/// 5.0 maxY → 5 │ ╭─╮
/// 4 │ ╰╮╰───┬╮
/// 3 │ │ ╭╮ │╵
/// 2.0 minY → 2 │ ╰─╯╰─╯
/// 1 │
/// 0 ┼────────────
/// 0 1 2 3 4 5 6
/// ↑ ↑
/// minX maxX
/// 1.5 5.0
/// ```
var minY: Length { get }



/// The highest x value when measuring this
///
/// This will always be greater than or equal to ``minX``
///
/// ```
/// 6 │
/// 5.0 maxY → 5 │ ╭─╮
/// 4 │ ╰╮╰───┬╮
/// 3 │ │ ╭╮ │╵
/// 2.0 minY → 2 │ ╰─╯╰─╯
/// 1 │
/// 0 ┼────────────
/// 0 1 2 3 4 5 6
/// ↑ ↑
/// minX maxX
/// 1.5 5.0
/// ```
var maxX: Length { get }

/// The highest y value when measuring this
///
/// This will always be greater than or equal to ``minY``
///
/// ```
/// 6 │
/// 5.0 maxY → 5 │ ╭─╮
/// 4 │ ╰╮╰───┬╮
/// 3 │ │ ╭╮ │╵
/// 2.0 minY → 2 │ ╰─╯╰─╯
/// 1 │
/// 0 ┼────────────
/// 0 1 2 3 4 5 6
/// ↑ ↑
/// minX maxX
/// 1.5 5.0
/// ```
var maxY: Length { get }
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,3 @@ public extension DualTwoDimensional where Self: Rectangle {
self.init(origin: firstDimensionPair, size: secondDimensionPair)
}
}



// MARK: - Default conformances

extension BinaryIntegerRectangle: DualTwoDimensional {}
extension DecimalRectangle: DualTwoDimensional {}
extension CGRect: DualTwoDimensional {}
2 changes: 2 additions & 0 deletions Sources/RectangleTools/Basic Protocols/Point2D.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ public protocol MutablePoint2D: Point2D, MutableTwoDimensional {

// MARK: - Synthesis

// MARK: TwoDimensional

public extension Point2D {

var measurementX: Length {
Expand Down
6 changes: 5 additions & 1 deletion Sources/RectangleTools/Basic Protocols/Rectangle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ import Foundation



// MARK: - Rectangle

/// A two-dimensional rectangle
public protocol Rectangle {
public protocol Rectangle: DualTwoDimensional, CartesianMeasurable {

/// The unit in which the origin and size are defined
associatedtype Length
Expand Down Expand Up @@ -47,6 +49,8 @@ public protocol Rectangle {



// MARK: - Mutable Rectangle

/// A two-dimensional rectangle which can be mutated
public protocol MutableRectangle: Rectangle
where
Expand Down
4 changes: 3 additions & 1 deletion Sources/RectangleTools/Basic Protocols/Size2D.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import Foundation


/// A size in two dimensions
public protocol Size2D: TwoDimensional {
public protocol Size2D: TwoDimensional, CartesianMeasurable {

/// The unit in which the size is defined
associatedtype Length
Expand Down Expand Up @@ -45,6 +45,8 @@ public protocol MutableSize2D: Size2D, MutableTwoDimensional {

// MARK: - Synthesis

// MARK: General

public extension Size2D {

var measurementX: Length {
Expand Down
3 changes: 3 additions & 0 deletions Sources/RectangleTools/Basic Protocols/TwoDimensional.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ import Foundation
/// Something which can be measured using only two dimensions
public protocol TwoDimensional {

/// The type to use for measurements of length (x/y, width/height, etc.).
///
/// This is typically a numeric type like `Int`, `Float64`, or `Decimal`.
associatedtype Length


Expand Down
Loading

0 comments on commit dcb344f

Please sign in to comment.