Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 | 7x 516x 129x 129x 129x 129x 129x 936x 312x 312x 312x 312x 37x 164x 37x 37x 37x 164x 164x 164x 164x 164x 164x 12x 37x 148x 148x 148x 148x 148x 148x 148x 148x 37x 37x 37x 33x 148x 33x 33x 148x 148x 148x 148x 54x 33x 7x 6x 6x 37x 37x 15x 6x 2x 2x 2x 32x 2x 9x 9x 6x 2x 1x 14x 7x 14x 13x 14x 45x 44x 44x 88x 88x 81x 44x 4x 4x 4x 4x 4x 2x 2x 1x 1x 8x 5x 3x 9x 9x 8x 9x | import * as Location from "expo-location";
import { Building, BUILDINGS } from "../data/buildings";
import { Coordinate } from "../type";
import { MovementMode } from "../hooks/useLocationStore";
const EARTH_RADIUS_METERS = 6371000;
export function haversineDistance(
coord1: Coordinate,
coord2: Coordinate,
): number {
const toRad = (deg: number) => (deg * Math.PI) / 180;
const dLat = toRad(coord2.latitude - coord1.latitude);
const dLon = toRad(coord2.longitude - coord1.longitude);
const a =
Math.sin(dLat / 2) ** 2 +
Math.cos(toRad(coord1.latitude)) *
Math.cos(toRad(coord2.latitude)) *
Math.sin(dLon / 2) ** 2;
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
return EARTH_RADIUS_METERS * c;
}
export interface NearestBuildingResult {
building: Building;
distance: number;
}
type XYPoint = { x: number; y: number };
function toLocalMeters(origin: Coordinate, point: Coordinate): XYPoint {
const toRad = (deg: number) => (deg * Math.PI) / 180;
const meanLat = toRad((origin.latitude + point.latitude) / 2);
const x =
toRad(point.longitude - origin.longitude) *
EARTH_RADIUS_METERS *
Math.cos(meanLat);
const y = toRad(point.latitude - origin.latitude) * EARTH_RADIUS_METERS;
return { x, y };
}
function isPointInsidePolygon(
point: Coordinate,
polygon: Coordinate[],
): boolean {
Iif (polygon.length < 3) return false;
const localPolygon = polygon.map((vertex) => toLocalMeters(point, vertex));
let inside = false;
for (
let i = 0, j = localPolygon.length - 1;
i < localPolygon.length;
j = i++
) {
const xi = localPolygon[i].x;
const yi = localPolygon[i].y;
const xj = localPolygon[j].x;
const yj = localPolygon[j].y;
const intersects =
yi > 0 !== yj > 0 &&
0 < ((xj - xi) * -yi) / (yj - yi === 0 ? Number.EPSILON : yj - yi) + xi;
if (intersects) {
inside = !inside;
}
}
return inside;
}
function distanceToSegmentMeters(
point: XYPoint,
segmentStart: XYPoint,
segmentEnd: XYPoint,
): number {
const dx = segmentEnd.x - segmentStart.x;
const dy = segmentEnd.y - segmentStart.y;
const lengthSquared = dx * dx + dy * dy;
Iif (lengthSquared === 0) {
return Math.hypot(point.x - segmentStart.x, point.y - segmentStart.y);
}
const t = Math.max(
0,
Math.min(
1,
((point.x - segmentStart.x) * dx + (point.y - segmentStart.y) * dy) /
lengthSquared,
),
);
const projectionX = segmentStart.x + t * dx;
const projectionY = segmentStart.y + t * dy;
return Math.hypot(point.x - projectionX, point.y - projectionY);
}
function distanceToPolygonMeters(
point: Coordinate,
polygon: Coordinate[],
): number {
Iif (polygon.length === 0) return Infinity;
Iif (polygon.length === 1) return haversineDistance(point, polygon[0]);
if (isPointInsidePolygon(point, polygon)) return 0;
const localPoint = { x: 0, y: 0 };
const localPolygon = polygon.map((vertex) => toLocalMeters(point, vertex));
let minDistance = Infinity;
for (let i = 0; i < localPolygon.length; i++) {
const start = localPolygon[i];
const end = localPolygon[(i + 1) % localPolygon.length];
const distance = distanceToSegmentMeters(localPoint, start, end);
if (distance < minDistance) {
minDistance = distance;
}
}
return minDistance;
}
export function findNearestBuilding(
userLocation: Coordinate,
buildings: Building[] = BUILDINGS,
): NearestBuildingResult | null {
if (!buildings.length) return null;
let nearest: NearestBuildingResult | null = null;
for (const building of buildings) {
const distance =
building.polygon?.length > 0
? distanceToPolygonMeters(userLocation, building.polygon)
: haversineDistance(userLocation, building.marker);
if (!nearest || distance < nearest.distance) {
nearest = { building, distance };
}
}
return nearest;
}
export function findNearestBuildingOnCampus(
userLocation: Coordinate,
campus: "SGW" | "LOYOLA",
): NearestBuildingResult | null {
const sgwBuildingIds = new Set([
"FB",
"EV",
"LB",
"H",
"CL",
"MB",
"LS",
"ER",
]);
const loyolaBuildingIds = new Set([
"VL",
"HU",
"SP",
"AD",
"CC",
"SC",
"HB",
"VE",
]);
const campusBuildings = BUILDINGS.filter((b) =>
campus === "SGW" ? sgwBuildingIds.has(b.id) : loyolaBuildingIds.has(b.id),
);
return findNearestBuilding(userLocation, campusBuildings);
}
export function determineMovementMode(
speedMetersPerSecond: number,
): MovementMode {
const speedKmh = speedMetersPerSecond * 3.6;
if (speedKmh < 2) return "idle";
if (speedKmh < 8) return "walking";
if (speedKmh < 30) return "biking";
return "transit";
}
export interface LocationWatcherConfig {
accuracy: Location.Accuracy;
timeInterval: number;
distanceInterval: number;
}
export interface WatcherStrategy {
getConfig(): LocationWatcherConfig;
}
class StaticWatcherStrategy implements WatcherStrategy {
constructor(private readonly config: LocationWatcherConfig) {}
getConfig(): LocationWatcherConfig {
return this.config;
}
}
const watcherStrategies: Record<MovementMode | "navigating", WatcherStrategy> =
{
navigating: new StaticWatcherStrategy({
accuracy: Location.Accuracy.BestForNavigation,
timeInterval: 1000,
distanceInterval: 1,
}),
idle: new StaticWatcherStrategy({
accuracy: Location.Accuracy.Low,
timeInterval: 1000,
distanceInterval: 1,
}),
walking: new StaticWatcherStrategy({
accuracy: Location.Accuracy.High,
timeInterval: 1000,
distanceInterval: 1,
}),
biking: new StaticWatcherStrategy({
accuracy: Location.Accuracy.High,
timeInterval: 1000,
distanceInterval: 1,
}),
transit: new StaticWatcherStrategy({
accuracy: Location.Accuracy.Balanced,
timeInterval: 1000,
distanceInterval: 1,
}),
};
export function getWatcherStrategy(
mode: MovementMode,
isNavigating: boolean,
): WatcherStrategy {
if (isNavigating) return watcherStrategies.navigating;
return watcherStrategies[mode] ?? watcherStrategies.idle;
}
export function getWatcherConfigForMode(
mode: MovementMode,
isNavigating: boolean,
): LocationWatcherConfig {
return getWatcherStrategy(mode, isNavigating).getConfig();
}
export function getDistanceToPolyline(
userLocation: Coordinate,
polylineCoords: Coordinate[],
): number {
if (!polylineCoords.length) return Infinity;
let minDistance = Infinity;
for (const point of polylineCoords) {
const distance = haversineDistance(userLocation, point);
if (distance < minDistance) {
minDistance = distance;
}
}
return minDistance;
}
export function isOnCampus(
userLocation: Coordinate,
thresholdMeters: number = 500,
): { onCampus: boolean; campus: "SGW" | "LOYOLA" | null } {
const SGW_CENTER = { latitude: 45.4973, longitude: -73.579 };
const LOYOLA_CENTER = { latitude: 45.4582, longitude: -73.6405 };
const distToSGW = haversineDistance(userLocation, SGW_CENTER);
const distToLoyola = haversineDistance(userLocation, LOYOLA_CENTER);
if (distToSGW < thresholdMeters) {
return { onCampus: true, campus: "SGW" };
}
if (distToLoyola < thresholdMeters) {
return { onCampus: true, campus: "LOYOLA" };
}
return { onCampus: false, campus: null };
}
export function formatDistance(meters: number): string {
if (meters < 1000) {
return `${Math.round(meters)} m`;
}
return `${(meters / 1000).toFixed(1)} km`;
}
export function sumPolylineDistance(coords: Coordinate[]): number {
let total = 0;
for (let i = 1; i < coords.length; i++) {
total += haversineDistance(coords[i - 1], coords[i]);
}
return total;
}
|