计算具有固定最小距离和最大距离的数字序列
Compute a sequence of number whose with fixed min distance and max distance
有没有办法生成 n
个数字,它们之间的 space 递增并且 space 在最小值和最大值之间变化?这些数字的域并不重要。
我想像这样调用一个函数:
const serie = computeSerie(n, minSpace, maxSpace)
// domain is not important, for example [1, +infinity] but also [0, 1], what you prefer
const serie1 = computeSerie(5, 1, 1) // [1, 2, 3, 4, 5]
const serie2 = computeSerie(5, 2, 2) // [1, 3, 5, 7, 9]
const serie3 = computeSerie(5, 1, 4) // [1, ...] I don't know, I suppose to use a pow math function (?)
const serie4 = computeSerie(7, 1, 6) // [1, 2, 4, 8, 13, 18, 24]
视觉上:
serie1: |-|-|-|-|
serie2: |--|--|--|--|
serie3: |-|???|--|
serie4: |-|--|---|----|-----|------|
我不知道如何实现它,也许 d3 可能有用,但如何实现?
非常感谢你的每一个提示
这应该可行,但如果您的 minSpace
和 maxSpace
值与 n
不匹配,您也会得到小数值。 (可以为那些做一个 Math.floor/ceil/round
吗?)
function computeSerie(n, minSpace, maxSpace) {
const step = (maxSpace - minSpace) / (n - 2)
const arr = []
const startAt = 1
arr.push(startAt)
for (let i = 1; i < n; i++) {
arr.push(arr[i - 1] + minSpace + (i - 1) * step)
}
return arr
}
console.log(computeSerie(5, 1, 1)) // [1, 2, 3, 4, 5]
console.log(computeSerie(5, 2, 2)) // [1, 3, 5, 7, 9]
console.log(computeSerie(5, 1, 4)) // [1, 2, 4, 7, 11]
console.log(computeSerie(7, 1, 6)) // [1, 2, 4, 7, 11, 16, 22]
// will get fractional values for this
console.log(computeSerie(7, 1, 5)) // [1, 2, 3.8, 6.4, 9.8, 14, 19]
有没有办法生成 n
个数字,它们之间的 space 递增并且 space 在最小值和最大值之间变化?这些数字的域并不重要。
我想像这样调用一个函数:
const serie = computeSerie(n, minSpace, maxSpace)
// domain is not important, for example [1, +infinity] but also [0, 1], what you prefer
const serie1 = computeSerie(5, 1, 1) // [1, 2, 3, 4, 5]
const serie2 = computeSerie(5, 2, 2) // [1, 3, 5, 7, 9]
const serie3 = computeSerie(5, 1, 4) // [1, ...] I don't know, I suppose to use a pow math function (?)
const serie4 = computeSerie(7, 1, 6) // [1, 2, 4, 8, 13, 18, 24]
视觉上:
serie1: |-|-|-|-|
serie2: |--|--|--|--|
serie3: |-|???|--|
serie4: |-|--|---|----|-----|------|
我不知道如何实现它,也许 d3 可能有用,但如何实现?
非常感谢你的每一个提示
这应该可行,但如果您的 minSpace
和 maxSpace
值与 n
不匹配,您也会得到小数值。 (可以为那些做一个 Math.floor/ceil/round
吗?)
function computeSerie(n, minSpace, maxSpace) {
const step = (maxSpace - minSpace) / (n - 2)
const arr = []
const startAt = 1
arr.push(startAt)
for (let i = 1; i < n; i++) {
arr.push(arr[i - 1] + minSpace + (i - 1) * step)
}
return arr
}
console.log(computeSerie(5, 1, 1)) // [1, 2, 3, 4, 5]
console.log(computeSerie(5, 2, 2)) // [1, 3, 5, 7, 9]
console.log(computeSerie(5, 1, 4)) // [1, 2, 4, 7, 11]
console.log(computeSerie(7, 1, 6)) // [1, 2, 4, 7, 11, 16, 22]
// will get fractional values for this
console.log(computeSerie(7, 1, 5)) // [1, 2, 3.8, 6.4, 9.8, 14, 19]