Skip to content

Commit

Permalink
Store intersectons in a map
Browse files Browse the repository at this point in the history
prevents possible duplication because keyed by node ID
  • Loading branch information
Nate-Wessel committed Jul 11, 2024
1 parent a2d4b95 commit 24a6830
Showing 1 changed file with 15 additions and 15 deletions.
30 changes: 15 additions & 15 deletions frontend/src/corridor.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Segment } from './segment.js'

// a sequence of segments forming a coherent corridor
export class Corridor extends Factor {
#intersections = []
#intersections = new Map() // disallows duplicates
#segments = []
constructor(dataContext){
super(dataContext)
Expand All @@ -14,8 +14,8 @@ export class Corridor extends Factor {
}
addIntersection(intersection,logActivity){
console.assert(intersection instanceof Intersection)
this.#intersections = [ ...this.#intersections, intersection ]
this.#segments = this.#intersections
this.#intersections.set(intersection.id, intersection)
this.#segments = this.intersections
.map( (int,i,ints) => {
if (i > 0){
return new Segment( {
Expand All @@ -31,7 +31,7 @@ export class Corridor extends Factor {
logActivity('shortest path returned')
} )
}
get intersections(){ return this.#intersections }
get intersections(){ return [...this.#intersections.values()] }
addSegment(segment){
if(segment instanceof Segment){
this.#segments.push(segment)
Expand All @@ -46,34 +46,34 @@ export class Corridor extends Factor {
return [...this.viaStreets].join(' & ')
}
get startCrossStreets(){
try { return difference(this.#intersections[0].streetNames,this.viaStreets) }
try { return difference(this.intersections[0].streetNames,this.viaStreets) }
catch (e) { return new Set() }
}
get startCrossStreetsString(){
if(this.startCrossStreets.size > 0){
return [...this.startCrossStreets].join(' & ')
}else if(this.#intersections.length > 0){
return this.#intersections[0].displayCoords
}else if(this.intersections.length > 0){
return this.intersections[0].displayCoords
}
return ''
}
get endCrossStreets(){
try { return difference(this.#intersections[1].streetNames,this.viaStreets) }
try { return difference(this.intersections[1].streetNames,this.viaStreets) }
catch (e) { return new Set() }
}
get endCrossStreetsString(){
if(this.endCrossStreets.size > 0){
return [...this.endCrossStreets].join(' & ')
}else if(this.#intersections.length > 1){
return this.#intersections[1].displayCoords
}else if(this.intersections.length > 1){
return this.intersections[1].displayCoords
}
return ''
}
get bearing(){
// azimuth calculation borrowed from:
// http://www.movable-type.co.uk/scripts/latlong.html
if( ! this.#intersections.length == 2 ) return undefined;
const [A, B] = this.#intersections
if( ! this.intersections.length == 2 ) return undefined;
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))
Expand All @@ -87,11 +87,11 @@ export class Corridor extends Factor {
return ''
}
get name(){
if(this.#intersections.length == 1){
if(this.intersections.length == 1){
return `Incomplete corridor starting from ${this.startCrossStreetsString}`
}else if(this.#intersections.length == 2 && this.viaStreets.size > 0){
}else if(this.intersections.length == 2 && this.viaStreets.size > 0){
return `${this.viaStreetsString} ${this.bearing.toLowerCase()} from ${this.startCrossStreetsString} to ${this.endCrossStreetsString}`
}else if(this.#intersections.length == 2){ // but no via streets (yet?)
}else if(this.intersections.length == 2){ // but no via streets (yet?)
return `${this.bearing.toLowerCase()} from ${this.startCrossStreetsString} to ${this.endCrossStreetsString}`
}
return 'New Corridor'
Expand Down

0 comments on commit 24a6830

Please sign in to comment.