Skip to content

Commit 94cc33c

Browse files
feat: add convertStereoToHorizontal() to projection module in @observerly/astrometry
feat: add convertStereoToHorizontal() to projection module in @observerly/astrometry
1 parent 2cfa928 commit 94cc33c

File tree

2 files changed

+89
-2
lines changed

2 files changed

+89
-2
lines changed

src/projection.ts

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import { type CartesianCoordinate, type HorizontalCoordinate } from './common'
1010

11-
import { convertDegreesToRadians as radians } from './utilities'
11+
import { convertRadiansToDegrees as degrees, convertDegreesToRadians as radians } from './utilities'
1212

1313
/*****************************************************************************************************************/
1414

@@ -60,3 +60,48 @@ export const convertHorizontalToStereo = (
6060
}
6161

6262
/*****************************************************************************************************************/
63+
64+
/**
65+
*
66+
* convertStereoToHorizontal()
67+
*
68+
* @param cartesianCoordinate representing the { x, y } position of a particular point to transform.
69+
* @param width (of type number) representing the width of the projected "canvas"
70+
* @param height (of type number) representing the height of the projected "canvas"
71+
* @returns the Horizontal Coordinate { alt, az } conversion from the stereographic projection
72+
*/
73+
export const convertStereoToHorizontal = (
74+
cartesianCoordinate: CartesianCoordinate,
75+
extent: { width: number; height: number },
76+
focus: number = 0.2
77+
): HorizontalCoordinate => {
78+
const { x, y } = cartesianCoordinate
79+
80+
const sinalt1 = 0
81+
82+
const cosalt1 = 1
83+
84+
const h = extent.height
85+
86+
const w = extent.width
87+
88+
const X = (x - w / 2) / h
89+
90+
const Y = (h - y) / h
91+
92+
const P = Math.sqrt(Math.pow(X, 2) + Math.pow(Y, 2))
93+
94+
const c = 2 * Math.atan2(P, 2 * focus)
95+
96+
const alt = Math.asin(Math.cos(c) * sinalt1 + (Y * Math.sin(c) * cosalt1) / P)
97+
98+
const az =
99+
Math.PI + Math.atan2(X * Math.sin(c), P * cosalt1 * Math.cos(c) - Y * sinalt1 * Math.sin(c))
100+
101+
return {
102+
az: degrees(az),
103+
alt: degrees(alt)
104+
}
105+
}
106+
107+
/*****************************************************************************************************************/

tests/projection.spec.ts

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,56 @@ import { describe, expect, it } from 'vitest'
1010

1111
/*****************************************************************************************************************/
1212

13-
import { convertHorizontalToStereo } from '../src'
13+
import { convertHorizontalToStereo, convertStereoToHorizontal } from '../src'
1414

1515
/*****************************************************************************************************************/
1616

1717
describe('convertHorizontalToStereo', () => {
1818
it('should be defined', () => {
1919
expect(convertHorizontalToStereo).toBeDefined()
2020
})
21+
22+
it('should return the correct Cartesian Coordinate { x, y } conversion to the stereographic projection', () => {
23+
const { x, y } = convertHorizontalToStereo(
24+
{
25+
az: 180,
26+
alt: 61.52543906847783
27+
},
28+
{
29+
width: 2000,
30+
height: 2000
31+
},
32+
0.42
33+
)
34+
35+
expect(x).toBeCloseTo(1000)
36+
expect(y).toBeCloseTo(1000)
37+
})
38+
})
39+
40+
/*****************************************************************************************************************/
41+
42+
describe('convertStereoToHorizontal', () => {
43+
it('should be defined', () => {
44+
expect(convertStereoToHorizontal).toBeDefined()
45+
})
46+
47+
it('should return the correct Horizontal Coordinate { az, alt } conversion from the stereographic projection', () => {
48+
const { az, alt } = convertStereoToHorizontal(
49+
{
50+
x: 1000,
51+
y: 1000
52+
},
53+
{
54+
width: 2000,
55+
height: 2000
56+
},
57+
0.42
58+
)
59+
60+
expect(az).toBeCloseTo(180)
61+
expect(alt).toBeCloseTo(61.525439)
62+
})
2163
})
2264

2365
/*****************************************************************************************************************/

0 commit comments

Comments
 (0)