Repository Public License
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project : ORIX Arcade9// File : Games/TrafficMayhem/TrafficGeometry.swift10// Description : Traffic Mayhem — 2D vectors, arc-length parameterised lane11// paths and the turtle-style path builder.12// Module : Games13// Repository : https://git.spboucher.ai/orix-arcade14// Created : 2026-11-2115// Last Modified : 2026-11-2116//17// =============================================================================18// AUTHOR & PROFESSIONAL AFFILIATION19// =============================================================================20//21// Author : Simon-Pierre Boucher22// Position : Professor of Finance23// Institution : Université du Québec en Outaouais (UQO)24// Department : Département des sciences administratives25// Organization : SP Boucher26//27// Website : https://spboucher.ai28// Git : https://git.spboucher.ai29//30// =============================================================================31// OFFICIAL CONTACT DIRECTORY32// =============================================================================33//34// General : info@spboucher.ai35// Professional : contact@spboucher.ai36// Direct : simon@spboucher.ai37// Research : research@spboucher.ai38// Development : dev@spboucher.ai39// Support : support@spboucher.ai40// Security/Admin : admin@spboucher.ai41// Notifications : notifications@spboucher.ai42//43// Preferred Contact: support@spboucher.ai44//45// =============================================================================46// COPYRIGHT & INTELLECTUAL PROPERTY47// =============================================================================48//49// Copyright (c) 202650// Simon-Pierre Boucher51//52// All rights reserved where applicable.53//54// This file is governed by the applicable project license,55// copyright notices, institutional agreements and relevant law.56//57// Unless expressly authorized by the applicable license or58// rights holder, copying, redistribution, sublicensing,59// publication and commercial exploitation are not permitted.60//61// Existing third-party rights and notices remain unaffected.62//63// =============================================================================64// CONFIDENTIALITY & SECURITY65// =============================================================================66//67// Classification : Public68// Distribution : Public repository; use is governed by the project license69// Security Contact : admin@spboucher.ai70//71// Access to this source does not independently grant permission72// to redistribute or disclose confidential materials.73//74// Do not embed credentials, private keys, access tokens,75// certificates or confidential personal information.76//77// =============================================================================78// LICENSE & MAINTENANCE79// =============================================================================80//81// License : Proprietary, all rights reserved (see license file)82// License File : LICENSE83// Maintainer : Simon-Pierre Boucher84// Technical Contact: dev@spboucher.ai85//86// =============================================================================87// END OF SP BOUCHER SOURCE HEADER88// =============================================================================8990import Foundation9192nonisolated struct Vec2: Equatable, Sendable {93 var x: Double94 var y: Double9596 static let zero = Vec2(x: 0, y: 0)9798 static func + (a: Vec2, b: Vec2) -> Vec2 { Vec2(x: a.x + b.x, y: a.y + b.y) }99 static func - (a: Vec2, b: Vec2) -> Vec2 { Vec2(x: a.x - b.x, y: a.y - b.y) }100 static func * (a: Vec2, k: Double) -> Vec2 { Vec2(x: a.x * k, y: a.y * k) }101102 var length: Double { (x * x + y * y).squareRoot() }103 func distance(to other: Vec2) -> Double { (self - other).length }104 func rotated(by angle: Double) -> Vec2 {105 let c = cos(angle), s = sin(angle)106 return Vec2(x: x * c - y * s, y: x * s + y * c)107 }108}109110nonisolated enum TrafficTurn: String, Sendable, Codable {111 case straight, left, right112}113114/// A drivable path: a polyline sampled finely enough to be treated as a smooth curve, indexed by arc length `s`.115nonisolated struct TrafficPath: Sendable {116 let id: Int117 let lane: Int118 let turn: TrafficTurn119 /// Paths that leave by the same road lane share an exit lane (used for car-following after the junction).120 let exitLane: Int121 let points: [Vec2]122 let cumulative: [Double]123 /// Arc length at which a vehicle's front bumper stops (the stop line).124 let stopS: Double125 /// Arc length of the far edge of the junction; a vehicle has cleared once its rear passes it.126 let exitS: Double127128 var length: Double { cumulative[cumulative.count - 1] }129130 func point(at s: Double) -> Vec2 {131 let clamped = min(max(s, 0), length)132 var low = 0, high = cumulative.count - 1133 while high - low > 1 {134 let mid = (low + high) / 2135 if cumulative[mid] <= clamped { low = mid } else { high = mid }136 }137 let span = cumulative[high] - cumulative[low]138 let t = span > 0 ? (clamped - cumulative[low]) / span : 0139 return points[low] + (points[high] - points[low]) * t140 }141142 /// Heading in radians (0 = +x, counter-clockwise), smoothed over a short chord so sprites turn without stepping.143 func heading(at s: Double) -> Double {144 let a = point(at: s - 5), b = point(at: s + 5)145 return atan2(b.y - a.y, b.x - a.x)146 }147148 /// Points from `from` to `to`, for drawing previews.149 func polyline(from: Double, to: Double, step: Double = 6) -> [Vec2] {150 var result: [Vec2] = []151 var s = max(0, from)152 let end = min(to, length)153 while s < end {154 result.append(point(at: s))155 s += step156 }157 result.append(point(at: end))158 return result159 }160}161162/// Builds a path like a turtle: go forward, turn along an arc, mark the stop line and the junction exit.163nonisolated final class PathBuilder {164 private(set) var position: Vec2165 private(set) var heading: Double166 private var points: [Vec2]167 private var cumulative: [Double] = [0]168 private var stopS = 0.0169 private var exitS = 0.0170171 init(start: Vec2, headingDegrees: Double) {172 position = start173 heading = headingDegrees * .pi / 180174 points = [start]175 }176177 private func append(_ p: Vec2) {178 cumulative.append(cumulative[cumulative.count - 1] + p.distance(to: points[points.count - 1]))179 points.append(p)180 position = p181 }182183 @discardableResult184 func forward(_ distance: Double) -> PathBuilder {185 let steps = max(1, Int((distance / 8).rounded(.up)))186 let origin = position187 let dir = Vec2(x: cos(heading), y: sin(heading))188 for i in 1...steps { append(origin + dir * (distance * Double(i) / Double(steps))) }189 return self190 }191192 @discardableResult193 func turn(_ direction: TrafficTurn, radius: Double, degrees: Double = 90) -> PathBuilder {194 guard direction != .straight else { return self }195 let sign = direction == .left ? 1.0 : -1.0196 let sweep = degrees * .pi / 180197 let normal = Vec2(x: -sin(heading), y: cos(heading)) * sign198 let center = position + normal * radius199 let startVector = position - center200 let steps = max(6, Int((radius * sweep / 2.5).rounded(.up)))201 for i in 1...steps {202 append(center + startVector.rotated(by: sign * sweep * Double(i) / Double(steps)))203 }204 heading += sign * sweep205 return self206 }207208 @discardableResult209 func markStop() -> PathBuilder { stopS = cumulative[cumulative.count - 1]; return self }210211 @discardableResult212 func markExit() -> PathBuilder { exitS = cumulative[cumulative.count - 1]; return self }213214 func build(id: Int, lane: Int, turn: TrafficTurn, exitLane: Int) -> TrafficPath {215 TrafficPath(id: id, lane: lane, turn: turn, exitLane: exitLane, points: points, cumulative: cumulative, stopS: stopS, exitS: exitS)216 }217}218