如何将区间 [start,end] 分成 n 个等距离的点?
How can I divide an interval [start,end] into n points of equal distance?
我有一个从 start
到 end
的区间,其中 start
和 end
的类型是 double
。我想将区间划分为 n
个点,其中每两个相邻点之间的距离相同。
例如:
// Given closed interval [-3.14,3.14]:
start = -3.14
end = 3.14
n = 3
// The 3 points would be:
-3.14, 0.0, 3.14
// Where the distance between each two neighboring points is 3.14
或者:
// Given left-closed, right-open interval [0,1):
start = 0
end = 1
n = 4
// The 4 points would be:
0.0, 0.25, 0.5, 0.75
// Where the distance between each two neighboring points is .25
我遇到了这个问题,感谢任何建议
根据您目前展示的逻辑,interval
尺寸为:
- 关闭:
(end - start) / (n - 1)
- 打开一侧:
(end - start) / n
- 打开两侧:
(end - start) / (n + 1)
初始左点为:
- 左侧关闭:
start
- 左边打开:
start + interval
.
所有其他点只需在顶部添加一个interval
我有一个从 start
到 end
的区间,其中 start
和 end
的类型是 double
。我想将区间划分为 n
个点,其中每两个相邻点之间的距离相同。
例如:
// Given closed interval [-3.14,3.14]:
start = -3.14
end = 3.14
n = 3
// The 3 points would be:
-3.14, 0.0, 3.14
// Where the distance between each two neighboring points is 3.14
或者:
// Given left-closed, right-open interval [0,1):
start = 0
end = 1
n = 4
// The 4 points would be:
0.0, 0.25, 0.5, 0.75
// Where the distance between each two neighboring points is .25
我遇到了这个问题,感谢任何建议
根据您目前展示的逻辑,interval
尺寸为:
- 关闭:
(end - start) / (n - 1)
- 打开一侧:
(end - start) / n
- 打开两侧:
(end - start) / (n + 1)
初始左点为:
- 左侧关闭:
start
- 左边打开:
start + interval
.
所有其他点只需在顶部添加一个interval