【发布时间】:2018-04-12 23:58:26
【问题描述】:
类似问题:
Generating N random points with certain predefined distance between them
choose n most distant points in R
但它们要么在 matlab 中,要么没有完成所需的任务。
我必须在一个长度为的盒子内创建 N 个点 任意两点之间的距离大于delta。
例如:
假设我在 x、y、z 轴上有一个长度为 10 埃的盒子。
我想在这个盒子里有 200 个随机点,这样最小距离
任意两点之间的距离大于 3 埃。
尝试:
#!python
# -*- coding: utf-8 -*-#
import numpy as np
np.random.seed(100)
np.set_printoptions(2)
box_length = 10
d = box_length
threshold = 6
num_points = 5
x1, y1, z1 = np.random.random(num_points)* box_length, np.random.random(num_points)* box_length, np.random.random(num_points)* box_length
x2, y2, z2 = np.random.random(num_points)* box_length, np.random.random(num_points)* box_length, np.random.random(num_points)* box_length
# print(len(x1))
# just for checking make ponts integers
for i in range(len(x1)):
x1[i] = int(x1[i])
x2[i] = int(x2[i])
y1[i] = int(y1[i])
y2[i] = int(y2[i])
z1[i] = int(z1[i])
z2[i] = int(z2[i])
print(x1)
print(y1)
print(z1)
print("\n")
pt1_lst = []
pt2_lst = []
for i in range(len(x1)):
a, b, c = x1[i], y1[i], z1[i]
a2, b2, c2 = x2[i], y2[i], z2[i]
dist2 = (a-a2)**2 + (b-b2)**2 + (c-c2)**2
print("\n")
print(a,b,c)
print(a2,b2,c2)
print(dist2)
if dist2 > threshold**2:
pt1 = (a,b,c)
pt2 = (a2,b2,c2)
pt1_lst.append(pt1)
pt2_lst.append(pt2)
print("points")
print(pt1_lst)
print(pt2_lst)
代码问题: 此代码比较 points1 和 points2 的点,但不会在 points1 和 points2 内部进行比较。
可能有更好的算法来解决这个问题,并向那些人致敬 提出解决问题的好主意的人。
谢谢。
附言: 我做了一些研究并尝试找到相关链接,但无法解决 问题。 还有一些相关链接是:
更新::
我在下面尝试了 Stefans 的代码,它适用于 N = 10,但我尝试了 N = 200 并且它使用了非常长的时间(我在 10 分钟后停止了代码)。
有什么有效的方法吗?
我们将不胜感激!
All paths of length L from node n using python
Create Random Points Inside Defined Rectangle with Python
【问题讨论】:
-
您可以通过以下步骤在 Scipy (docs.scipy.org/doc/scipy/reference/generated/…) 中使用 cdist: 1- 生成随机点。 2-使用cdist。 3- 删除指定距离内的这些点。