Files
sorsfonalak/services/frontend/src/lib/transits.ts
T

99 lines
3.3 KiB
TypeScript
Raw Normal View History

// Today's transits to a birth chart: the planets now against the chart's
// planets and angles. A plain calculation on data the page already has.
import type { ChartResult, SkyPlanet } from "./types";
export interface Transit {
transiting: string;
natal: string;
aspect: "conjunction" | "sextile" | "square" | "trine" | "opposition";
/** Degrees from exact. */
orb: number;
/** Getting closer (otherwise past exact and fading). */
applying: boolean;
}
const ASPECTS: [Transit["aspect"], number][] = [
["conjunction", 0],
["sextile", 60],
["square", 90],
["trine", 120],
["opposition", 180],
];
/** Orbs for a day's view: the faster the planet, the shorter it stays in one. */
const ORB: Record<string, number> = {
moon: 2,
sun: 2,
mercury: 2,
venus: 2,
mars: 2,
jupiter: 1.5,
saturn: 1.5,
uranus: 1,
neptune: 1,
pluto: 1,
};
const PLANETS = Object.keys(ORB);
/** Angular distance `[0, 180]`. */
function separation(a: number, b: number): number {
const d = Math.abs(a - b) % 360;
return d > 180 ? 360 - d : d;
}
/** The chart points measured to: the ten planets, and with a birth time the Ascendant and the MC (the Moon only then). */
function natalPoints(chart: ChartResult, timeKnown: boolean): [string, number][] {
const out: [string, number][] = chart.points
.filter((p) => PLANETS.includes(p.id) && (timeKnown || p.id !== "moon"))
.map((p) => [p.id, p.longitude]);
if (timeKnown && chart.angles) {
for (const id of ["ascendant", "midheaven"]) {
const a = chart.angles[id];
if (a && typeof a === "object") out.push([id, a.longitude]);
}
}
return out;
}
/** The tightest transits now, closest first (relative to the orb allowed). */
export function todaysTransits(sky: SkyPlanet[], chart: ChartResult, timeKnown: boolean, limit = 3): Transit[] {
const found: (Transit & { score: number })[] = [];
for (const t of sky) {
const maxOrb = ORB[t.id];
if (maxOrb === undefined) continue;
// Where it will be in about an hour: closer or farther from exact.
const later = t.position.longitude + t.speed / 24;
for (const [natal, lon] of natalPoints(chart, timeKnown)) {
for (const [aspect, angle] of ASPECTS) {
const orb = Math.abs(separation(t.position.longitude, lon) - angle);
if (orb > maxOrb) continue;
const applying = Math.abs(separation(later, lon) - angle) < orb;
found.push({ transiting: t.id, natal, aspect, orb, applying, score: orb / maxOrb });
}
}
}
// The Moon passes everything within hours: one of its aspects at most.
let moon = 0;
return found
.sort((a, b) => a.score - b.score)
.filter((t) => t.transiting !== "moon" || moon++ === 0)
.slice(0, limit)
.map(({ score: _, ...t }) => t);
}
/** The natal house a longitude falls in (1–12), or `null` without houses. */
export function natalHouse(chart: ChartResult, longitude: number): number | null {
const cusps = chart.houses?.cusps;
if (!cusps || cusps.length !== 12) return null;
const sorted = [...cusps].sort((a, b) => a.house - b.house).map((c) => c.longitude);
for (let i = 0; i < 12; i++) {
const start = sorted[i];
const end = sorted[(i + 1) % 12];
const span = (end - start + 360) % 360;
if ((longitude - start + 360) % 360 < span) return i + 1;
}
return null;
}