krz/moonrise

Night-sky companion for iPhone, Apple Watch and Vision Pro: what's up tonight, the Moon, planets, launches and missions, and a Library of the solar system. SwiftUI app with a Cloudflare Workers backend. astronomy cloudflare-workers ios night-sky space swiftui visionos watchos

Packages/MoonriseKit/Sources/SkyRenderer/Pointing.swift

15 lines · 657 bytes

2 symbols in this file
 1import Foundation
 2import SkyEngine
 3
 4public enum Pointing {
 5    /// Where the back of the phone points, from the third row of a Core Motion
 6    /// rotation matrix in the x-true-north, z-vertical reference frame
 7    /// (x north, y west, z up). The camera looks along the device's −z axis.
 8    public static func direction(m31: Double, m32: Double, m33: Double) -> Horizontal {
 9        let north = -m31, west = -m32, up = -m33
10        let altitude = asin(max(-1, min(1, up))) * 180 / .pi
11        var azimuth = atan2(-west, north) * 180 / .pi
12        if azimuth < 0 { azimuth += 360 }
13        return Horizontal(altitude: altitude, azimuth: azimuth)
14    }
15}