Private/Presentation/SolarBoundary.ps1

function Get-SolarBoundaryPosition {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory)]
        [datetimeoffset]$AtUtc
    )

    $utc = $AtUtc.ToUniversalTime().UtcDateTime
    $hour = $utc.Hour + $utc.Minute / 60.0
    $declination = -23.44 * [Math]::Cos(2 * [Math]::PI / 365 * ($utc.DayOfYear + 10))
    $subSolarLongitude = - ($hour / 24.0 * 360 - 180)

    if ($subSolarLongitude -lt -180) {
        $subSolarLongitude += 360
    }
    if ($subSolarLongitude -gt 180) {
        $subSolarLongitude -= 360
    }

    [pscustomobject][ordered]@{
        declinationDegrees = $declination
        subSolarLongitudeDegrees = $subSolarLongitude
    }
}

function Add-GlobeNightOverlay {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory)]$Canvas,
        [Parameter(Mandatory)][double]$Width,
        [Parameter(Mandatory)][double]$Height,
        [Parameter(Mandatory)][double]$CenterLat,
        [Parameter(Mandatory)][double]$CenterLon,
        [Parameter(Mandatory)][double]$Declination,
        [Parameter(Mandatory)][double]$SubSolarLon
    )

    $radians = [Math]::PI / 180
    $centerLatRad = $CenterLat * $radians
    $declinationRad = $Declination * $radians
    $deltaLon = ($SubSolarLon - $CenterLon) * $radians
    $sunX = [Math]::Cos($declinationRad) * [Math]::Sin($deltaLon)
    $sunY = -([Math]::Cos($centerLatRad) * [Math]::Sin($declinationRad) -
        [Math]::Sin($centerLatRad) * [Math]::Cos($declinationRad) * [Math]::Cos($deltaLon))
    $sunZ = [Math]::Sin($centerLatRad) * [Math]::Sin($declinationRad) +
        [Math]::Cos($centerLatRad) * [Math]::Cos($declinationRad) * [Math]::Cos($deltaLon)
    $projectedSunLength = [Math]::Sqrt($sunX * $sunX + $sunY * $sunY)
    $radius = [Math]::Min($Width, $Height) / 2 * 0.92
    $cx = $Width / 2
    $cy = $Height / 2
    $nightPoints = [System.Windows.Media.PointCollection]::new()
    $converter = [System.Windows.Media.BrushConverter]::new()
    $steps = 90

    if ($projectedSunLength -lt 1e-10) {
        if ($sunZ -ge 0) { return }
        for ($i = 0; $i -lt 2 * $steps; $i++) {
            $angle = 2 * [Math]::PI * $i / (2 * $steps)
            $nightPoints.Add([System.Windows.Point]::new(
                    $cx + $radius * [Math]::Cos($angle), $cy + $radius * [Math]::Sin($angle)))
        }
    }
    else {
        # The visible terminator is the front half of the great circle perpendicular to the sun.
        $ux = -$sunY / $projectedSunLength
        $uy = $sunX / $projectedSunLength
        $vx = -$sunZ * $sunX / $projectedSunLength
        $vy = -$sunZ * $sunY / $projectedSunLength
        $linePoints = [System.Windows.Media.PointCollection]::new()
        for ($i = 0; $i -le $steps; $i++) {
            $angle = [Math]::PI * $i / $steps
            $point = [System.Windows.Point]::new(
                $cx + $radius * ($ux * [Math]::Cos($angle) + $vx * [Math]::Sin($angle)),
                $cy + $radius * ($uy * [Math]::Cos($angle) + $vy * [Math]::Sin($angle)))
            $linePoints.Add($point)
            $nightPoints.Add($point)
        }

        # Close along the limb through the side facing away from the sun.
        for ($i = 1; $i -le $steps; $i++) {
            $angle = [Math]::PI * $i / $steps
            $nightPoints.Add([System.Windows.Point]::new(
                    $cx + $radius * (-$ux * [Math]::Cos($angle) - $sunX / $projectedSunLength * [Math]::Sin($angle)),
                    $cy + $radius * (-$uy * [Math]::Cos($angle) - $sunY / $projectedSunLength * [Math]::Sin($angle))))
        }

        $line = [System.Windows.Shapes.Polyline]::new()
        $line.Stroke = $converter.ConvertFromString('#66FF8800')
        $line.StrokeThickness = 1.5
        $line.IsHitTestVisible = $false
        $line.Points = $linePoints
        $Canvas.Children.Add($line) | Out-Null
    }

    $night = [System.Windows.Shapes.Polygon]::new()
    $night.Fill = $converter.ConvertFromString('#40000000')
    $night.IsHitTestVisible = $false
    $night.Points = $nightPoints
    $Canvas.Children.Add($night) | Out-Null
}