gpt4 book ai didi

javascript - 点数组的插值

转载 作者:行者123 更新时间:2023-12-04 13:07:11 36 4
gpt4 key购买 nike

我有以下代表星座边界的数据点。

let boundaries = [
{
ra: 344.46530375,
dec: 35.1682358
},
{
ra: 344.34285125,
dec: 53.1680298
},
{
ra: 351.45289375,
dec: 53.1870041
},
...
]

不相关,但这些已从 IAU 引用值中提取。

但是。在这些点中的每一个点之间,我需要所谓的“大圆距离”——或正向距离,在点 i 和点 i+1 之间。为此,我们可以在点 i 和点 i+1 之间以给定精度 x 进行插值。

我编写了以下可以执行此操作的函数(值得注意的是,它是在赤道坐标系中完成的——不是笛卡尔坐标——因此数学稍微复杂一些,但并不复杂)。

两点间插值的函数如下(为了繁荣安全用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 方法,但成功为零......

对于以下“边界”,所需的输出如下:

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
...
]

最佳答案

我认为 Array.reduce() 是这里使用的最佳工具。

您可以编写一个函数,该函数接受一个边界数组和一个预定义并输出您想要的赤道坐标数组:

const fillInBoundryPoints = (boundaries: EquatorialCoordinate[], precision: number) => {
return boundaries.reduce((acc, cur, i, arr) => {
if (i < arr.length - 1) {
acc.push(cur)
acc.push(...getInterpolatedEquatorialCoordinates(cur, arr[i + 1], precision))
}
return acc
}, [] as EquatorialCoordinate[])
}

你会这样使用:

const precision = 10

const boundaries: EquatorialCoordinate[] = [
{
ra: 344.46530375,
dec: 35.1682358
},
{
ra: 344.34285125,
dec: 53.1680298
}
]

const desiredOutput = fillInBoundryPoints(boundaries, precision)

输出:

[
{ ra: 344.46530375, dec: 35.1682358 },
{ 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 }
]

关于javascript - 点数组的插值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68833634/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com