如何按距原点的距离顺序生成 3d 整数坐标

How can I generate 3d integer coordinates in order of distance from an origin

我想按距原点的距离顺序生成一系列坐标。序列显然是无限的,所以只生成它们并按距离排序对我来说不起作用。

对于那些距离相同的点,我不关心顺序。

例如,这里有一些点,它们与原点的距离最多相距两步。

# d² = 0
(0,0,0)
# d² = 1
(0,0,-1)
(0,-1,0)
(-1,0,0)
(1,0,0)
(0,1,0)
(0,0,1)
# d² = 2
(0,-1,-1)
(-1,0,-1)
(1,0,-1)
(0,1,-1)
(-1,-1,0)
(1,-1,0)
(-1,1,0)
(1,1,0)
(0,-1,1)
(-1,0,1)
(1,0,1)
(0,1,1)
# d² = 3
(-1,-1,-1)
(1,-1,-1)
(-1,1,-1)
(1,1,-1)
(-1,-1,1)
(1,-1,1)
(-1,1,1)
(1,1,1)
# d² = 4
(0,0,-2)
(0,-2,0)
(-2,0,0)
(2,0,0)
(0,2,0)
(0,0,2)
# d² = 5
(0,-1,-2)
(-1,0,-2)
(1,0,-2)
(0,1,-2)
(0,-2,-1)
(-2,0,-1)
(2,0,-1)
(0,2,-1)
(-1,-2,0)
(1,-2,0)
(-2,-1,0)
(2,-1,0)
(-2,1,0)
(2,1,0)
(-1,2,0)
(1,2,0)
(0,-2,1)
(-2,0,1)
(2,0,1)
(0,2,1)
(0,-1,2)
(-1,0,2)
(1,0,2)
(0,1,2)
# d² = 6
(-1,-1,-2)
(1,-1,-2)
(-1,1,-2)
(1,1,-2)
(-1,-2,-1)
(1,-2,-1)
(-2,-1,-1)
(2,-1,-1)
(-2,1,-1)
(2,1,-1)
(-1,2,-1)
(1,2,-1)
(-1,-2,1)
(1,-2,1)
(-2,-1,1)
(2,-1,1)
(-2,1,1)
(2,1,1)
(-1,2,1)
(1,2,1)
(-1,-1,2)
(1,-1,2)
(-1,1,2)
(1,1,2)
# d² = 8
(0,-2,-2)
(-2,0,-2)
(2,0,-2)
(0,2,-2)
(-2,-2,0)
(2,-2,0)
(-2,2,0)
(2,2,0)
(0,-2,2)
(-2,0,2)
(2,0,2)
(0,2,2)
# d² = 9
(-1,-2,-2)
(1,-2,-2)
(-2,-1,-2)
(2,-1,-2)
(-2,1,-2)
(2,1,-2)
(-1,2,-2)
(1,2,-2)
(-2,-2,-1)
(2,-2,-1)
(-2,2,-1)
(2,2,-1)
(-2,-2,1)
(2,-2,1)
(-2,2,1)
(2,2,1)
(-1,-2,2)
(1,-2,2)
(-2,-1,2)
(2,-1,2)
(-2,1,2)
(2,1,2)
(-1,2,2)
(1,2,2)
# d² = 12
(-2,-2,-2)
(2,-2,-2)
(-2,2,-2)
(2,2,-2)
(-2,-2,2)
(2,-2,2)
(-2,2,2)
(2,2,2)

从解决方案 aaa 开始,然后是 aab,然后是 abc,您可以检索所有其他 "permutations",如 aba、baa、-a-a-b 等等。所以你可以保持a < b < c,正数。

几何学:地球上的八分之一三角形。对于一个圆 x²+y²,一个人会在 x 上迭代四分之一,并让 y 从 r² - x² 中检索。在这里发生给定 a.

不幸的是,编码对我来说太多了,无法在阳光明媚的星期天回答。 (=够硬)。 伪代码示意图:

int distance = -1;
int a;
int b;
int c;
PermutationIterator perm = ...

Point next() {
    if (perm.atEnd()) { // Initially true.
        perm.nextDistance();
        ++distance;
        a = distance;
        b = a;
        c = a;
        // Will return Point(a, a, a);
    }
    return perm.nextPerm();
}