From b949c38c3aaecf367b9ff3ca66649b180acb08dc Mon Sep 17 00:00:00 2001 From: Nate-Wessel Date: Tue, 26 Mar 2024 15:43:54 +0000 Subject: [PATCH] calculate rough compass bearing --- frontend/src/corridor.js | 26 +++++++++++++++++++++++--- frontend/src/intersection.js | 4 +++- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/frontend/src/corridor.js b/frontend/src/corridor.js index 8643b9e..7810864 100644 --- a/frontend/src/corridor.js +++ b/frontend/src/corridor.js @@ -69,13 +69,29 @@ export class Corridor extends Factor { } return '' } + get bearing(){ + if( ! this.#intersections.length == 2) return + const [A, B] = this.#intersections + const x = Math.cos(d2r(A.lat)) * Math.sin(d2r(B.lat)) + - Math.sin(d2r(A.lat)) * Math.cos(d2r(B.lat)) * Math.cos(d2r(B.lng - A.lng)) + const y = Math.sin(d2r(B.lng - A.lng)) * Math.cos(d2r(B.lat)) + // degrees from true East, "corrected" 17d for the city's grid rotation + const azimuth = r2d(Math.atan2(x,y)) + const compass = { NE: 45, SE: -45, SW: -135, NW: 135 } + console.log(azimuth) + if( azimuth < compass.NE && azimuth > compass.SE ) return 'East' + if( azimuth > compass.NE && azimuth < compass.NW ) return 'North' + if( azimuth < compass.SE && azimuth > compass.SW ) return 'South' + if( azimuth > compass.NW || azimuth < compass.SW ) return 'West' + return azimuth + } get name(){ if(this.#intersections.length == 1){ return `Incomplete corridor starting from ${this.startCrossStreetsString}` }else if(this.#intersections.length == 2 && this.viaStreets.size > 0){ - return `${this.viaStreetsString} from ${this.startCrossStreetsString} to ${this.endCrossStreetsString}` + return `${this.viaStreetsString} from ${this.startCrossStreetsString} to ${this.endCrossStreetsString} (${this.bearing})` }else if(this.#intersections.length == 2){ // but no via streets (yet?) - return `from ${this.startCrossStreetsString} to ${this.endCrossStreetsString}` + return `from ${this.startCrossStreetsString} to ${this.endCrossStreetsString} (${this.bearing})` } return 'New Corridor' } @@ -111,4 +127,8 @@ function difference(setA, setB) { setDiff.delete(elem) } return setDiff -} \ No newline at end of file +} + +// convert between degrees and radians +function d2r(degrees) { return degrees * (Math.PI / 180) } +function r2d(rad) { return rad / (Math.PI / 180) } \ No newline at end of file diff --git a/frontend/src/intersection.js b/frontend/src/intersection.js index c324f1d..be058a5 100644 --- a/frontend/src/intersection.js +++ b/frontend/src/intersection.js @@ -10,7 +10,9 @@ export class Intersection { this.#streetNames = new Set(streetNames) } get id(){ return this.#id } - get latlng(){ return { lat: this.#lat, lng: this.#lng } } + get lat(){ return this.#lat } + get lng(){ return this.#lng } + get latlng(){ return { lat: this.lat, lng: this.lng } } get displayCoords(){ // for display purposes only return `${this.#lng.toFixed(5)}, ${this.#lat.toFixed(5)}`