Move couple util funcs to Station, add comments

This commit is contained in:
2018-04-14 18:22:16 -04:00
parent 7c488cf2ca
commit 61642cb70f
2 changed files with 27 additions and 23 deletions

View File

@@ -1,6 +1,6 @@
import * as tinycolor from 'tinycolor2';
import { distance } from './utils';
import { distance, randomPoint } from './utils';
let stationCount = 0;
@@ -27,6 +27,28 @@ export default class Station {
);
}
public static isPointDistant(point: PIXI.Point, stations: Station[],
minDistance: number): boolean {
for (const station of stations) {
if (distance(point, station.location) < minDistance) {
return false;
}
}
return true;
}
public static randomDistantPoint(stations: Station[], minDistance: number): PIXI.Point | null {
let tries = 100;
while (tries > 0) {
const point = randomPoint();
if (Station.isPointDistant(point, stations, minDistance)) {
return point;
}
tries -= 1;
}
return null;
}
public location: PIXI.Point;
public population: number;
public connections: Station[];