mirror of
https://github.com/cosinekitty/astronomy.git
synced 2026-05-19 06:17:03 -04:00
Calendar demo: added a moon quarter enumerator.
Added MoonQuarterEnumerator, which finds new moon, first quarter, full moon, and third quarter events. Changed the calendar start date to May 2021, so it is more relevant to the time I'm testing it.
This commit is contained in:
@@ -5,7 +5,15 @@
|
||||
interesting events for a calendar.
|
||||
*/
|
||||
|
||||
import { AstroTime, Observer, Body, SearchRiseSet, Seasons, SeasonInfo } from "./astronomy";
|
||||
import {
|
||||
AstroTime,
|
||||
Body,
|
||||
Observer,
|
||||
SearchMoonQuarter, NextMoonQuarter, MoonQuarter,
|
||||
SearchRiseSet,
|
||||
Seasons,
|
||||
} from "./astronomy";
|
||||
|
||||
|
||||
class AstroEvent {
|
||||
constructor(
|
||||
@@ -15,9 +23,10 @@ class AstroEvent {
|
||||
{}
|
||||
}
|
||||
|
||||
|
||||
interface AstroEventEnumerator {
|
||||
FindFirst(startTime: AstroTime): AstroEvent | null;
|
||||
FindNext(): AstroEvent | null;
|
||||
FindFirst(startTime: AstroTime): AstroEvent;
|
||||
FindNext(): AstroEvent;
|
||||
}
|
||||
|
||||
|
||||
@@ -31,14 +40,14 @@ class EventCollator implements AstroEventEnumerator {
|
||||
this.enumeratorList.push(enumerator);
|
||||
}
|
||||
|
||||
FindFirst(startTime: AstroTime): AstroEvent | null {
|
||||
FindFirst(startTime: AstroTime): AstroEvent {
|
||||
this.eventQueue = [];
|
||||
for (let enumerator of this.enumeratorList)
|
||||
this.InsertEvent(enumerator.FindFirst(startTime));
|
||||
return this.FindNext();
|
||||
}
|
||||
|
||||
FindNext(): AstroEvent | null {
|
||||
FindNext(): AstroEvent {
|
||||
if (this.eventQueue.length === 0)
|
||||
return null;
|
||||
|
||||
@@ -48,7 +57,7 @@ class EventCollator implements AstroEventEnumerator {
|
||||
return evt;
|
||||
}
|
||||
|
||||
InsertEvent(evt: AstroEvent | null): void {
|
||||
InsertEvent(evt: AstroEvent): void {
|
||||
if (evt !== null) {
|
||||
// Insert the event in time order -- after anything that happens before it.
|
||||
|
||||
@@ -68,14 +77,14 @@ class RiseSetEnumerator implements AstroEventEnumerator {
|
||||
constructor(private observer: Observer, private body: Body, private direction: number, private title: string) {
|
||||
}
|
||||
|
||||
FindFirst(startTime: AstroTime): AstroEvent | null {
|
||||
FindFirst(startTime: AstroTime): AstroEvent {
|
||||
this.nextSearchTime = SearchRiseSet(this.body, this.observer, this.direction, startTime, 366.0);
|
||||
if (this.nextSearchTime)
|
||||
return new AstroEvent(this.nextSearchTime, this.title, this);
|
||||
return null;
|
||||
}
|
||||
|
||||
FindNext(): AstroEvent | null {
|
||||
FindNext(): AstroEvent {
|
||||
if (this.nextSearchTime) {
|
||||
const startTime = this.nextSearchTime.AddDays(0.01);
|
||||
return this.FindFirst(startTime);
|
||||
@@ -93,16 +102,14 @@ class SeasonEnumerator implements AstroEventEnumerator {
|
||||
FindFirst(startTime: AstroTime): AstroEvent {
|
||||
this.year = startTime.date.getUTCFullYear();
|
||||
this.LoadYear(this.year);
|
||||
while (this.index < this.slist.length && this.slist[this.index].time.tt < startTime.tt) {
|
||||
while (this.index < this.slist.length && this.slist[this.index].time.tt < startTime.tt)
|
||||
++this.index;
|
||||
}
|
||||
return this.FindNext();
|
||||
}
|
||||
|
||||
FindNext(): AstroEvent {
|
||||
if (this.index === this.slist.length) {
|
||||
if (this.index === this.slist.length)
|
||||
this.LoadYear(++this.year);
|
||||
}
|
||||
return this.slist[this.index++];
|
||||
}
|
||||
|
||||
@@ -112,22 +119,46 @@ class SeasonEnumerator implements AstroEventEnumerator {
|
||||
new AstroEvent(seasons.mar_equinox, 'March equinox', this),
|
||||
new AstroEvent(seasons.jun_solstice, 'June solstice', this),
|
||||
new AstroEvent(seasons.sep_equinox, 'September equinox', this),
|
||||
new AstroEvent(seasons.dec_solstice, 'December solstice', this),
|
||||
new AstroEvent(seasons.dec_solstice, 'December solstice', this)
|
||||
];
|
||||
this.index = 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class MoonQuarterEnumerator implements AstroEventEnumerator {
|
||||
private mq: MoonQuarter;
|
||||
|
||||
FindFirst(startTime: AstroTime): AstroEvent {
|
||||
this.mq = SearchMoonQuarter(startTime);
|
||||
return this.MakeEvent();
|
||||
}
|
||||
|
||||
FindNext(): AstroEvent {
|
||||
this.mq = NextMoonQuarter(this.mq);
|
||||
return this.MakeEvent();
|
||||
}
|
||||
|
||||
private MakeEvent(): AstroEvent {
|
||||
return new AstroEvent(
|
||||
this.mq.time,
|
||||
['new moon', 'first quarter', 'full moon', 'third quarter'][this.mq.quarter],
|
||||
this
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function RunTest(): void {
|
||||
const startTime = new AstroTime(new Date('2022-01-01T00:00:00Z'));
|
||||
const startTime = new AstroTime(new Date('2021-05-12T00:00:00Z'));
|
||||
const observer = new Observer(28.6, -81.2, 10.0);
|
||||
const collator = new EventCollator([
|
||||
new RiseSetEnumerator(observer, Body.Sun, +1, 'sunrise'),
|
||||
new RiseSetEnumerator(observer, Body.Sun, -1, 'sunset'),
|
||||
new RiseSetEnumerator(observer, Body.Moon, +1, 'moonrise'),
|
||||
new RiseSetEnumerator(observer, Body.Moon, -1, 'moonset'),
|
||||
new SeasonEnumerator()
|
||||
new SeasonEnumerator(),
|
||||
new MoonQuarterEnumerator()
|
||||
]);
|
||||
|
||||
const stopYear = startTime.date.getUTCFullYear() + 11;
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user