【发布时间】:2017-11-26 20:41:48
【问题描述】:
我想生成所有这些果岭。我知道我走错了
我正在使用以下代码
import java.util.ArrayList;
import java.util.List;
public class classA {
static final int startX = 52760;
static final int startY = 72440;
static final int endX = 52520;
static final int endY = 71896;
static final List<String> coordinates = new ArrayList<>();
public static void main(String[] args)
{
calculate(startX, startY, endX, endY);
coordinates.forEach(System.out::println);
}
private static void calculate(int _startX, int _startY, int _endX, int _endY)
{
final int _x = (_startX + _endX) / 2;
final int _y = (_startY + _endY) / 2;
coordinates.add(_x + "," + _y);
if (coordinates.size() != 8)
calculate(startX, startY, _x, _y);
}
}
我可以理解代码将从“下一个”结束值(即中心)计算开始值,但我无法配置它
输出是
我应该怎么做?谢谢!
【问题讨论】:
-
为什么每次都除以2? (或者事实上,为什么这里完全涉及 2?)
-
这更像是一个数学问题,而不是除以 2,您应该计算这 2 个点之间的距离,然后将点放在该距离的 1/8 上,然后放在距离的 2/8 上,等等.
-
为什么涉及递归?这可以通过每次增加开始和结束之间距离的 1/9 的循环来轻松完成。 (为什么是 1/9 而不是 1/8?谷歌一错再错)
-
@FilipRistic 考虑到以下代码是正确的,这种情况下的距离是 594 (int) Math.sqrt((endX - startX) * (endX - startX) + (endY - startY) * (endY - startY)) 但是我怎样才能得到这个 594 的 1/8 的坐标呢?
标签: java coordinates