Something went wrong. Try again.
An auto tiler for macOS
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137public struct Point: Sendable, Equatable, Hashable, Codable { public var xCoordinate: Double public var yCoordinate: Double
public init(x xCoordinate: Double = 0, y yCoordinate: Double = 0) { self.xCoordinate = xCoordinate self.yCoordinate = yCoordinate }}
public struct Size: Sendable, Equatable, Codable { public var width: Double public var height: Double
public init(width: Double = 0, height: Double = 0) { self.width = width self.height = height }}
public struct Insets: Sendable, Equatable, Codable { public var top: Double public var left: Double public var bottom: Double public var right: Double
public init(top: Double = 0, left: Double = 0, bottom: Double = 0, right: Double = 0) { self.top = top self.left = left self.bottom = bottom self.right = right }
public init(_ all: Double) { self.init(top: all, left: all, bottom: all, right: all) }}
public struct Rect: Sendable, Equatable, Codable { public var origin: Point public var size: Size
public init(x xCoordinate: Double = 0, y yCoordinate: Double = 0, width: Double = 0, height: Double = 0) { origin = Point(x: xCoordinate, y: yCoordinate) size = Size(width: width, height: height) }
public init(origin: Point, size: Size) { self.origin = origin self.size = size }
public var minX: Double { origin.xCoordinate } public var minY: Double { origin.yCoordinate } public var maxX: Double { origin.xCoordinate + max(0, size.width) } public var maxY: Double { origin.yCoordinate + max(0, size.height) } public var center: Point { Point(x: (minX + maxX) / 2, y: (minY + maxY) / 2) }
public func inset(by insets: Insets) -> Rect { let left = min(max(0, insets.left), max(0, size.width)) let top = min(max(0, insets.top), max(0, size.height)) return Rect( x: minX + left, y: minY + top, width: max(0, size.width - left - max(0, insets.right)), height: max(0, size.height - top - max(0, insets.bottom)) ) }
// Round shared edges, not sizes, so neighbouring tiles stay joined. public func roundedToWholePoints() -> Rect { let horizontal = minX.rounded() let vertical = minY.rounded() return Rect(x: horizontal, y: vertical, width: maxX.rounded() - horizontal, height: maxY.rounded() - vertical) }
public func contains(_ point: Point) -> Bool { point.xCoordinate >= minX && point.xCoordinate <= maxX && point.yCoordinate >= minY && point.yCoordinate <= maxY }}
public enum Axis: String, Sendable, Equatable, Codable { case horizontal, vertical}
public enum Direction: String, Sendable, Equatable, Codable, CaseIterable { case left, right, up, down
public var axis: Axis { self == .left || self == .right ? .horizontal : .vertical }
public var isBefore: Bool { self == .left || self == .up }
public func distance(from source: Rect, to candidate: Rect) -> DirectionalDistance? { let sourceCenter = source.center let candidateCenter = candidate.center let forward: Double let perpendicular: Double let overlap: Double switch self { case .left, .right: forward = self == .left ? sourceCenter.xCoordinate - candidateCenter.xCoordinate : candidateCenter.xCoordinate - sourceCenter.xCoordinate perpendicular = abs(sourceCenter.yCoordinate - candidateCenter.yCoordinate) overlap = min(source.maxY, candidate.maxY) - max(source.minY, candidate.minY) case .up, .down: forward = self == .up ? sourceCenter.yCoordinate - candidateCenter.yCoordinate : candidateCenter.yCoordinate - sourceCenter.yCoordinate perpendicular = abs(sourceCenter.xCoordinate - candidateCenter.xCoordinate) overlap = min(source.maxX, candidate.maxX) - max(source.minX, candidate.minX) } guard forward > 0 else { return nil } return DirectionalDistance( isAligned: overlap > 0, forward: forward, perpendicular: perpendicular ) }}
public struct DirectionalDistance: Sendable, Equatable, Codable, Comparable { public let isAligned: Bool public let forward: Double public let perpendicular: Double
public static func < (lhs: Self, rhs: Self) -> Bool { if lhs.isAligned != rhs.isAligned { return lhs.isAligned } let leftSquared = lhs.forward * lhs.forward + lhs.perpendicular * lhs.perpendicular let rightSquared = rhs.forward * rhs.forward + rhs.perpendicular * rhs.perpendicular if leftSquared != rightSquared { return leftSquared < rightSquared } return lhs.perpendicular < rhs.perpendicular }}