Skip to content

Commit

Permalink
Changing Convex2Convex2Intersection to allow representing multiple in…
Browse files Browse the repository at this point in the history
…tersection points
  • Loading branch information
LuizZak committed Jun 28, 2024
1 parent 92b81a5 commit 2496056
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 12 deletions.
21 changes: 15 additions & 6 deletions Sources/Geometria/2D/Convex2Convex2Intersection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,20 @@ public enum Convex2Convex2Intersection<Vector: Vector2FloatingPoint> {
/// shape on a single vertex, or tangentially, in case of spheroids.
case singlePoint(PointNormal<Vector>)

/// Represents cases where the convex crosses the other convex shape twice.
case twoPoints(PointNormal<Vector>, PointNormal<Vector>)
/// A set of two or more intersection points between the two convexes.
///
/// The normals are in relation to one of the convexes.
case points([PointNormal<Vector>])

/// Represents the case where no intersection occurs at any point.
case noIntersection

/// Convenience for `.points([p1, p2])`.
@inlinable
public static func twoPoints(_ p1: PointNormal<Vector>, _ p2: PointNormal<Vector>) -> Self {
.points([p1, p2])
}

/// Returns a new ``Convex2Convex2Intersection`` where any ``PointNormal`` value
/// is mapped by a provided closure before being stored back into the same
/// enum case and returned.
Expand All @@ -40,8 +48,8 @@ public enum Convex2Convex2Intersection<Vector: Vector2FloatingPoint> {
case .singlePoint(let pointNormal):
return .singlePoint(mapper(pointNormal, .singlePoint))

case let .twoPoints(p1, p2):
return .twoPoints(mapper(p1, .twoPointsFirst), mapper(p2, .twoPointsSecond))
case .points(let points):
return .points(points.enumerated().map({ mapper($0.element, .points(index: $0.offset)) }))
}
}

Expand All @@ -65,8 +73,8 @@ public enum Convex2Convex2Intersection<Vector: Vector2FloatingPoint> {
case .singlePoint(let pointNormal):
return .singlePoint(mapper(pointNormal, .singlePoint))

case let .twoPoints(p1, p2):
return .twoPoints(mapper(p1, .twoPointsFirst), mapper(p2, .twoPointsSecond))
case .points(let points):
return .points(points.enumerated().map({ mapper($0.element, .points(index: $0.offset)) }))
}
}

Expand All @@ -77,6 +85,7 @@ public enum Convex2Convex2Intersection<Vector: Vector2FloatingPoint> {
case singlePoint
case twoPointsFirst
case twoPointsSecond
case points(index: Int)
}
}

Expand Down
13 changes: 10 additions & 3 deletions Sources/TestCommons/Assertions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -244,11 +244,18 @@ public func assertEqual<T: Vector2FloatingPoint>(
)
"""

case .twoPoints(let pn1, let pn2):
case .points(let points) where points.count == 2:
buffer = """
.twoPoints(
\(printPointNormal(pn1)),
\(printPointNormal(pn2))
\(printPointNormal(points[0])),
\(printPointNormal(points[1]))
)
"""

case .points(let points):
buffer = """
.points(
\(points.map(printPointNormal).joined(separator: ",\n"))
)
"""
}
Expand Down
7 changes: 4 additions & 3 deletions Sources/TestCommons/P5Printer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,10 @@ public class P5Printer {
case .singlePoint(let pn):
add(pn, style: style, file: file, line: line)

case let .twoPoints(p1, p2):
add(p1, style: style, file: file, line: line)
add(p2, style: style, file: file, line: line)
case .points(let points):
for point in points {
add(point, style: style, file: file, line: line)
}
}
}

Expand Down

0 comments on commit 2496056

Please sign in to comment.