|
@@ -1,25 +1,67 @@
|
|
1
|
+import * as tinycolor from 'tinycolor2';
|
|
2
|
+
|
|
3
|
+import Direction, { getPointDirection } from './Direction';
|
1
|
4
|
import Station from './Station';
|
2
|
5
|
import { distance, randomInt, randomPoint } from './utils';
|
3
|
6
|
|
|
7
|
+const CONNECTION_RADIUS = Math.floor(Math.sqrt(
|
|
8
|
+ Math.pow(window.innerWidth, 2) + Math.pow(window.innerHeight, 2),
|
|
9
|
+) / 4);
|
|
10
|
+
|
4
|
11
|
export default class Line {
|
|
12
|
+ public static getLinesWithStation(lines: Line[], station: Station): Line[] {
|
|
13
|
+ return lines.filter(line => line.stations.indexOf(station) >= 0);
|
|
14
|
+ }
|
|
15
|
+
|
5
|
16
|
public stations: Station[];
|
|
17
|
+ public color: tinycolorInstance;
|
6
|
18
|
|
7
|
|
- constructor(stations: Station[], numStations: number) {
|
|
19
|
+ constructor(stations: Station[], start: PIXI.Point, startDirection: Direction,
|
|
20
|
+ numStations: number, color: tinycolorInstance) {
|
|
21
|
+ this.color = color;
|
8
|
22
|
this.stations = [];
|
9
|
|
- let stationsLeft = stations;
|
10
|
|
- let largest = Station.largestStation(stationsLeft);
|
11
|
|
- stationsLeft = stationsLeft.filter(s => s !== largest);
|
12
|
|
- this.stations.push(largest);
|
|
23
|
+ let stationsLeft = stations.slice();
|
|
24
|
+ let currentStation = Station.randomCloseLargeStation(stationsLeft, start, CONNECTION_RADIUS);
|
|
25
|
+ stationsLeft = stationsLeft.filter(s => s !== currentStation);
|
|
26
|
+ this.stations.push(currentStation);
|
13
|
27
|
|
|
28
|
+ let direction = startDirection;
|
14
|
29
|
while (this.stations.length < numStations) {
|
15
|
|
- largest = Station.largestStation(
|
16
|
|
- Station.stationsWithinRadius(stationsLeft, largest.location, 500),
|
|
30
|
+ const previousStation = this.stations[this.stations.length - 1];
|
|
31
|
+ let candidateStations = Station.stationsWithinRadius(
|
|
32
|
+ stationsLeft,
|
|
33
|
+ currentStation.location,
|
|
34
|
+ CONNECTION_RADIUS,
|
17
|
35
|
);
|
18
|
|
- if (largest === null) {
|
|
36
|
+
|
|
37
|
+ if (this.stations.length > 1) {
|
|
38
|
+ const secondPreviousStation = this.stations[this.stations.length - 2];
|
|
39
|
+ direction = getPointDirection(secondPreviousStation.location,
|
|
40
|
+ previousStation.location);
|
|
41
|
+ }
|
|
42
|
+
|
|
43
|
+ const straightStations = Station.stationsInDirection(
|
|
44
|
+ candidateStations, previousStation.location, direction,
|
|
45
|
+ );
|
|
46
|
+ const leftStations = Station.stationsInDirection(
|
|
47
|
+ candidateStations, previousStation.location, (direction - 1) % 7,
|
|
48
|
+ );
|
|
49
|
+ const rightStations = Station.stationsInDirection(
|
|
50
|
+ candidateStations, previousStation.location, (direction + 1) % 7,
|
|
51
|
+ );
|
|
52
|
+ candidateStations = [
|
|
53
|
+ ...straightStations,
|
|
54
|
+ ...leftStations,
|
|
55
|
+ ...rightStations,
|
|
56
|
+ ];
|
|
57
|
+
|
|
58
|
+ currentStation = Station.randomCloseLargeStation(candidateStations, previousStation.location,
|
|
59
|
+ CONNECTION_RADIUS);
|
|
60
|
+ if (currentStation === null || currentStation === undefined) {
|
19
|
61
|
break;
|
20
|
62
|
}
|
21
|
|
- stationsLeft = stationsLeft.filter(s => s !== largest);
|
22
|
|
- this.stations.push(largest);
|
|
63
|
+ stationsLeft = stationsLeft.filter(s => s !== currentStation);
|
|
64
|
+ this.stations.push(currentStation);
|
23
|
65
|
}
|
24
|
66
|
}
|
25
|
67
|
}
|