Skip to content

Commit

Permalink
calculate rough compass bearing
Browse files Browse the repository at this point in the history
  • Loading branch information
Nate-Wessel committed Mar 26, 2024
1 parent 6476ab8 commit b949c38
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
26 changes: 23 additions & 3 deletions frontend/src/corridor.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,29 @@ export class Corridor extends Factor {
}
return ''
}
get bearing(){
if( ! this.#intersections.length == 2) return

Check failure on line 73 in frontend/src/corridor.js

View workflow job for this annotation

GitHub Actions / linting

Expected to return a value in getter 'bearing'
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'
}
Expand Down Expand Up @@ -111,4 +127,8 @@ function difference(setA, setB) {
setDiff.delete(elem)
}
return setDiff
}
}

// convert between degrees and radians
function d2r(degrees) { return degrees * (Math.PI / 180) }
function r2d(rad) { return rad / (Math.PI / 180) }
4 changes: 3 additions & 1 deletion frontend/src/intersection.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)}`
Expand Down

0 comments on commit b949c38

Please sign in to comment.