+87
@@ -0,0 +1,87 @@
|
||||
/*
|
||||
* Copyright (c) 2020. Pascal Syma <pascal@syma.dev>.
|
||||
* All rights reserved.
|
||||
*/
|
||||
|
||||
import Stop from "./Stop";
|
||||
import Coordinate from "./Coordinate";
|
||||
|
||||
export default class Line {
|
||||
readonly name: String;
|
||||
readonly stops: Stop[] = [];
|
||||
|
||||
readonly breaks: number[] = [];
|
||||
readonly stopBreaks: number[] = [];
|
||||
readonly duration: number = 0;
|
||||
readonly coords: Coordinate[];
|
||||
readonly color: string = '#000';
|
||||
|
||||
constructor(name: String, stops: string, color: string) {
|
||||
this.color = color
|
||||
this.name = name;
|
||||
let coords = stops.split("\n").map(e => {
|
||||
let a = e.split(',');
|
||||
return a.length > 2 ? new Stop(Number.parseFloat(a[0]), Number.parseFloat(a[1]), Number.parseFloat(a[2]), a[3]) : new Coordinate(Number.parseFloat(a[0]), Number.parseFloat(a[1]));
|
||||
});
|
||||
this.coords = coords;
|
||||
|
||||
let lastStop = <Stop>coords[0];
|
||||
let length = 0;
|
||||
let breaks: number[] = [];
|
||||
for (let i = 0; i < coords.length; i++) {
|
||||
if (coords[i] instanceof Stop) {
|
||||
let stop = <Stop>coords[i];
|
||||
this.stops.push(stop)
|
||||
this.stopBreaks.push(stop.minute)
|
||||
|
||||
const td = stop.minute - lastStop.minute
|
||||
if (coords[i - 1])
|
||||
length += coords[i - 1].distance(stop)
|
||||
breaks.map(e => lastStop.minute + (e / length) * td).forEach(e => this.breaks.push(e));
|
||||
|
||||
|
||||
this.breaks.push(stop.minute)
|
||||
|
||||
length = 0;
|
||||
breaks = [];
|
||||
|
||||
lastStop = stop
|
||||
} else {
|
||||
let len = coords[i - 1].distance(coords[i]);
|
||||
length += len;
|
||||
breaks.push(length)
|
||||
}
|
||||
}
|
||||
this.duration = lastStop.minute
|
||||
}
|
||||
|
||||
draw(ctx: CanvasRenderingContext2D) {
|
||||
let last: Coordinate = null;
|
||||
this.coords.forEach(c => {
|
||||
ctx.beginPath()
|
||||
ctx.textAlign = "center";
|
||||
if (c instanceof Stop) {
|
||||
let stop: Stop = c;
|
||||
ctx.arc(c.x, c.y, 5, 0, 2 * Math.PI);
|
||||
ctx.strokeStyle = this.color
|
||||
ctx.fillStyle = '#000'
|
||||
ctx.fillText(stop.name, c.x, c.y - 10);
|
||||
}
|
||||
ctx.stroke()
|
||||
ctx.closePath()
|
||||
if (last) {
|
||||
ctx.beginPath()
|
||||
ctx.moveTo(last.x, last.y)
|
||||
ctx.lineWidth = 3;
|
||||
ctx.lineTo(c.x, c.y)
|
||||
ctx.strokeStyle = this.color
|
||||
ctx.stroke()
|
||||
ctx.closePath()
|
||||
}
|
||||
last = c;
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user