【问题标题】:How can I divide an interval [start,end] into n points of equal distance?如何将区间 [start,end] 划分为 n 个等距离的点?
【发布时间】:2019-11-06 16:18:29
【问题描述】:

我有一个从startend 的区间,其中startenddouble 类型。我想将区间划分为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

我遇到了麻烦,感谢任何建议

【问题讨论】:

  • end - start / n 会给你点之间的距离。你可以自己去吗?
  • @FedericoklezCulloca 也许我很挑剔,但它应该是 (end-start)/(n-1) 的一个封闭区间
  • @RobertKock 啊,我没有注意到这两个例子展示了不同的东西,我只关注第二个。
  • @FedericoklezCulloca 如果我将这个公式与start = -3.14end = 3.14 一起使用,我会得到2.09333333333...恐怕我不知道如何使用这个值。我错过了什么吗?
  • @FedericoklezCulloca 对不起,现在我明白了。我的坏

标签: java math intervals divide


【解决方案1】:

根据您到目前为止显示的逻辑,interval 的大小是:

  • 关闭:(end - start) / (n - 1)
  • 打开一侧:(end - start) / n
  • 打开两边:(end - start) / (n + 1)

初始左点为:

  • 左侧关闭:start
  • 在左侧打开:start + interval

所有其他点只需在顶部添加一个interval

【讨论】:

    猜你喜欢
    • 2014-11-06
    • 1970-01-01
    • 2018-05-07
    • 1970-01-01
    • 1970-01-01
    • 2021-11-17
    • 1970-01-01
    • 2011-03-22
    • 2013-06-04
    相关资源
    最近更新 更多