给定层数生成三维金字塔坐标
可以与 算法2 做对比,选择更适合的 当前算法采取理性的思维,做什么事之前,都先做好准备,这样,不管到哪,都能有计划地操作
count 是层数,per_length 是两个点之间的距离,即单位长度
count = 4
per_length = 1
each_side = [each * 2 + 1 for each in range(count)]
each_count = [each ** 2 for each in each_side]
all_count = sum(each_count)
over_count = []
for each in each_count:
if over_count:
over_count.append(over_count[-1] + each)
else:
over_count.append(each)
print(f'每层的边长为{each_side}')
print(f'每层的个数 {each_count}')
print(f'总数为 {all_count}')
print(f'截止到当前层数,上面总个数 {over_count}')
for each in range(all_count):
large = [e for e in over_count if e > each]
floor = over_count.index(large[0])
this_count = each - large[0] + each_count[floor]
this_side = each_side[floor]
print(f'{floor} 第 {this_count}', end='\t')
x = this_count // this_side
y = this_count % this_side
if floor > 0:
x -= floor
y -= floor
z = -floor
x *= per_length
y *= per_length
z *= per_length
print(x, y, z)
解读
- 金字塔的每一层都是正方形,边长第一层是1,第二层是3,第三层是5,一次类推,为连续奇数,即
each_side - 每层的点个数分别为
1^2, 3^2, 5^2 ... ,即each_count - 截止当前层,上面点的个数为
1, 1+9, 1+9+25 ...
|