|
| 1 | +import Foundation |
| 2 | +import Swift2D |
| 3 | + |
| 4 | +/// A `Rect` contained within a `CartesianPlane` with an `origin` relative to the `cartesianOrigin` of the plane. |
| 5 | +/// |
| 6 | +/// ## Example |
| 7 | +/// |
| 8 | +/// ```swift |
| 9 | +/// let plane = CartesianPlan(size: Size(width: 100, height: 100)) |
| 10 | +/// let rect = Rect(origin: Point(x: 40, y: 40), size: Size(width: 10, height: 10)) |
| 11 | +/// let frame = Rect(origin: Point(x: -10, y: 10), size: Size(width: 10, height: 10)) |
| 12 | +/// ``` |
| 13 | +public typealias CartesianFrame = Rect |
| 14 | + |
| 15 | +public extension CartesianFrame { |
| 16 | + typealias Offset = Point |
| 17 | + |
| 18 | + /// The _x_ & _y_ offset required to reach the cartesian origin of the plane that contains this frame. |
| 19 | + /// |
| 20 | + /// The size of the plane is irrelevant. The assumption being made is that the plane is equal to or larger than the |
| 21 | + /// size of the frame. |
| 22 | + /// |
| 23 | + /// TODO: Verify this behavior, as it seems like this should be a strict inversion of the frame origin. |
| 24 | + /// |
| 25 | + /// ``` |
| 26 | + /// ┌────────────────────────▲───────────────────────┐ |
| 27 | + /// │ │ │ |
| 28 | + /// │ │ │ |
| 29 | + /// │ P───────────┼───────────┐ │ |
| 30 | + /// │ │ │ │ │ |
| 31 | + /// │ │ (x, y) │ │ │ |
| 32 | + /// │ │ │ │ │ |
| 33 | + /// ◀────────────┼───────────O───────────┼───────────▶ |
| 34 | + /// │ │ │ │ │ |
| 35 | + /// │ │ │ │ │ |
| 36 | + /// │ │ │ Frame │ │ |
| 37 | + /// │ └───────────┼───────────┘ │ |
| 38 | + /// │ │ │ |
| 39 | + /// │ │ Plane │ |
| 40 | + /// └────────────────────────▼───────────────────────┘ |
| 41 | + /// ``` |
| 42 | + var offsetToCartesianOrigin: Offset { |
| 43 | + return (x <= 0) ? Offset(x: abs(x), y: y) : Offset(x: -(x), y: y) |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +public extension CartesianFrame { |
| 48 | + /// Identifies the minimum `CartesianFrame` that contains all of the provided points. |
| 49 | + /// |
| 50 | + /// - parameter points: The `CartesianPoint`s with which to map into a frame. |
| 51 | + /// - returns: A `CartesianFrame` containing all of the points. |
| 52 | + static func make(for points: [CartesianPoint]) -> CartesianFrame { |
| 53 | + var minXMaxY = Point() |
| 54 | + var maxXMinY = Point() |
| 55 | + |
| 56 | + // TODO: Check the logic here. Is checking == 0 needed, or could it provide false results? |
| 57 | + |
| 58 | + points.forEach { (point) in |
| 59 | + if point.x < minXMaxY.x || minXMaxY.x == 0 { |
| 60 | + minXMaxY.x = point.x |
| 61 | + } |
| 62 | + |
| 63 | + if point.y > minXMaxY.y || minXMaxY.y == 0 { |
| 64 | + minXMaxY.y = point.y |
| 65 | + } |
| 66 | + |
| 67 | + if point.x > maxXMinY.x || maxXMinY.x == 0 { |
| 68 | + maxXMinY.x = point.x |
| 69 | + } |
| 70 | + |
| 71 | + if point.y < maxXMinY.y || maxXMinY.y == 0 { |
| 72 | + maxXMinY.y = point.y |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + return CartesianFrame( |
| 77 | + origin: minXMaxY, |
| 78 | + size: Size( |
| 79 | + width: abs(maxXMinY.x - minXMaxY.x), |
| 80 | + height: abs(maxXMinY.y - minXMaxY.y) |
| 81 | + ) |
| 82 | + ) |
| 83 | + } |
| 84 | + |
| 85 | + /// Identifies the minimum `CartesianFrame` that contains the provided points, accounting for any expansion needed |
| 86 | + /// when crossing an axis at a given distance (radius) from the origin. |
| 87 | + /// |
| 88 | + /// This is especially useful when determining the frame needed for a specific **chord** of a circle. |
| 89 | + /// |
| 90 | + /// ## Example |
| 91 | + /// |
| 92 | + /// Given the points (A, B) and the radius (R), a cartesian frame can be determined that encompasses all of the |
| 93 | + /// points. |
| 94 | + /// |
| 95 | + /// ``` |
| 96 | + /// ▲ |
| 97 | + /// │ |
| 98 | + /// │ |
| 99 | + /// .───┼───. |
| 100 | + /// ,' │ ┌─────┐ |
| 101 | + /// ,' │ │ A │ |
| 102 | + /// ; │ │ : │ |
| 103 | + /// │ │ │ │ │ |
| 104 | + /// ◀────┼────────┼────┼───R─┼───▶ |
| 105 | + /// : │ │ ; │ |
| 106 | + /// ╲ │ │ ╱ │ |
| 107 | + /// `. │ │,B │ |
| 108 | + /// `. │ ,└─────┘ |
| 109 | + /// `──┼──' |
| 110 | + /// │ |
| 111 | + /// │ |
| 112 | + /// ▼ |
| 113 | + /// ``` |
| 114 | + /// |
| 115 | + /// - parameter arc: The points and radius of the circle on which the chord is present. |
| 116 | + /// - parameter points: Additional points that extend the resulting frame. |
| 117 | + /// - returns: A `CartesianFrame` containing all of the points. |
| 118 | + /// - throws: GraphPointError.unhandledQuadrantTransition(_:_:) |
| 119 | + static func make(for arc: Arc, points: [CartesianPoint]) throws -> CartesianFrame { |
| 120 | + let startQuadrant = try Quadrant(degree: arc.startingDegree, clockwise: arc.clockwise) |
| 121 | + let endQuadrant = try Quadrant(degree: arc.endingDegree, clockwise: arc.clockwise) |
| 122 | + var frame = make(for: points) |
| 123 | + |
| 124 | + guard startQuadrant != endQuadrant else { |
| 125 | + return frame |
| 126 | + } |
| 127 | + |
| 128 | + switch (startQuadrant, endQuadrant) { |
| 129 | + case (.I, .IV), (.IV, .I): |
| 130 | + let maxAxis = frame.origin.x + frame.width |
| 131 | + if maxAxis < arc.radius { |
| 132 | + frame.size.width += (arc.radius - maxAxis) |
| 133 | + } |
| 134 | + case (.II, .I), (.I, .II): |
| 135 | + let maxAxis = frame.origin.y |
| 136 | + if maxAxis < arc.radius { |
| 137 | + frame.origin.y += (arc.radius - maxAxis) |
| 138 | + frame.size.height += (arc.radius - maxAxis) |
| 139 | + } |
| 140 | + case (.III, .II), (.II, .III): |
| 141 | + let maxAxis = abs(frame.origin.x) |
| 142 | + if maxAxis < arc.radius { |
| 143 | + frame.origin.x -= (arc.radius - maxAxis) |
| 144 | + frame.size.width += (arc.radius - maxAxis) |
| 145 | + } |
| 146 | + case (.IV, .III), (.III, .IV): |
| 147 | + let maxAxis = abs(frame.origin.y) + frame.size.height |
| 148 | + if maxAxis < arc.radius { |
| 149 | + frame.size.height += (arc.radius - maxAxis) |
| 150 | + } |
| 151 | + default: |
| 152 | + throw GraphPointError.unhandledQuadrantTransition(startQuadrant, endQuadrant) |
| 153 | + } |
| 154 | + |
| 155 | + return frame |
| 156 | + } |
| 157 | +} |
0 commit comments