Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/watchOS.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ jobs:
run: |
xcodebuild test \
-sdk watchsimulator \
-destination 'platform=watchOS Simulator,name=Apple Watch Series 10 (46mm)' \
-destination 'platform=watchOS Simulator,name=Any watchOS Simulator Device' \
-scheme RectangleTools
57 changes: 53 additions & 4 deletions Sources/RectangleTools/Synthesized Conveniences/scaled.swift
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,9 @@ public enum ScaleMethod {


/// Discard the aspect ratio, and simply set the size to be the same as the parent's
///
/// - When scaling direction is `.down`, then this might result in zero, two, or all four sides touching or passing parent sides.
/// - When scaling direction is `.upOrDown`, then all four sides will **always** touch parent sides.
case stretch
}

Expand All @@ -331,7 +334,7 @@ where Length: MultiplicativeArithmetic,
Length: Comparable,
Length: ExpressibleByIntegerLiteral
{
/// Scales this within the given parent, using the given scaling method
/// Scales this within the given parent, using the given scaling method & direction
///
/// - Parameters:
/// - parent: The parent within which to scale this
Expand Down Expand Up @@ -421,10 +424,35 @@ where Length: MultiplicativeArithmetic,
return scaleToMatchWidth()

case .stretch:
return .init(measurementX: parent.measurementX,
measurementY: parent.measurementY)
switch direction {
case .down:
return .init(measurementX: min(self.measurementX, parent.measurementX),
measurementY: min(self.measurementY, parent.measurementY))

case .upOrDown:
return .init(measurementX: parent.measurementX,
measurementY: parent.measurementY)
}
}
}


/// Scales this by the given multiplier, using the given scaling method & direction.
///
/// The edges are scaled, not the area.
///
/// - Parameters:
/// - multiplier: Scales the outer dimensions of this by this amount. So a `2x2` scaled by `0.5` becomes a `1x1`, or scaled by `3.0` becomes a `6x6`.
/// - direction: _optional_ - Which direction to scale this rectangle. Defaults to `.down`
///
/// - Returns: A scaled version of this rectangle, relative to the given parent
func scaling(dimensionsBy multiplier: Length, direction: ScaleDirection = .down) -> Self {
scaled(
within: self * multiplier,
method: .stretch,
direction: direction
)
}
}


Expand All @@ -436,7 +464,7 @@ where Length: MultiplicativeArithmetic,
Length: ExpressibleByIntegerLiteral
{

/// Scales this rectangle within the given parent, using the given scaling method
/// Scales this rectangle within the given parent, using the given scaling method & direction
///
/// - Parameters:
/// - parent: The parent rectangle within which to scale this rectangle
Expand All @@ -456,6 +484,27 @@ where Length: MultiplicativeArithmetic,
}


/// Scales this rectangle by the given multiplier, using the given scaling method & direction
///
/// The resulting rectangle's center point is the same as this one's.
///
/// The edges are scaled, not the area.
///
/// - Parameters:
/// - multiplier: Scales the outer dimensions of the rectangle by this amount. So a `2x2` rectangle scaled by `0.5` becomes a `1x1` rectangle, or scaled by `3.0` becomes a `6x6` (assuming the direction allows that).
/// - direction: _optional_ - Which direction to scale this rectangle. Defaults to `.down`
///
/// - Returns: A scaled version of this rectangle, relative to the given parent
func scaling(dimensionsBy multiplier: Length, direction: ScaleDirection = .down) -> Self {
scaled(
within: Self(origin: .zero, size: size * multiplier)
.centered(within: self),
method: .stretch,
direction: direction
)
}


/// Returns a version of this rectangle which is centered within the given other rectangle, so that their `.center` values are the same
///
/// - Returns: This rectangle, centered within the given other one
Expand Down
31 changes: 31 additions & 0 deletions Tests/RectangleToolsTests/Testing convenience functions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import RectangleTools



// MARK: - Semi-equality

infix operator &&= : AssignmentPrecedence
infix operator ≈≈ : ComparisonPrecedence

Expand Down Expand Up @@ -57,3 +59,32 @@ where Value: DualTwoDimensional,
lhs.firstDimensionPair ≈≈ rhs.firstDimensionPair
&& lhs.secondDimensionPair ≈≈ rhs.secondDimensionPair
}



// MARK: - Randomization



extension CGFloat {
static func random() -> Self {
random(in: -1024...1024)
}
}



extension CGPoint {
static func random() -> Self {
.init(x: .random(), y: .random())
}
}



// MARK: - Enumerations

enum AspectRatioExpectation {
case sameAsOriginal
case sameAsParent
}
Loading
Loading