我有以下数据点,代表一个星座的边界。
let boundaries = [
{
ra: 344.46530375,
dec: 35.1682358
},
{
ra: 344.34285125,
dec: 53.1680298
},
{
ra: 351.45289375,
dec: 53.1870041
},
...
]
虽然不相关,但这些数值已从IAU提供的数据中提取出来。
然而,在这些点之间,我需要计算所谓的“大圆距离”或正弦距离,即点i
和点i+1
之间的正交距离。为了做到这一点,我们可以在给定精度x
处在点i
和点i+1
之间进行插值。
我编写了以下函数来执行此操作(值得注意的是,它是在赤道坐标系中完成的 - 而不是笛卡尔坐标系 - 因此数学上略微复杂,但并不是非常复杂)。
插值两个点的函数如下(用TypeScript ES6编写,以确保效率和安全性):
/**
* getInterpolatedEquatorialCoordinates()
*
* @description This function takes in a start and end
* @param start EquatorialCoordinate
* @param end EquatorialCoordinate
* @param precision number
* @output returns the interpolated array of []EquatorialCoordinate points:
*/
export const getInterpolatedEquatorialCoordinates = (
start: EquatorialCoordinate,
end: EquatorialCoordinate,
precision: number
): Array<EquatorialCoordinate> => {
// The interpolated points array to return:
const points: Array<EquatorialCoordinate> = []
// Obtain the difference between the end coordinate's Right Ascension, RA, and the start coordinate's RA:
let nra = ((end.ra - start.ra)) % 360
if (nra > 180) nra = nra - 360
if (nra < -180) nra = nra + 360
// Obtain the "gradient" rate of change of RA:
const dra: number = nra / precision
// Obtain the difference between the end coordinate's Declination, dec, and the start coordinate's dec:
const ndec = ((end.dec - start.dec))
// Obtain the "gradient" rate of change of dec:
const ddec: number = ndec / precision
let i = 0
// Obtain the interpolated EquatorialCoordinate points for each step:
while (i < precision) {
i++
points.push({
ra: start.ra + (dra * i),
dec: start.dec + (ddec * i)
})
}
return points
}
我已经通过测试套件运行了它,位于上述“边界”的前两个点之间,以在数学上获得其有效性:
test('getInterpolatedEquatorialCoordinates', function () {
const start = {
ra: 344.46530375,
dec: 35.1682358
}
const end = {
ra: 344.34285125,
dec: 53.1680298
}
const minRA = Math.min(start.ra, end.ra)
const minDec = Math.min(start.dec, end.dec)
const maxRA = Math.max(start.ra, end.ra)
const maxDec = Math.max(start.ra, end.ra)
const precision = 10
const points = getInterpolatedEquatorialCoordinates(start, end, precision)
const isRAAboveLowerThreshold = (
p: EquatorialCoordinate
) => p.ra >= minRA
const isRABelowUpperThreshold = (
p: EquatorialCoordinate
) => p.ra <= maxRA
const isDecAboveLowerThreshold = (
p: EquatorialCoordinate
) => p.dec >= minDec
const isDecBelowUpperThreshold = (
p: EquatorialCoordinate
) => p.dec <= maxDec
expect(points.length).toBeGreaterThan(0)
expect(points.length).toBe(precision)
expect(points.every(isRAAboveLowerThreshold)).toBe(true)
expect(points.every(isRABelowUpperThreshold)).toBe(true)
expect(points.every(isDecAboveLowerThreshold)).toBe(true)
expect(points.every(isDecBelowUpperThreshold)).toBe(true)
})
插值输出的点如下:
[
{ ra: 344.4530585, dec: 36.968215199999996 },
{ ra: 344.44081324999996, dec: 38.7681946 },
{ ra: 344.428568, dec: 40.568174 },
{ ra: 344.41632275, dec: 42.3681534 },
{ ra: 344.40407749999997, dec: 44.168132799999995 },
{ ra: 344.39183225, dec: 45.9681122 },
{ ra: 344.379587, dec: 47.7680916 },
{ ra: 344.36734175000004, dec: 49.568071 },
{ ra: 344.3550965, dec: 51.3680504 },
{ ra: 344.34285125, dec: 53.1680298 }
]
这似乎对任何精度都可以获得我满意的良好准确度,而且它也不受我们“方向”是哪个的影响。
问题:
现在我想将其应用于上述boundaries
数组,对于每个点,返回一个合并所有点的新数组...
最好的方法是什么?我已尝试使用Array.prototype.map
方法,但完全没有成功...
以下是所需输出,适用于以下“boundaries
”:
let boundaries = [
{
ra: 344.46530375,
dec: 35.1682358
},
{
ra: 344.34285125,
dec: 53.1680298
},
...
]
let desiredOutput = [
{
ra: 344.46530375,
dec: 35.1682358
}, // Original Index 0
// Start Interpolated Intermediary Values
{
ra: 344.4530585,
dec: 36.968215199999996
},
{
ra: 344.44081324999996,
dec: 38.7681946
},
{
ra: 344.428568,
dec: 40.568174
},
{
ra: 344.41632275,
dec: 42.3681534
},
{
ra: 344.40407749999997,
dec: 44.168132799999995
},
{
ra: 344.39183225,
dec: 45.9681122
},
{
ra: 344.379587,
dec: 47.7680916
},
{
ra: 344.36734175000004,
dec: 49.568071
},
{
ra: 344.3550965,
dec: 51.3680504
},
{
ra: 344.34285125,
dec: 53.1680298
},
// End Interpolated Intermediary Values
{
ra: 344.34285125,
dec: 53.1680298
}, // Original Index 1
...
]