gpt4 book ai didi

python : Calculate the eucledean distances from one coordinates to a array with coordinates

转载 作者:行者123 更新时间:2023-11-28 18:23:31 25 4
gpt4 key购买 nike

因为我之前的问题很不清楚,所以我编辑了一下:

我有以下问题:

我想为半径为 r+fcr_size 的空心球体构建一个图案。空心球体的空腔半径应为 r。有了这个图案,我可以在许多不同的球体中心使用它,并得到许多空心球体。现在我正在寻找最快的解决方案。我的做法是:

    centoEdge = radius+fcr_size #Bounding box coordinates from center to edge
xyz_pattern=[]

#Create the Bounding Box only in positive x,y,z direction, because they are later mirrowed

x1 = range(0,int(centoEdge)+1)
y1 = range(0,int(centoEdge)+1)
z1 = range(0,int(centoEdge)+1)

#Check if coordinates are the hallow sphere and add them to xyz_pattern list
for coords in itertools.product(x1,y1,z1):
if radius < distance.euclidean([0,0,0],coords) <= (radius+fcr_size):
xyz_pattern.append(coords)

#mirrow the pattern arround center
out = []
for point in xyz_pattern:
for factors in itertools.product([1, -1], repeat=3): # (1, 1, 1), (1, 1, -1), (1, -1, 1), ..., (-1, -1, -1)
out.append(tuple(point[i]*factors[i] for i in range(3)))


xyz_pattern=list(set(out))

最佳答案

本方案基于Python函数式编程,希望您喜欢。

import math
from functools import partial
import itertools
import numpy as np


def distance(p1, p2):
return math.sqrt(sum(math.pow(float(x1) - float(x2), 2) for x1, x2 in zip(p1, p2)))


def inside_radius(radius, p):
return distance(p, (0, 0, 0)) < float(radius)


def inside_squre(centoEdge, p):
return all(math.fabs(x) <= centoEdge for x in p)


radius = 5
fcr_siz = 5
centoEdge = radius + fcr_siz
x1 = range(0, int(centoEdge) + 1)
y1 = range(0, int(centoEdge) + 1)
z1 = range(0, int(centoEdge) + 1)
coords = np.array(list(itertools.product(x1, y1, z1)))


inside_squre_with_cento = partial(inside_squre, centoEdge)
inside_radius_with_radius = partial(inside_radius, radius)

result = filter(lambda p: not inside_radius_with_radius(p), filter(inside_squre_with_cento, coords))

print(result)

关于 python : Calculate the eucledean distances from one coordinates to a array with coordinates,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43230182/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com