Spaces:
Sleeping
Sleeping
| import Foundation | |
| /// See https://mathworld.wolfram.com/LambertAzimuthalEqual-AreaProjection.html | |
| struct LambertAzimuthalEqualAreaProjection: Projectable { | |
| let 位0: Float | |
| let 蠒1: Float | |
| let R: Float | |
| /* | |
| 位0 central longitude | |
| 蠒1 standard parallal | |
| radius of earth | |
| */ | |
| init(位0 位0_dec: Float, 蠒1 蠒1_dec: Float, radius: Float = 6371229) { | |
| 位0 = 位0_dec.degreesToRadians | |
| 蠒1 = 蠒1_dec.degreesToRadians | |
| R = radius | |
| } | |
| func forward(latitude: Float, longitude: Float) -> (x: Float, y: Float) { | |
| let 位 = longitude.degreesToRadians | |
| let 蠒 = latitude.degreesToRadians | |
| let k = sqrtf(2/(1 + sinf(蠒1) * sinf(蠒) + cosf(蠒1) * cosf(蠒) * cosf(位 - 位0))) | |
| let x = R * k * cosf(蠒) * sinf(位 - 位0) | |
| let y = R * k * (cosf(蠒1) * sinf(蠒) - sinf(蠒1) * cos(蠒) * cosf(位 - 位0)) | |
| return (x, y) | |
| } | |
| func inverse(x: Float, y: Float) -> (latitude: Float, longitude: Float) { | |
| let x = x / R | |
| let y = y / R | |
| let p = sqrtf(x*x + y*y) | |
| let c = 2 * asinf(0.5 * p) | |
| let 蠒 = asinf(cosf(c) * sinf(蠒1) + (y * sinf(c) * cosf(蠒1))/p) | |
| let 位 = 位0 + atanf((x * sinf(c) / (p * cosf(蠒1) * cosf(c) - y * sinf(蠒1) * sinf(c)))) | |
| return (蠒.radiansToDegrees, 位.radiansToDegrees) | |
| } | |
| } | |