Route between stars, through the crossings a chosen range allows
The map could say where a star is and what is near it, and nothing about getting from one to another. This adds the question and the answer: pick a departure and a destination, choose how far a single crossing may be, and get the chain — how many jumps, how far in total, and every star on the way, each one a step you can fly to. A jump link is not a feature of space. There are no corridors out there; a link is a question asked of the catalogue, which is why the range is the user's control rather than a constant. Two facts about that catalogue decide what the answers look like, and both are stated in the code because they read as defects otherwise. It is magnitude-limited, so it is dense around the Sun and thins with distance — within 50 pc a 3 pc range links 99% of it into one piece, while over the whole 250 pc reach the same range leaves most stars alone. And a gap in it is a gap in what has been catalogued, not in what is there. That is why "no route" is not the end of the answer. Where no chain exists at the range asked for, the panel says which range would open one — the chain whose longest hop is as short as possible, found by the same search with the cost of arriving somewhere being the worst hop taken rather than the sum — and offers that number as a control to accept. Departure defaults to wherever the view already is, so one field is usually enough. Sol to Vega at 3 pc: four jumps, 10 pc, by way of Barnard's Star, Struve 2398 B and HD 155876. Narrow it to 0.8 pc and it says 2.26 would reach. The graph is drawn as one buffer of line segments and the route as a second, brighter one over it, with the graph stepping back while a route is up: near the Sun the links are a haze, and a thread through a bright cloud is not a thread. Both fade out with the local layer, since from outside the Galaxy the graph is a smear. Two measurements shaped this. Asking the index for each star's neighbours in turn — sixty-eight thousand sorted lists, thrown away — took eight seconds; the grid now walks its own cells once and pairs them, which takes a quarter of one. And the range control emits per pixel dragged, so the rebuild waits for the hand to settle. Three defects fixed on the way, all older than the routing: hud-acquire animated with fill-mode `both`, which leaves its closing keyframe applied for good — and that keyframe carries a clip-path. Every panel wearing it has been clipping its own box ever since, so anything that had to escape one was cut away and could not even be clicked. Nothing had needed to escape until this panel's dropdown opened upward. The routing fields returned nothing when typed into before the catalogue finished loading, and stayed nothing until the next keystroke. The options are derived from the query and the index together now, so they appear when the second of the two arrives, whichever that is. And a link was `3-7` walking one way and `7-3` walking the other, which is two links to anything comparing them. Verified: build clean, 571/571 unit, 9/9 end-to-end including two new specs — one plotting Sol to Sirius, one narrowing the range until there is no route and accepting the one it names — design detector clean, screenshots at 1440x900 and 390x844. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016jxMkwA2rbicdGxHosecYi
This commit is contained in:
@@ -0,0 +1,170 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
|
||||
import { collectJumpLinks, minimumRangeBetween, routeBetween } from './jump-links';
|
||||
import { StarNeighbourhood, StarPoint } from './star-neighbourhood';
|
||||
|
||||
/** Stars a parsec apart along x, so a chain's length is the number of hops it takes. */
|
||||
function chain(count: number): StarNeighbourhood {
|
||||
return new StarNeighbourhood(Array.from({ length: count }, (_, i) => ({ id: i, x: i, y: 0, z: 0 })));
|
||||
}
|
||||
|
||||
function index(points: StarPoint[]): StarNeighbourhood {
|
||||
return new StarNeighbourhood(points);
|
||||
}
|
||||
|
||||
describe('routeBetween', () => {
|
||||
it('walks the chain a hop at a time when that is all the range allows', () => {
|
||||
const route = routeBetween(chain(5), 0, 4, 1.5);
|
||||
|
||||
expect(route?.stars).toEqual([0, 1, 2, 3, 4]);
|
||||
expect(route?.totalPc).toBeCloseTo(4);
|
||||
expect(route?.longestHopPc).toBeCloseTo(1);
|
||||
});
|
||||
|
||||
it('goes straight there when the range reaches, however many stars lie between', () => {
|
||||
// The direct crossing is never longer than a chain through anything — Euclid says so — so a
|
||||
// range that covers it makes it the answer, and the stars in between are just scenery.
|
||||
const route = routeBetween(chain(5), 0, 4, 5);
|
||||
|
||||
expect(route?.stars).toEqual([0, 4]);
|
||||
expect(route?.totalPc).toBeCloseTo(4);
|
||||
});
|
||||
|
||||
it('picks the shorter of two ways round when neither is a straight line', () => {
|
||||
// 0 to 3 is 10 pc, out of a 6 pc range. Two ways round, both inside it: through 1, barely
|
||||
// off the line, or through 2, well off it. Shorter is what "the way there" means.
|
||||
const route = routeBetween(
|
||||
index([
|
||||
{ id: 0, x: 0, y: 0, z: 0 },
|
||||
{ id: 1, x: 5, y: 0.5, z: 0 },
|
||||
{ id: 2, x: 5, y: 3, z: 0 },
|
||||
{ id: 3, x: 10, y: 0, z: 0 }
|
||||
]),
|
||||
0,
|
||||
3,
|
||||
6
|
||||
);
|
||||
|
||||
expect(route?.stars).toEqual([0, 1, 3]);
|
||||
expect(route?.totalPc).toBeCloseTo(10.05, 1);
|
||||
});
|
||||
|
||||
it('finds nothing across a gap wider than the range', () => {
|
||||
const split = index([
|
||||
{ id: 0, x: 0, y: 0, z: 0 },
|
||||
{ id: 1, x: 1, y: 0, z: 0 },
|
||||
{ id: 2, x: 20, y: 0, z: 0 }
|
||||
]);
|
||||
|
||||
expect(routeBetween(split, 0, 2, 5)).toBeNull();
|
||||
});
|
||||
|
||||
it('answers nothing for a star that is not there, or for going nowhere', () => {
|
||||
const line = chain(3);
|
||||
|
||||
expect(routeBetween(line, 0, 0, 2)).toBeNull();
|
||||
expect(routeBetween(line, 0, 99, 2)).toBeNull();
|
||||
expect(routeBetween(line, 0, 2, 0)).toBeNull();
|
||||
});
|
||||
|
||||
it('reports the longest hop, which is what the range has to cover', () => {
|
||||
const route = routeBetween(
|
||||
index([
|
||||
{ id: 0, x: 0, y: 0, z: 0 },
|
||||
{ id: 1, x: 1, y: 0, z: 0 },
|
||||
{ id: 2, x: 5, y: 0, z: 0 }
|
||||
]),
|
||||
0,
|
||||
2,
|
||||
4
|
||||
);
|
||||
|
||||
expect(route?.longestHopPc).toBeCloseTo(4);
|
||||
});
|
||||
});
|
||||
|
||||
describe('minimumRangeBetween', () => {
|
||||
it('names the shortest range that opens a way through', () => {
|
||||
// Hops of 1 and 4: no range under 4 connects them, and 4 exactly does.
|
||||
const stepped = index([
|
||||
{ id: 0, x: 0, y: 0, z: 0 },
|
||||
{ id: 1, x: 1, y: 0, z: 0 },
|
||||
{ id: 2, x: 5, y: 0, z: 0 }
|
||||
]);
|
||||
|
||||
expect(minimumRangeBetween(stepped, 0, 2, 50)).toBeCloseTo(4);
|
||||
expect(routeBetween(stepped, 0, 2, 4)).not.toBeNull();
|
||||
expect(routeBetween(stepped, 0, 2, 3.99)).toBeNull();
|
||||
});
|
||||
|
||||
it('prefers a longer way whose worst hop is shorter, since that is what the range pays for', () => {
|
||||
// Direct: one hop of 10. Round: three hops of at most 4. The range only has to cover 4.
|
||||
const both = index([
|
||||
{ id: 0, x: 0, y: 0, z: 0 },
|
||||
{ id: 1, x: 0, y: 4, z: 0 },
|
||||
{ id: 2, x: 6, y: 7, z: 0 },
|
||||
{ id: 3, x: 10, y: 0, z: 0 }
|
||||
]);
|
||||
|
||||
const needed = minimumRangeBetween(both, 0, 3, 50);
|
||||
|
||||
expect(needed).toBeLessThan(10);
|
||||
expect(routeBetween(both, 0, 3, needed!)).not.toBeNull();
|
||||
});
|
||||
|
||||
it('finds nothing when even the ceiling does not reach', () => {
|
||||
const split = index([
|
||||
{ id: 0, x: 0, y: 0, z: 0 },
|
||||
{ id: 1, x: 100, y: 0, z: 0 }
|
||||
]);
|
||||
|
||||
expect(minimumRangeBetween(split, 0, 1, 50)).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe('collectJumpLinks', () => {
|
||||
it('reports each pair once, not once from either end', () => {
|
||||
const links = collectJumpLinks(chain(4), 1.5);
|
||||
|
||||
expect(links.map((link) => [link.from, link.to])).toEqual([
|
||||
[0, 1],
|
||||
[1, 2],
|
||||
[2, 3]
|
||||
]);
|
||||
});
|
||||
|
||||
it('measures every link it reports', () => {
|
||||
const links = collectJumpLinks(chain(3), 2.5);
|
||||
|
||||
expect(links.find((link) => link.from === 0 && link.to === 2)?.distancePc).toBeCloseTo(2);
|
||||
});
|
||||
|
||||
it('draws nothing at no range', () => {
|
||||
expect(collectJumpLinks(chain(4), 0)).toEqual([]);
|
||||
});
|
||||
|
||||
it('agrees with every route it makes possible', () => {
|
||||
// The graph drawn and the graph walked have to be the same graph, or the map shows a way
|
||||
// the route cannot take.
|
||||
let seed = 11;
|
||||
const random = () => ((seed = (seed * 1103515245 + 12345) % 2147483648) / 2147483648) * 30 - 15;
|
||||
const points: StarPoint[] = Array.from({ length: 120 }, (_, id) => ({ id, x: random(), y: random(), z: random() }));
|
||||
const cloud = index(points);
|
||||
// 9 rather than 6: at 6 this cloud falls into pieces and 0 never reaches 119, which an
|
||||
// earlier version of this test hid by only checking the route it happened to find.
|
||||
const range = 9;
|
||||
|
||||
const links = collectJumpLinks(cloud, range);
|
||||
const drawn = new Set(links.map((link) => `${link.from}-${link.to}`));
|
||||
|
||||
const route = routeBetween(cloud, 0, 119, range);
|
||||
// Asserted, not guarded: a skipped body would let the two disagree unnoticed.
|
||||
expect(route).not.toBeNull();
|
||||
expect(route!.stars.length).toBeGreaterThan(2);
|
||||
for (let i = 1; i < route!.stars.length; i++) {
|
||||
const [a, b] = [route!.stars[i - 1], route!.stars[i]].sort((x, y) => x - y);
|
||||
expect(drawn.has(`${a}-${b}`)).toBe(true);
|
||||
}
|
||||
expect(links.length).toBeGreaterThan(0);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,206 @@
|
||||
/**
|
||||
* Which stars are within reach of which, and how to get from one to another through them.
|
||||
*
|
||||
* A "jump link" is nothing more than a pair of catalogued stars closer together than some
|
||||
* chosen range. It is not a feature of space — there are no corridors out there — it is a
|
||||
* question asked of the catalogue: if a crossing of at most this far can be made, which stars
|
||||
* can be strung together, and what is the shortest chain from here to there.
|
||||
*
|
||||
* Two facts about the catalogue shape everything here, and both are worth stating because the
|
||||
* answers look like defects otherwise. It is magnitude-limited, so it is dense around the Sun
|
||||
* and thins with distance: within 50 pc a 3 pc range links 99% of it into one piece, while over
|
||||
* the whole 250 pc reach the same range leaves most stars alone. And a gap in it is a gap in
|
||||
* what has been catalogued, not in what is there. So a route that cannot be found is a
|
||||
* statement about the map, and `minimumRangeBetween` exists to say which.
|
||||
*/
|
||||
|
||||
import { StarNeighbourhood } from './star-neighbourhood';
|
||||
|
||||
/** A chain of stars from one to another, each hop within the range that was asked for. */
|
||||
export interface Route {
|
||||
/** Star ids, departure first and destination last. One hop is two ids. */
|
||||
readonly stars: readonly number[];
|
||||
/** The sum of the hops, in parsecs. */
|
||||
readonly totalPc: number;
|
||||
/**
|
||||
* The longest single hop. The range has to cover this and nothing wider, so it is what a
|
||||
* reader checks a route against — and it is the figure `minimumRangeBetween` minimises.
|
||||
*/
|
||||
readonly longestHopPc: number;
|
||||
}
|
||||
|
||||
/** An unordered pair of stars within range of each other. */
|
||||
export interface JumpLink {
|
||||
readonly from: number;
|
||||
readonly to: number;
|
||||
readonly distancePc: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* A cap on how much of the catalogue one search may walk. Reached only where a route does not
|
||||
* exist and the range is wide enough to make most of the catalogue one component; a search that
|
||||
* hits it has already visited more stars than any real chain passes through.
|
||||
*/
|
||||
const MAX_VISITED = 20000;
|
||||
|
||||
/** Pops the smallest-cost entry. A linear scan: the frontier is small next to the work per node. */
|
||||
function takeCheapest<T>(frontier: Map<number, T>, costOf: (value: T) => number): [number, T] | undefined {
|
||||
let bestId: number | undefined;
|
||||
let bestValue: T | undefined;
|
||||
let bestCost = Number.POSITIVE_INFINITY;
|
||||
for (const [id, value] of frontier) {
|
||||
const cost = costOf(value);
|
||||
if (cost < bestCost) {
|
||||
bestCost = cost;
|
||||
bestId = id;
|
||||
bestValue = value;
|
||||
}
|
||||
}
|
||||
if (bestId === undefined || bestValue === undefined) {
|
||||
return undefined;
|
||||
}
|
||||
frontier.delete(bestId);
|
||||
return [bestId, bestValue];
|
||||
}
|
||||
|
||||
function rebuild(cameFrom: Map<number, number>, fromId: number, toId: number): number[] {
|
||||
const stars = [toId];
|
||||
let at = toId;
|
||||
while (at !== fromId) {
|
||||
const previous = cameFrom.get(at);
|
||||
if (previous === undefined) {
|
||||
return [];
|
||||
}
|
||||
stars.push(previous);
|
||||
at = previous;
|
||||
}
|
||||
return stars.reverse();
|
||||
}
|
||||
|
||||
/**
|
||||
* The shortest chain from one star to another in which no single hop exceeds `rangePc`, or
|
||||
* `null` where the catalogue holds no such chain.
|
||||
*
|
||||
* Shortest by total distance travelled rather than by number of hops: two chains of the same
|
||||
* length are not equally good, and the one that covers less ground is the one a reader means by
|
||||
* "the way there". Neighbours are asked for as the search reaches each star rather than built
|
||||
* into a graph first, so finding one route never costs a pass over the whole catalogue.
|
||||
*/
|
||||
export function routeBetween(index: StarNeighbourhood, fromId: number, toId: number, rangePc: number): Route | null {
|
||||
if (fromId === toId || rangePc <= 0 || !index.point(fromId) || !index.point(toId)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const best = new Map<number, number>([[fromId, 0]]);
|
||||
const cameFrom = new Map<number, number>();
|
||||
const settled = new Set<number>();
|
||||
const frontier = new Map<number, number>([[fromId, 0]]);
|
||||
|
||||
while (frontier.size > 0 && settled.size < MAX_VISITED) {
|
||||
const cheapest = takeCheapest(frontier, (cost) => cost);
|
||||
if (!cheapest) {
|
||||
break;
|
||||
}
|
||||
const [starId, costHere] = cheapest;
|
||||
if (settled.has(starId)) {
|
||||
continue;
|
||||
}
|
||||
settled.add(starId);
|
||||
|
||||
if (starId === toId) {
|
||||
const stars = rebuild(cameFrom, fromId, toId);
|
||||
return stars.length === 0 ? null : { stars, totalPc: costHere, longestHopPc: longestHop(index, stars) };
|
||||
}
|
||||
|
||||
for (const neighbour of index.within(starId, rangePc)) {
|
||||
if (settled.has(neighbour.id)) {
|
||||
continue;
|
||||
}
|
||||
const cost = costHere + neighbour.distancePc;
|
||||
if (cost < (best.get(neighbour.id) ?? Number.POSITIVE_INFINITY)) {
|
||||
best.set(neighbour.id, cost);
|
||||
cameFrom.set(neighbour.id, starId);
|
||||
frontier.set(neighbour.id, cost);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
function longestHop(index: StarNeighbourhood, stars: readonly number[]): number {
|
||||
let longest = 0;
|
||||
for (let i = 1; i < stars.length; i++) {
|
||||
const a = index.point(stars[i - 1]);
|
||||
const b = index.point(stars[i]);
|
||||
if (a && b) {
|
||||
longest = Math.max(longest, Math.hypot(b.x - a.x, b.y - a.y, b.z - a.z));
|
||||
}
|
||||
}
|
||||
return longest;
|
||||
}
|
||||
|
||||
/**
|
||||
* The shortest range at which any chain at all exists between two stars, or `null` if none does
|
||||
* within `ceilingPc`.
|
||||
*
|
||||
* This is what turns "no route" from a dead end into an answer: the range control can be told
|
||||
* what it would have to be raised to. It is the minimax path — the chain whose longest hop is as
|
||||
* short as possible — found by the same search as above, with the cost of reaching a star being
|
||||
* the longest hop taken to get there rather than the sum of them.
|
||||
*/
|
||||
export function minimumRangeBetween(index: StarNeighbourhood, fromId: number, toId: number, ceilingPc: number): number | null {
|
||||
if (fromId === toId || ceilingPc <= 0 || !index.point(fromId) || !index.point(toId)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const best = new Map<number, number>([[fromId, 0]]);
|
||||
const settled = new Set<number>();
|
||||
const frontier = new Map<number, number>([[fromId, 0]]);
|
||||
|
||||
while (frontier.size > 0 && settled.size < MAX_VISITED) {
|
||||
const cheapest = takeCheapest(frontier, (cost) => cost);
|
||||
if (!cheapest) {
|
||||
break;
|
||||
}
|
||||
const [starId, worstHopHere] = cheapest;
|
||||
if (settled.has(starId)) {
|
||||
continue;
|
||||
}
|
||||
settled.add(starId);
|
||||
|
||||
if (starId === toId) {
|
||||
return worstHopHere;
|
||||
}
|
||||
|
||||
for (const neighbour of index.within(starId, ceilingPc)) {
|
||||
if (settled.has(neighbour.id)) {
|
||||
continue;
|
||||
}
|
||||
// What this chain would need: the longest hop on it, not the distance covered by it.
|
||||
const needed = Math.max(worstHopHere, neighbour.distancePc);
|
||||
if (needed < (best.get(neighbour.id) ?? Number.POSITIVE_INFINITY)) {
|
||||
best.set(neighbour.id, needed);
|
||||
frontier.set(neighbour.id, needed);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Every link within `rangePc` in the whole catalogue, each pair once.
|
||||
*
|
||||
* For drawing the graph, which is the only thing that wants all of it: routing asks for a
|
||||
* star's neighbours as it reaches that star and never builds this.
|
||||
*/
|
||||
export function collectJumpLinks(index: StarNeighbourhood, rangePc: number): JumpLink[] {
|
||||
const links: JumpLink[] = [];
|
||||
index.forEachPairWithin(rangePc, (a, b, distancePc) => {
|
||||
// The smaller id first, always. The grid hands pairs over in whatever order it walks its
|
||||
// cells, and a link that is `3-7` here and `7-3` there is two links to anything comparing.
|
||||
links.push(a.id < b.id ? { from: a.id, to: b.id, distancePc } : { from: b.id, to: a.id, distancePc });
|
||||
});
|
||||
return links;
|
||||
}
|
||||
@@ -151,6 +151,55 @@ export class StarNeighbourhood {
|
||||
return found;
|
||||
}
|
||||
|
||||
/**
|
||||
* Visits every pair of stars within `radiusPc` of each other, once per pair.
|
||||
*
|
||||
* The same question `within` answers, asked of the whole catalogue at once — and a different
|
||||
* shape of answer, because asking it star by star is asking it twice per pair and paying for a
|
||||
* sorted list of each star's neighbours that the caller then throws away. Sixty-eight thousand
|
||||
* of those took eight seconds; walking the grid once takes a fraction of it.
|
||||
*
|
||||
* Each cell is paired with itself and with the half of its surrounding cells that lie after it
|
||||
* in the scan, which is what makes each pair come up exactly once.
|
||||
*/
|
||||
forEachPairWithin(radiusPc: number, visit: (a: StarPoint, b: StarPoint, distancePc: number) => void): void {
|
||||
if (radiusPc <= 0) {
|
||||
return;
|
||||
}
|
||||
const reach = Math.ceil(radiusPc / this.cellSizePc);
|
||||
const radiusSq = radiusPc * radiusPc;
|
||||
|
||||
for (const [key, cell] of this.cells) {
|
||||
const [ix, iy, iz] = key.split(',').map(Number);
|
||||
for (let dx = 0; dx <= reach; dx++) {
|
||||
for (let dy = dx === 0 ? 0 : -reach; dy <= reach; dy++) {
|
||||
for (let dz = dx === 0 && dy === 0 ? 0 : -reach; dz <= reach; dz++) {
|
||||
const other = dx === 0 && dy === 0 && dz === 0 ? cell : this.cells.get(cellKey(ix + dx, iy + dy, iz + dz));
|
||||
if (!other) {
|
||||
continue;
|
||||
}
|
||||
const sameCell = other === cell;
|
||||
for (let i = 0; i < cell.length; i++) {
|
||||
const a = this.points[cell[i]];
|
||||
// Within one cell, only the pairs after this one; across two, all of them — the
|
||||
// other cell is only ever visited from this side.
|
||||
for (let j = sameCell ? i + 1 : 0; j < other.length; j++) {
|
||||
const b = this.points[other[j]];
|
||||
const dxp = b.x - a.x;
|
||||
const dyp = b.y - a.y;
|
||||
const dzp = b.z - a.z;
|
||||
const distanceSq = dxp * dxp + dyp * dyp + dzp * dzp;
|
||||
if (distanceSq <= radiusSq) {
|
||||
visit(a, b, Math.sqrt(distanceSq));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private keyFor(x: number, y: number, z: number): string {
|
||||
const [ix, iy, iz] = this.cellFor(x, y, z);
|
||||
return cellKey(ix, iy, iz);
|
||||
|
||||
Reference in New Issue
Block a user