feat: 完善日月食与月掩几何链路并扩展历法接口

- 新增日月食中心带、偏食带、阴影足迹、等时线、食分线及升落边界计算,支持极区与混合食拓扑
- 新增日食单时刻阴影求解器、站心状态查询、批量采样和 ΔT 覆盖接口
- 重构恒星与行星月掩路径,补充有限盘面接触、站心修正、掩带宽度、极区投影及升落边界
- 扩展 SVG 与 GeoJSON 输出,支持详细面板、全球/极区/地球投影、边界闭合、时间标记和拓扑签名
- 扩展日月食候选搜索、局地搜索、沙罗序列预计算与范围外推,补充系列锚点和成员一致性校验
- 补齐古历纪年、儒略历独有闰日、多公历候选、历法改革跨日及精确日期运算接口
- 优化 ΔT、章动、恒星时、月球地平线、事件根搜索和本地星历缓存,降低重复计算开销并提升边界稳定
This commit is contained in:
2026-09-17 12:27:40 +08:00
parent 9ee2163cc7
commit 2bf8478639
428 changed files with 85981 additions and 7998 deletions
+507 -281
View File
@@ -9,6 +9,8 @@ import (
"time"
eclipsecore "b612.me/astro/eclipse"
"b612.me/astro/internal/geodata"
"b612.me/astro/internal/svgchart"
"b612.me/astro/internal/svgmap"
)
@@ -18,50 +20,77 @@ type solarEclipseMapLayout struct {
panelY float64
panelWidth float64
panelHeight float64
// nasa 为 true 时按 NASA 摆法排版:球面居中放大,阶段面板移到球面下方分三栏,图例再往下。
// nasa switches to the NASA composition: a centred globe, phase panels in three columns
// below it, and the legend under those.
nasa bool
legendY float64
scaleY float64
margin float64
panelGap float64
blockY float64
blockHeight float64
secondPanelY float64
// landscape 为真时用横版排布:地图在左,数据块两栏三行在右,地图下方那条放天平动与比例尺。
landscape bool
gridX float64
gridCellWidth float64
gridCellHeight float64
gridGapX float64
gridGapY float64
// 三行数据块的行高按各行行数分配:6 行块与 4 行块等分会把行距压到文字高度以下。
gridRowY [solarEclipseLandscapeRows]float64
gridRowHeights [solarEclipseLandscapeRows]float64
bottomY float64
// stripHeight 是地图下方那条里天平动盒的高度;scaleSlot 是比例尺槽,两者不重叠。
stripHeight float64
scaleSlot svgchart.LabelBox
}
type solarEclipseGlobalEventRow struct {
kind string
name string
time time.Time
point eclipsecore.SolarEclipsePathPoint
hasPoint bool
// solarEclipsePanelBlock 是一个带标题的数据块,竖版与横版共用同一批内容。
type solarEclipsePanelBlock struct {
title string
rows []svgchart.PanelRow
}
func solarEclipseMapLayoutFor(options SolarEclipseMapSVGOptions, projection svgmap.Projection) solarEclipseMapLayout {
// solarEclipsePanelBlocks 汇总日食详细版式的全部数据块,两种版式只是排布不同。
type solarEclipsePanelBlocks struct {
sun, moon solarEclipsePanelBlock
penumbra, umbra solarEclipsePanelBlock
circumstances, ephemeris solarEclipsePanelBlock
libration solarEclipsePanelBlock
}
// solarEclipseMapLayoutFor 选详细版式:按画布朝向定排布,按投影定地图长宽比。
// 横版把数据块放到地图右侧,竖版放到地图下方;两者内容完全一致。
func solarEclipseMapLayoutFor(
options SolarEclipseMapSVGOptions,
projection svgmap.Projection,
center svgmap.GeoPoint,
) solarEclipseMapLayout {
// 只给几何关系的调用方用两行图例与四行、四行、六行的数据块行数。
return solarEclipseMapLayoutForBlocks(options, projection, center, 2, solarEclipseLandscapeDefaultRowFields)
}
// solarEclipseMapLayoutForBlocks 按图例行数与数据块行数排版:两者决定横带上下限,必须先算出来。
func solarEclipseMapLayoutForBlocks(
options SolarEclipseMapSVGOptions,
projection svgmap.Projection,
center svgmap.GeoPoint,
legendRows int,
rowFields [solarEclipseLandscapeRows]float64,
) solarEclipseMapLayout {
width := float64(options.Width)
height := float64(options.Height)
margin := math.Max(26, math.Min(45, width*0.04))
gap := math.Max(16, math.Min(24, width*0.025))
panelWidth := math.Max(148, math.Min(238, width*0.22))
availableWidth := width - 2*margin - gap - panelWidth
bottomReserve := 92.0
if projection != svgmap.ProjectionEquirectangular {
bottomReserve = 118
}
availableHeight := math.Max(110, height-142-bottomReserve)
mapWidth := math.Min(availableWidth, availableHeight*2)
mapHeight := mapWidth / 2
if projection != svgmap.ProjectionEquirectangular {
mapWidth = math.Min(availableWidth, availableHeight)
mapHeight = mapWidth
}
mapY := 142 + math.Max(0, (availableHeight-mapHeight)/2)
groupWidth := mapWidth + gap + panelWidth
mapX := math.Max(margin, (width-groupWidth)/2)
return solarEclipseMapLayout{
frame: svgmap.Frame{
X: mapX, Y: mapY, Width: mapWidth, Height: mapHeight, Projection: projection,
},
panelX: mapX + mapWidth + gap,
panelY: mapY,
panelWidth: panelWidth,
panelHeight: mapHeight,
if width >= height {
return solarEclipseLandscapeMapLayout(width, height, projection, center, legendRows, rowFields)
}
return solarEclipsePortraitMapLayout(width, height, projection, center, legendRows)
}
func writeSolarEclipseMapSectionTitle(
builder *strings.Builder,
labels *svgchart.LabelTable,
layout solarEclipseMapLayout,
options SolarEclipseMapSVGOptions,
hasCentral bool,
@@ -77,154 +106,47 @@ func writeSolarEclipseMapSectionTitle(
} else {
label = "全球见食范围"
}
} else {
label = svgchart.EllipsizeText(label, layout.frame.Width-8, 14)
}
fmt.Fprintf(builder, `<text x="%.3f" y="%.3f" fill="#161a1b" font-family="Georgia, 'Times New Roman', serif" font-size="14" font-weight="700">%s</text>`,
layout.frame.X, layout.frame.Y-10, html.EscapeString(label))
}
func writeSolarEclipseGlobalEventsPanel(
builder *strings.Builder,
partial eclipsecore.SolarEclipsePartialFootprintsInfo,
path eclipsecore.SolarEclipsePath,
hasCentral bool,
layout solarEclipseMapLayout,
options SolarEclipseMapSVGOptions,
) {
rows := solarEclipseGlobalEventRows(partial, path, hasCentral, options.Language)
title := options.EventsTitle
if title == "" {
if options.Language == "en" {
title = "Global phases"
} else {
title = "全球阶段"
}
anchor, x, y := "start", layout.frame.X, layout.frame.Y-10
if layout.nasa {
// 让开球面顶端的 N 标记。
anchor, x, y = "middle", layout.frame.X+layout.frame.Width/2, layout.frame.Y-28
}
fmt.Fprintf(builder, `<g class="solar-global-events">`)
fmt.Fprintf(builder, `<line x1="%.3f" y1="%.3f" x2="%.3f" y2="%.3f" stroke="#d0d3d1" stroke-width="1"/>`,
layout.panelX-10, layout.panelY, layout.panelX-10, layout.panelY+layout.panelHeight)
fmt.Fprintf(builder, `<text x="%.3f" y="%.3f" fill="#161a1b" font-family="Georgia, 'Times New Roman', serif" font-size="14" font-weight="700">%s</text>`,
layout.panelX, layout.panelY+13, html.EscapeString(title))
rowTop := layout.panelY + 27
rowHeight := math.Max(21, (layout.panelHeight-27)/float64(len(rows)))
for index, row := range rows {
y := rowTop + float64(index)*rowHeight
if index > 0 {
fmt.Fprintf(builder, `<line x1="%.3f" y1="%.3f" x2="%.3f" y2="%.3f" stroke="#e0e1df" stroke-width="0.8"/>`,
layout.panelX, y-5, layout.panelX+layout.panelWidth, y-5)
}
fmt.Fprintf(builder, `<text x="%.3f" y="%.3f" fill="#1c2528" font-family="Arial, sans-serif" font-size="11" font-weight="700">%s</text>`,
layout.panelX, y+10, html.EscapeString(row.name))
fmt.Fprintf(builder, `<text x="%.3f" y="%.3f" fill="#1c2528" font-family="Arial, sans-serif" font-size="10" text-anchor="end">%s</text>`,
layout.panelX+layout.panelWidth, y+10, html.EscapeString(row.time.In(options.Location).Format("15:04:05")))
if row.hasPoint && rowHeight >= 45 {
fmt.Fprintf(builder, `<text x="%.3f" y="%.3f" fill="#4c585a" font-family="Arial, sans-serif" font-size="10">%s</text>`,
layout.panelX, y+27, html.EscapeString(solarEclipseFormatCoordinates(row.point.Longitude, row.point.Latitude)))
}
if row.hasPoint && rowHeight >= 52 {
detail := solarEclipseGlobalEventDetail(row, partial.Eclipse, options.Language)
fmt.Fprintf(builder, `<text x="%.3f" y="%.3f" fill="#697476" font-family="Arial, sans-serif" font-size="9">%s</text>`,
layout.panelX, y+43, html.EscapeString(detail))
}
}
builder.WriteString(`</g>`)
}
func solarEclipseGlobalEventRows(
partial eclipsecore.SolarEclipsePartialFootprintsInfo,
path eclipsecore.SolarEclipsePath,
hasCentral bool,
language string,
) []solarEclipseGlobalEventRow {
info := partial.Eclipse
names := []string{"偏食始", "中心食始", "食甚", "中心食终", "偏食终"}
if language == "en" {
names = []string{"Partial begins", "Central begins", "Greatest", "Central ends", "Partial ends"}
}
rows := make([]solarEclipseGlobalEventRow, 0, 11)
appendContact := func(name string, point eclipsecore.SolarEclipsePathPoint) {
if point.Time.IsZero() {
return
}
rows = append(rows, solarEclipseGlobalEventRow{
kind: "shadow-contact", name: name, time: point.Time, point: point, hasPoint: true,
})
}
appendContact("P1 "+names[0], partial.P1)
appendContact("P2", partial.P2)
appendContact("U1", partial.U1)
appendContact("U2", partial.U2)
if hasCentral && len(path.CenterLine) > 0 {
rows = append(rows, solarEclipseGlobalEventRow{
kind: "central-start", name: names[1], time: info.CentralBeginOnEarth,
point: path.CenterLine[0], hasPoint: true,
})
}
greatest := eclipsecore.SolarEclipsePathPoint{
Time: info.GreatestEclipse, Longitude: info.GreatestLongitude,
Latitude: info.GreatestLatitude, WidthKM: info.PathWidthKM,
}
if hasCentral {
greatest = path.Greatest
}
rows = append(rows, solarEclipseGlobalEventRow{
kind: "greatest", name: names[2], time: info.GreatestEclipse,
point: greatest, hasPoint: true,
// 图框上沿是标题带:表头文字已经占位,这里只在这条带里选位置。
placement, ok := labels.Place(label, 14, []svgchart.LabelPlacement{
{X: x, Y: y, Anchor: anchor},
{X: x, Y: layout.frame.Y - 10, Anchor: anchor},
{X: layout.frame.X, Y: layout.frame.Y - 10, Anchor: "start"},
{X: layout.frame.X, Y: layout.frame.Y - 28, Anchor: "start"},
})
appendContact("U3", partial.U3)
appendContact("U4", partial.U4)
if hasCentral && len(path.CenterLine) > 0 {
rows = append(rows, solarEclipseGlobalEventRow{
kind: "central-end", name: names[3], time: info.CentralEndOnEarth,
point: path.CenterLine[len(path.CenterLine)-1], hasPoint: true,
})
if !ok {
return
}
appendContact("P3", partial.P3)
appendContact("P4 "+names[4], partial.P4)
if len(rows) == 0 || partial.P1.Time.IsZero() {
rows = append(rows, solarEclipseGlobalEventRow{kind: "partial-start", name: names[0], time: info.PartialBeginOnEarth})
}
if partial.P4.Time.IsZero() {
rows = append(rows, solarEclipseGlobalEventRow{kind: "partial-end", name: names[4], time: info.PartialEndOnEarth})
}
sort.SliceStable(rows, func(i, j int) bool { return rows[i].time.Before(rows[j].time) })
return rows
fmt.Fprintf(builder, `<text x="%.3f" y="%.3f" fill="#161a1b" font-family="Georgia, 'Times New Roman', serif" font-size="14" font-weight="700" text-anchor="%s">%s</text>`,
placement.X, placement.Y, placement.Anchor, html.EscapeString(label))
}
func solarEclipseGlobalEventDetail(
row solarEclipseGlobalEventRow,
info eclipsecore.SolarEclipseInfo,
language string,
) string {
if row.kind == "greatest" {
if info.HasCentral {
if language == "en" {
return fmt.Sprintf("Path width %.1f km", info.PathWidthKM)
}
return fmt.Sprintf("食带宽 %.1f km", info.PathWidthKM)
}
if language == "en" {
return fmt.Sprintf("Magnitude %.3f", info.Magnitude)
}
return fmt.Sprintf("食分 %.3f", info.Magnitude)
}
if language == "en" {
return fmt.Sprintf("Sun altitude %+.1f°", row.point.SunAltitude)
}
return fmt.Sprintf("太阳高度 %+.1f°", row.point.SunAltitude)
}
func writeSolarEclipseTerminator(
func writeSolarEclipseRiseSetCurves(
builder *strings.Builder,
info eclipsecore.SolarEclipseInfo,
curves []eclipsecore.SolarEclipseRiseSetCurve,
frame svgmap.Frame,
) {
terminator := svgmap.SphericalCircle(solarEclipseSubsolarPoint(info.GreatestEclipse), 90, 360)
if len(terminator) > 0 {
terminator = append(terminator, terminator[0])
for _, curve := range curves {
className := fmt.Sprintf("solar-rise-set-boundary solar-%s-%s",
html.EscapeString(string(curve.Phase)), html.EscapeString(string(curve.Direction)))
for _, segment := range curve.Segments {
points := make([]svgmap.GeoPoint, len(segment))
for index, point := range segment {
points[index] = svgmap.GeoPoint{Longitude: point.Longitude, Latitude: point.Latitude}
}
writeEclipseMapGeoLine(
builder, frame, points, className, "#d97706", 1.35, "6 4", "solar-map-clip",
eclipseMapSourceRiseSetPhaseLines,
)
}
}
writeEclipseMapGeoLine(
builder, frame, terminator, "solar-greatest-terminator", "#6f7778", 1.05, "4 3", "solar-map-clip",
)
}
func writeSolarEclipsePenumbralOutlines(
@@ -232,6 +154,7 @@ func writeSolarEclipsePenumbralOutlines(
info eclipsecore.SolarEclipsePartialFootprintsInfo,
frame svgmap.Frame,
options SolarEclipseMapSVGOptions,
labels *svgchart.LabelTable,
) {
if options.PenumbralOutlineStep <= 0 {
return
@@ -242,22 +165,27 @@ func writeSolarEclipsePenumbralOutlines(
options.Location,
info.Eclipse.GreatestEclipse,
)
labelPositions := make([][2]float64, 0, len(selected))
for _, footprint := range selected {
writeSolarEclipseFootprintBoundary(
builder, footprint, frame, "solar-penumbral-outline", "#b07a18", 0.75, "3 3",
eclipseMapSourcePenumbralOutlines,
)
if mapTimeDistance(footprint.Time, info.Eclipse.GreatestEclipse) <= info.Step/2 {
continue
}
x, y, ok := solarEclipseFootprintLabelPosition(footprint, frame)
if !ok || solarEclipseMapLabelOverlaps(x, y, labelPositions) {
if !ok {
continue
}
labelPositions = append(labelPositions, [2]float64{x, y})
labelTime := solarEclipseMapAlignedTime(footprint.Time, options.PenumbralOutlineStep, options.Location)
fmt.Fprintf(builder, `<text class="solar-penumbral-time-label" x="%.3f" y="%.3f" fill="#8b5b08" stroke="#ffffff" stroke-width="2.4" paint-order="stroke" font-family="Arial, sans-serif" font-size="8" font-weight="700" text-anchor="middle">%s</text>`,
x, y-4, html.EscapeString(labelTime.Format("15:04")))
text := labelTime.Format("15:04")
placed, ok := labels.Place(text, 8, labelCandidatesInsideBox(
svgchart.LabelCandidates(x, y-4, "middle", 11), text, 8, solarEclipseFrameBox(frame)))
if !ok {
continue
}
fmt.Fprintf(builder, `<text class="solar-penumbral-time-label" x="%.3f" y="%.3f" fill="#8b5b08" stroke="#ffffff" stroke-width="2.4" paint-order="stroke" font-family="Arial, sans-serif" font-size="8" font-weight="700" text-anchor="%s">%s</text>`,
placed.X, placed.Y, placed.Anchor, html.EscapeString(text))
}
}
@@ -269,10 +197,170 @@ func writeSolarEclipseCentralShadowOutlines(
for _, footprint := range footprints {
writeSolarEclipseFootprintBoundary(
builder, footprint, frame, "solar-central-shadow-outline", "#7b5a42", 0.8, "",
eclipseMapSourceCentralShadowOutlines,
)
}
}
func writeSolarEclipseCentralShadowSweep(
builder *strings.Builder,
footprints []eclipsecore.SolarEclipsePartialFootprint,
frame svgmap.Frame,
eclipseType eclipsecore.SolarEclipseType,
) {
if len(footprints) == 0 {
return
}
var path strings.Builder
hasOpen := false
samples := solarEclipseCentralShadowSweepSamples(footprints)
for _, footprint := range footprints {
hasOpen = hasOpen || !footprint.Closed
}
if hasOpen {
polygons, err := geodata.OpenBoundarySweep(samples)
if err == nil {
for _, polygon := range polygons {
if len(polygon) < 3 {
continue
}
for _, fragment := range svgmap.PolygonFragments(polygon, frame.Clip()) {
if len(fragment) < 3 {
continue
}
path.WriteString(`<path d="`)
appendEclipseMapPolygonPathConsistent(&path, frame, fragment)
path.WriteString(`"/>`)
}
}
}
}
if !hasOpen {
for _, footprint := range footprints {
segments := make([][]svgmap.GeoPoint, 0, len(footprint.Boundaries))
for _, source := range footprint.Boundaries {
segment := make([]svgmap.GeoPoint, len(source))
for index, point := range source {
segment[index] = svgmap.GeoPoint{Longitude: point.Longitude, Latitude: point.Latitude}
}
segments = append(segments, segment)
}
boundary := svgmap.JoinPolylineSegments(segments)
if len(boundary) > 1 && svgmap.SameGeoPoint(boundary[0], boundary[len(boundary)-1]) {
boundary = boundary[:len(boundary)-1]
}
if len(boundary) < 3 {
continue
}
for _, fragment := range svgmap.PolygonFragments(boundary, frame.Clip()) {
if len(fragment) < 3 {
continue
}
path.WriteString(`<path d="`)
appendEclipseMapPolygonPathConsistent(&path, frame, fragment)
path.WriteString(`"/>`)
}
}
}
if path.Len() == 0 {
return
}
color := solarEclipseCentralPathColor(eclipseType)
fmt.Fprintf(builder, `<g class="solar-central-shadow-sweep" data-source="%s" clip-path="url(#solar-map-clip)" fill="%s" fill-opacity="0.18" fill-rule="nonzero">%s</g>`, eclipseMapSourceSampledOpenSweep, color, path.String())
}
func solarEclipseMonotoneCentralShadowSweepPolygons(
footprints []eclipsecore.SolarEclipsePartialFootprint,
) ([][]geodata.GeoPoint, error) {
samples := solarEclipseCentralShadowSweepSamples(footprints)
polygons, err := geodata.MonotoneOpenBoundarySweep(samples)
if err != nil {
return geodata.OpenBoundarySweep(
geodata.DecimateOpenBoundarySweepSamples(samples, 24, 40),
)
}
return polygons, nil
}
func solarEclipseCentralBandInnerTransitionCaps(
footprints []eclipsecore.SolarEclipsePartialFootprint,
) [][]geodata.GeoPoint {
samples := solarEclipseCentralShadowSweepSamples(footprints)
samples = geodata.DecimateOpenBoundarySweepSamples(samples, len(samples), 40)
return geodata.OpenBoundarySweepInnerCaps(samples, 500)
}
func solarEclipseCentralShadowSweepSamples(
footprints []eclipsecore.SolarEclipsePartialFootprint,
) []geodata.OpenBoundarySweepSample {
samples := make([]geodata.OpenBoundarySweepSample, 0, len(footprints))
for _, footprint := range footprints {
boundaries := make([][]geodata.GeoPoint, 0, len(footprint.Boundaries))
for _, source := range footprint.Boundaries {
segment := make([]geodata.GeoPoint, len(source))
for index, point := range source {
segment[index] = geodata.GeoPoint{Longitude: point.Longitude, Latitude: point.Latitude}
}
boundaries = append(boundaries, segment)
}
samples = append(samples, geodata.OpenBoundarySweepSample{
Boundaries: boundaries,
Closed: footprint.Closed,
})
}
return samples
}
func writeSolarEclipseCentralBandEnvelope(
builder *strings.Builder,
segments [][]eclipsecore.SolarEclipsePathPoint,
frame svgmap.Frame,
eclipseType eclipsecore.SolarEclipseType,
hasCentral bool,
) bool {
if len(segments) == 0 {
return false
}
polygons := make([][]geodata.GeoPoint, 0, len(segments))
for _, segment := range segments {
if len(segment) < 4 {
return false
}
polygon := make([]geodata.GeoPoint, len(segment))
for index, point := range segment {
polygon[index] = geodata.GeoPoint{Longitude: point.Longitude, Latitude: point.Latitude}
}
polygons = append(polygons, polygon)
}
merged, err := geodata.UnionPolygons(polygons)
if err != nil {
return false
}
var path strings.Builder
for _, polygon := range merged {
for _, fragment := range svgmap.PolygonFragments(polygon, frame.Clip()) {
if len(fragment) < 3 {
continue
}
path.WriteString(`<path d="`)
appendEclipseMapPolygonPathConsistent(&path, frame, fragment)
path.WriteString(`"/>`)
}
}
if path.Len() == 0 {
return false
}
color := solarEclipseCentralPathColor(eclipseType)
// 非中心食的包络同样是从 CentralBandSegments 来的真实食带,只是没有中心线;
// 用更淡的填充会让人以为没画,两者共用同一透明度,仅类名不同以便区分。
class, opacity := "solar-central-shadow-sweep", 0.30
if hasCentral {
class = "central-eclipse-band"
}
fmt.Fprintf(builder, `<g class="%s" data-source="%s" clip-path="url(#solar-map-clip)" fill="%s" fill-opacity="%.2f" fill-rule="nonzero">%s</g>`, class, eclipseMapSourceBesselianEnvelope, color, opacity, path.String())
return true
}
func solarEclipseFootprintLabelPosition(
footprint eclipsecore.SolarEclipsePartialFootprint,
frame svgmap.Frame,
@@ -295,15 +383,6 @@ func solarEclipseFootprintLabelPosition(
return bestX, bestY, !math.IsInf(bestScore, 1)
}
func solarEclipseMapLabelOverlaps(x, y float64, positions [][2]float64) bool {
for _, position := range positions {
if math.Abs(x-position[0]) < 52 && math.Abs(y-position[1]) < 17 {
return true
}
}
return false
}
func solarEclipseMapAlignedTime(value time.Time, step time.Duration, location *time.Location) time.Time {
local := value.In(location)
dayStart := time.Date(local.Year(), local.Month(), local.Day(), 0, 0, 0, 0, location)
@@ -317,14 +396,14 @@ func writeSolarEclipseFootprintBoundary(
frame svgmap.Frame,
className, color string,
strokeWidth float64,
dash string,
dash, dataSource string,
) {
for _, boundary := range footprint.Boundaries {
points := make([]svgmap.GeoPoint, len(boundary))
for index, point := range boundary {
points[index] = svgmap.GeoPoint{Longitude: point.Longitude, Latitude: point.Latitude}
}
writeEclipseMapGeoLine(builder, frame, points, className, color, strokeWidth, dash, "solar-map-clip")
writeEclipseMapGeoLine(builder, frame, points, className, color, strokeWidth, dash, "solar-map-clip", dataSource)
}
}
@@ -369,49 +448,15 @@ func mapTimeDistance(a, b time.Time) time.Duration {
return value
}
func writeSolarEclipseContactMarkers(
builder *strings.Builder,
info eclipsecore.SolarEclipsePartialFootprintsInfo,
frame svgmap.Frame,
) {
type marker struct {
name string
point eclipsecore.SolarEclipsePathPoint
color string
labelDX float64
labelDY float64
textAnchor string
}
markers := []marker{
{name: "P1", point: info.P1, color: "#a52d70", labelDX: -7, labelDY: 14, textAnchor: "end"},
{name: "P2", point: info.P2, color: "#a52d70", labelDX: 7, labelDY: -7, textAnchor: "start"},
{name: "P3", point: info.P3, color: "#a52d70", labelDX: -7, labelDY: -7, textAnchor: "end"},
{name: "P4", point: info.P4, color: "#a52d70", labelDX: 7, labelDY: 14, textAnchor: "start"},
{name: "U1", point: info.U1, color: "#a12c25", labelDX: -7, labelDY: -7, textAnchor: "end"},
{name: "U2", point: info.U2, color: "#a12c25", labelDX: 7, labelDY: 14, textAnchor: "start"},
{name: "U3", point: info.U3, color: "#a12c25", labelDX: -7, labelDY: 14, textAnchor: "end"},
{name: "U4", point: info.U4, color: "#a12c25", labelDX: 7, labelDY: -7, textAnchor: "start"},
}
for _, marker := range markers {
if marker.point.Time.IsZero() {
continue
func writeSolarEclipseContactMarkers(builder *strings.Builder, contacts []solarEclipseContactLabel) {
for _, contact := range contacts {
fmt.Fprintf(builder, `<g class="solar-shadow-contact solar-contact-%s"><circle cx="%.3f" cy="%.3f" r="2.7" fill="%s" stroke="#ffffff" stroke-width="0.9"/>`,
strings.ToLower(contact.marker.name), contact.x, contact.y, contact.marker.color)
if contact.ok {
fmt.Fprintf(builder, `<text x="%.3f" y="%.3f" fill="%s" stroke="#ffffff" stroke-width="2.4" paint-order="stroke" font-family="Arial, sans-serif" font-size="8" font-weight="700" text-anchor="%s">%s</text>`,
contact.placed.X, contact.placed.Y, contact.marker.color, contact.placed.Anchor, html.EscapeString(contact.text))
}
x, y, visible := frame.Project(marker.point.Longitude, marker.point.Latitude)
if !visible {
continue
}
labelX := x + marker.labelDX
textAnchor := marker.textAnchor
if labelX < frame.X+18 {
labelX = x + 7
textAnchor = "start"
} else if labelX > frame.X+frame.Width-18 {
labelX = x - 7
textAnchor = "end"
}
fmt.Fprintf(builder, `<g class="solar-shadow-contact solar-contact-%s"><circle cx="%.3f" cy="%.3f" r="2.7" fill="%s" stroke="#ffffff" stroke-width="0.9"/><text x="%.3f" y="%.3f" fill="%s" stroke="#ffffff" stroke-width="2.4" paint-order="stroke" font-family="Arial, sans-serif" font-size="8" font-weight="700" text-anchor="%s">%s</text></g>`,
strings.ToLower(marker.name), x, y, marker.color, labelX, y+marker.labelDY, marker.color,
textAnchor, marker.name)
builder.WriteString(`</g>`)
}
}
@@ -445,23 +490,18 @@ func writeSolarEclipseAxisMarkers(
}
}
func writeSolarEclipseSubsolarMarker(
builder *strings.Builder,
info eclipsecore.SolarEclipseInfo,
frame svgmap.Frame,
language string,
) {
point := solarEclipseSubsolarPoint(info.GreatestEclipse)
x, y, visible := frame.Project(point.Longitude, point.Latitude)
if !visible {
func writeSolarEclipseSubsolarMarker(builder *strings.Builder, label solarEclipseMapPointLabel) {
if label.text == "" {
return
}
label := "日下点"
if language == "en" {
label = "Subsolar"
x, y := label.x, label.y
fmt.Fprintf(builder, `<g class="solar-subsolar-marker"><circle cx="%.3f" cy="%.3f" r="4" fill="#f4c542" stroke="#714f00" stroke-width="1"/><path d="M %.3f %.3f h 8 M %.3f %.3f v 8" fill="none" stroke="#714f00" stroke-width="1"/>`,
x, y, x-4, y, x, y-4)
if label.ok {
fmt.Fprintf(builder, `<text x="%.3f" y="%.3f" fill="#714f00" stroke="#ffffff" stroke-width="2.5" paint-order="stroke" font-family="Arial, sans-serif" font-size="7" font-weight="700" text-anchor="%s" opacity="0.72">%s</text>`,
label.placed.X, label.placed.Y, label.placed.Anchor, html.EscapeString(label.text))
}
fmt.Fprintf(builder, `<g class="solar-subsolar-marker"><circle cx="%.3f" cy="%.3f" r="4" fill="#f4c542" stroke="#714f00" stroke-width="1"/><path d="M %.3f %.3f h 8 M %.3f %.3f v 8" fill="none" stroke="#714f00" stroke-width="1"/><text x="%.3f" y="%.3f" fill="#714f00" stroke="#ffffff" stroke-width="2.5" paint-order="stroke" font-family="Arial, sans-serif" font-size="9" font-weight="700" text-anchor="middle">%s</text></g>`,
x, y, x-4, y, x, y-4, x, y-9, html.EscapeString(label))
builder.WriteString(`</g>`)
}
func solarEclipseFormatCoordinates(longitude, latitude float64) string {
@@ -476,45 +516,15 @@ func solarEclipseFormatCoordinates(longitude, latitude float64) string {
return fmt.Sprintf("%.4f°%s, %.4f°%s", math.Abs(longitude), lonSuffix, math.Abs(latitude), latSuffix)
}
func writeSolarEclipseTimeMarkers(
builder *strings.Builder,
path eclipsecore.SolarEclipsePath,
frame svgmap.Frame,
options SolarEclipseMapSVGOptions,
) {
if options.TimeLabelStep <= 0 || len(path.CenterLine) < 2 {
return
}
excluded := []time.Time{
path.Eclipse.CentralBeginOnEarth,
path.Eclipse.CentralEndOnEarth,
}
markers := solarEclipseTimeMarkerPoints(path.CenterLine, options.TimeLabelStep, options.Location, excluded)
projected := make([][2]float64, 0, len(markers))
for _, marker := range markers {
x, y, visible := frame.Project(marker.Longitude, marker.Latitude)
if !visible || x < frame.X+22 || x > frame.X+frame.Width-22 || y < frame.Y+12 || y > frame.Y+frame.Height-12 {
continue
func writeSolarEclipseTimeMarkers(builder *strings.Builder, labels []solarEclipseMapPointLabel) {
for _, label := range labels {
fmt.Fprintf(builder, `<g class="solar-time-marker"><circle cx="%.3f" cy="%.3f" r="2.3" fill="#263f58" stroke="#ffffff" stroke-width="1"/>`,
label.x, label.y)
if label.ok {
fmt.Fprintf(builder, `<text x="%.3f" y="%.3f" fill="#263f58" stroke="#ffffff" stroke-width="3" paint-order="stroke" font-family="Arial, sans-serif" font-size="9" font-weight="700" text-anchor="%s">%s</text>`,
label.placed.X, label.placed.Y, label.placed.Anchor, html.EscapeString(label.text))
}
tooClose := false
for _, previous := range projected {
if math.Hypot(x-previous[0], y-previous[1]) < 44 {
tooClose = true
break
}
}
if tooClose {
continue
}
projected = append(projected, [2]float64{x, y})
labelY := y - 8
if mapTimesNear(marker.Time, path.Eclipse.GreatestEclipse, solarEclipseGreatestTimeLabelWindow(options.TimeLabelStep)) {
labelY = y + 15
} else if labelY < frame.Y+10 {
labelY = y + 15
}
fmt.Fprintf(builder, `<g class="solar-time-marker"><circle cx="%.3f" cy="%.3f" r="2.3" fill="#263f58" stroke="#ffffff" stroke-width="1"/><text x="%.3f" y="%.3f" fill="#263f58" stroke="#ffffff" stroke-width="3" paint-order="stroke" font-family="Arial, sans-serif" font-size="9" font-weight="700" text-anchor="middle">%s</text></g>`,
x, y, x, labelY, html.EscapeString(marker.Time.In(options.Location).Format("15:04")))
builder.WriteString(`</g>`)
}
}
@@ -623,3 +633,219 @@ func mapTimesNear(a, b time.Time, window time.Duration) bool {
}
return delta <= window
}
// writeSolarEclipseGreatestTimeContours 绘制地方食甚时刻等时线及其 HH:MM 标注。
func writeSolarEclipseGreatestTimeContours(
builder *strings.Builder,
contours []eclipsecore.SolarEclipseGreatestTimeContour,
frame svgmap.Frame,
options SolarEclipseMapSVGOptions,
labels *svgchart.LabelTable,
) {
if len(contours) == 0 {
return
}
for _, contour := range contours {
for _, segment := range contour.Segments {
points := make([]svgmap.GeoPoint, len(segment))
for index, point := range segment {
points[index] = svgmap.GeoPoint{Longitude: point.Longitude, Latitude: point.Latitude}
}
writeEclipseMapGeoLine(
builder, frame, points, "solar-greatest-time-isoline", "#1f6fb2", 1.0, "", "solar-map-clip",
eclipseMapSourceGreatestTimeIsochrones,
)
}
text := contour.Time.In(options.Location).Format("15:04")
candidates := solarEclipseContourLabelCandidates(contour.Segments, frame, 0, 0, false)
placed, ok := labels.Place(text, 9, labelCandidatesInsideBox(candidates, text, 9, solarEclipseFrameBox(frame)))
if !ok {
continue
}
fmt.Fprintf(builder, `<text class="solar-greatest-time-label" x="%.3f" y="%.3f" fill="#1a5c96" stroke="#ffffff" stroke-width="2.6" paint-order="stroke" font-family="Arial, sans-serif" font-size="9" font-weight="700" text-anchor="%s">%s</text>`,
placed.X, placed.Y, placed.Anchor, html.EscapeString(text))
}
}
// writeSolarEclipseMagnitudeContours 绘制地方最大食分等值线及其数值标注。
func writeSolarEclipseMagnitudeContours(
builder *strings.Builder,
contours []eclipsecore.SolarEclipseMagnitudeContour,
frame svgmap.Frame,
labels *svgchart.LabelTable,
axis [3]float64,
normal [3]float64,
) {
if len(contours) == 0 {
return
}
for _, contour := range contours {
for _, segment := range contour.Segments {
points := make([]svgmap.GeoPoint, len(segment))
for index, point := range segment {
points[index] = svgmap.GeoPoint{Longitude: point.Longitude, Latitude: point.Latitude}
}
writeEclipseMapGeoLine(
builder, frame, points, "solar-magnitude-contour", "#7c3aed",
0.8+0.8*math.Max(0, math.Min(1, contour.Magnitude)), "4 3", "solar-map-clip",
eclipseMapSourceMagnitudeContours,
)
}
text := fmt.Sprintf("%.1f", contour.Magnitude)
x, y, hasPreferred := solarEclipseMagnitudePreferredPosition(contour.Segments, frame, axis, normal)
candidates := solarEclipseContourLabelCandidates(contour.Segments, frame, x, y, hasPreferred)
placed, ok := labels.Place(text, 10, labelCandidatesInsideBox(candidates, text, 10, solarEclipseFrameBox(frame)))
if !ok {
continue
}
fmt.Fprintf(builder, `<text class="solar-magnitude-label" x="%.3f" y="%.3f" fill="#5b21b6" stroke="#ffffff" stroke-width="3" paint-order="stroke" font-family="Arial, sans-serif" font-size="10" font-weight="700" text-anchor="%s">%s</text>`,
placed.X, placed.Y, placed.Anchor, html.EscapeString(text))
}
}
// solarEclipseMagnitudeLabelAxis 返回过食甚点、垂直于中心线的那个大圆的法线。
// 食分等值线是绕中心线的一圈闭合曲线,让它与这个固定大圆相交,各条线的标注就落在同一条线上。
func solarEclipseMagnitudeLabelAxis(path eclipsecore.SolarEclipsePath) ([3]float64, [3]float64, bool) {
points := path.CenterLine
if len(points) < 3 {
return [3]float64{}, [3]float64{}, false
}
nearest := 0
best := math.Inf(1)
for index, point := range points {
distance := math.Hypot(point.Longitude-pathsGreatestLongitude(path), point.Latitude-pathsGreatestLatitude(path))
if distance < best {
best, nearest = distance, index
}
}
if nearest == 0 {
nearest = 1
}
if nearest >= len(points)-1 {
nearest = len(points) - 2
}
before := solarEclipseUnitVector(points[nearest-1])
after := solarEclipseUnitVector(points[nearest+1])
normal := solarEclipseCross(before, after)
at := solarEclipseUnitVector(points[nearest])
tangent := solarEclipseCross(normal, at)
length := math.Sqrt(tangent[0]*tangent[0] + tangent[1]*tangent[1] + tangent[2]*tangent[2])
normalLength := math.Sqrt(normal[0]*normal[0] + normal[1]*normal[1] + normal[2]*normal[2])
if length < 1e-12 || normalLength < 1e-12 {
return [3]float64{}, [3]float64{}, false
}
return [3]float64{tangent[0] / length, tangent[1] / length, tangent[2] / length},
[3]float64{normal[0] / normalLength, normal[1] / normalLength, normal[2] / normalLength}, true
}
func pathsGreatestLongitude(path eclipsecore.SolarEclipsePath) float64 {
return path.Eclipse.GreatestLongitude
}
func pathsGreatestLatitude(path eclipsecore.SolarEclipsePath) float64 {
return path.Eclipse.GreatestLatitude
}
func solarEclipseUnitVector(point eclipsecore.SolarEclipsePathPoint) [3]float64 {
longitude := point.Longitude * math.Pi / 180
latitude := point.Latitude * math.Pi / 180
return [3]float64{
math.Cos(latitude) * math.Cos(longitude),
math.Cos(latitude) * math.Sin(longitude),
math.Sin(latitude),
}
}
func solarEclipseCross(first, second [3]float64) [3]float64 {
return [3]float64{
first[1]*second[2] - first[2]*second[1],
first[2]*second[0] - first[0]*second[2],
first[0]*second[1] - first[1]*second[0],
}
}
// solarEclipseMagnitudePreferredPosition 取等值线与标注大圆的交点,让同族标注排在同一条线上。
// 标注大圆与每条闭合等值线有两个交点,一个在中心线北侧、一个在南侧;
// 按“相对中心线的哪一侧”固定取同一侧,否则各条线会各自跳到对面,看着就很散。
func solarEclipseMagnitudePreferredPosition(
segments [][]eclipsecore.SolarEclipsePathPoint,
frame svgmap.Frame,
axis [3]float64,
normal [3]float64,
) (float64, float64, bool) {
best, bestDistance, found := eclipsecore.SolarEclipsePathPoint{}, math.Inf(1), false
bestOther, bestOtherDistance, foundOther := eclipsecore.SolarEclipsePathPoint{}, math.Inf(1), false
for _, segment := range segments {
for _, point := range segment {
vector := solarEclipseUnitVector(point)
distance := math.Abs(vector[0]*axis[0] + vector[1]*axis[1] + vector[2]*axis[2])
if vector[0]*normal[0]+vector[1]*normal[1]+vector[2]*normal[2] >= 0 {
if distance < bestDistance {
best, bestDistance, found = point, distance, true
}
continue
}
if distance < bestOtherDistance {
bestOther, bestOtherDistance, foundOther = point, distance, true
}
}
}
if !found && foundOther {
best, found = bestOther, true
}
if !found || math.IsInf(bestDistance, 1) {
return 0, 0, false
}
x, y, visible := frame.Project(best.Longitude, best.Latitude)
if !visible {
return 0, 0, false
}
return x, y - 6, true
}
// writeSolarEclipsePartialBoundary 描出偏食可见域的真实边界。
// 直接描填充路径会把 ±180° 的切边画成假线,所以走按反经线分段的折线绘制。
func writeSolarEclipsePartialBoundary(
builder *strings.Builder,
contours [][]eclipsecore.SolarEclipsePathPoint,
frame svgmap.Frame,
) {
for _, contour := range contours {
points := make([]svgmap.GeoPoint, len(contour))
for index, point := range contour {
points[index] = svgmap.GeoPoint{Longitude: point.Longitude, Latitude: point.Latitude}
}
writeEclipseMapGeoLine(builder, frame, points, "solar-partial-boundary", "#c8921a", 1.0, "", "solar-map-clip",
eclipseMapSourcePartialBandContours)
}
}
// solarEclipseDetailedBlocks 汇总日食详细版式的全部数据块。
// 竖版与横版只是排布不同,块内容必须来自这里,避免两套版式各写一遍造行逻辑。
func solarEclipseDetailedBlocks(
partial eclipsecore.SolarEclipsePartialFootprintsInfo,
local eclipsecore.LocalSolarEclipseInfo,
hasLocal bool,
geocentric eclipsecore.SolarEclipseGeocentricPanel,
hasGeocentric bool,
options SolarEclipseMapSVGOptions,
) solarEclipsePanelBlocks {
penumbra, umbra, circumstances := solarEclipseContactRows(partial, local, hasLocal, options)
penumbraTitle, circumstancesTitle, umbraTitle := solarEclipseContactTitles(options)
ephemerisTitle, librationTitle := solarEclipseEphemerisTitles(options)
ephemeris, libration := solarEclipseEphemerisRows(geocentric, options)
sunTitle, moonTitle := solarEclipseGeocentricTitles(options)
sunRows, moonRows := solarEclipseGeocentricRows(geocentric)
if !hasGeocentric {
ephemeris, libration, sunRows, moonRows = nil, nil, nil, nil
}
return solarEclipsePanelBlocks{
sun: solarEclipsePanelBlock{title: sunTitle, rows: sunRows},
moon: solarEclipsePanelBlock{title: moonTitle, rows: moonRows},
penumbra: solarEclipsePanelBlock{title: penumbraTitle, rows: penumbra},
umbra: solarEclipsePanelBlock{title: umbraTitle, rows: umbra},
circumstances: solarEclipsePanelBlock{title: circumstancesTitle, rows: circumstances},
ephemeris: solarEclipsePanelBlock{title: ephemerisTitle, rows: ephemeris},
libration: solarEclipsePanelBlock{title: librationTitle, rows: libration},
}
}