Skip to content

Commit a124199

Browse files
committed
Fix division by zero bugs in repeat operations
- RepeatAlong: Guard against count == 0 in Range variant - RepeatAlong: Guard against count <= 1 in ClosedRange variant - RepeatAround: Change count > 0 to count > 1 for ClosedRange variants - Handle count == 1 by placing single instance at lower bound
1 parent 0322566 commit a124199

File tree

2 files changed

+28
-14
lines changed

2 files changed

+28
-14
lines changed

Sources/Cadova/Abstract Layer/Operations/Duplication/RepeatAlong.swift

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@ extension Geometry {
2424
///
2525
@GeometryBuilder<D>
2626
public func repeated(along axis: D.Axis, in range: Range<Double>, count: Int) -> D.Geometry {
27-
let step = (range.upperBound - range.lowerBound) / Double(count)
28-
for value in stride(from: range.lowerBound, to: range.upperBound, by: step) {
29-
translated(D.Vector(axis, value: value))
27+
if count > 0 {
28+
let step = (range.upperBound - range.lowerBound) / Double(count)
29+
for value in stride(from: range.lowerBound, to: range.upperBound, by: step) {
30+
translated(D.Vector(axis, value: value))
31+
}
3032
}
3133
}
3234

@@ -40,9 +42,13 @@ extension Geometry {
4042
///
4143
@GeometryBuilder<D>
4244
public func repeated(along axis: D.Axis, in range: ClosedRange<Double>, count: Int) -> D.Geometry {
43-
let step = (range.upperBound - range.lowerBound) / Double(count - 1)
44-
for value in stride(from: range.lowerBound, through: range.upperBound, by: step) {
45-
translated(D.Vector(axis, value: value))
45+
if count > 1 {
46+
let step = (range.upperBound - range.lowerBound) / Double(count - 1)
47+
for value in stride(from: range.lowerBound, through: range.upperBound, by: step) {
48+
translated(D.Vector(axis, value: value))
49+
}
50+
} else if count == 1 {
51+
translated(D.Vector(axis, value: range.lowerBound))
4652
}
4753
}
4854

@@ -102,15 +108,19 @@ extension Geometry {
102108

103109
if cyclically {
104110
let count = Int(floor(rangeLength / (boundsLength + minimumSpacing)))
105-
let step = rangeLength / Double(count)
106-
self.repeated(along: axis, step: step, count: count)
107-
.translated(D.Vector(axis, value: range.lowerBound))
111+
if count > 0 {
112+
let step = rangeLength / Double(count)
113+
self.repeated(along: axis, step: step, count: count)
114+
.translated(D.Vector(axis, value: range.lowerBound))
115+
}
108116
} else {
109117
let availableLength = rangeLength - boundsLength
110118
let count = Int(floor(availableLength / (boundsLength + minimumSpacing)))
111-
let step = availableLength / Double(count)
112-
self.repeated(along: axis, step: step, count: count + 1)
113-
.translated(D.Vector(axis, value: range.lowerBound))
119+
if count > 0 {
120+
let step = availableLength / Double(count)
121+
self.repeated(along: axis, step: step, count: count + 1)
122+
.translated(D.Vector(axis, value: range.lowerBound))
123+
}
114124
}
115125
}
116126
}

Sources/Cadova/Abstract Layer/Operations/Duplication/RepeatAround.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,13 @@ extension Geometry2D {
3939

4040
@GeometryBuilder2D
4141
public func repeated(in range: ClosedRange<Angle>, count: Int) -> any Geometry2D {
42-
if count > 0 {
42+
if count > 1 {
4343
let step = (range.upperBound - range.lowerBound) / Double(count - 1)
4444
for value in stride(from: range.lowerBound, through: range.upperBound, by: step) {
4545
rotated(value)
4646
}
47+
} else if count == 1 {
48+
rotated(range.lowerBound)
4749
}
4850
}
4951
}
@@ -89,11 +91,13 @@ extension Geometry3D {
8991

9092
@GeometryBuilder3D
9193
public func repeated(around axis: Axis3D, in range: ClosedRange<Angle>, count: Int) -> any Geometry3D {
92-
if count > 0 {
94+
if count > 1 {
9395
let step = (range.upperBound - range.lowerBound) / Double(count - 1)
9496
for value in stride(from: range.lowerBound, through: range.upperBound, by: step) {
9597
rotated(angle: value, axis: axis)
9698
}
99+
} else if count == 1 {
100+
rotated(angle: range.lowerBound, axis: axis)
97101
}
98102
}
99103
}

0 commit comments

Comments
 (0)