简体   繁体   English

如何在 python 中生成一组二维点,其中任何点都在至少一个其他点的 R 单位内

[英]How to generate a set of 2D points in python where any point is within R units of at least one other point

Eg if I have points A,B and C, the euclidean distance例如,如果我有点 A、B 和 C,欧几里得距离

which means than A is within R units from at least one neighbor.这意味着 A 至少在一个邻居的 R 单位内。

Here a have generated an initial point and from there a try to generate the next one within the euclidean distance using the random function.这里已经生成了一个初始点,然后尝试使用随机 function 在欧几里得距离内生成下一个点。

import random
import math
import matplotlib.pyplot as plt

euclideanR = 5
NUMPOINTS = 1000
SPACEMIN = -30
SPACEMAX = 30

# Euclidean distance
def distance(p, q):
    return math.sqrt((q[0]-p[0])**2 + (q[1]-p[1])**2)

# try new point
def calc_new(points):
    x1 = points[0]
    y1 = points[1]

    # help the function place the next point
    x2 = x1-2*abs(euclideanR)*random.random()+euclideanR
    y2 = y1-2*abs(euclideanR)*random.random()+euclideanR
    return (x2,y2)

# generate first point in this space range
x0 = random.randint(SPACEMIN, SPACEMAX)
y0 = random.randint(SPACEMIN, SPACEMAX)
points2D = [(x0, y0)]

dist_list = []
while len(points2D) < NUMPOINTS:

    q = random.choice(points2D)
    p = calc_new(q)
    d = distance(q, p)

    if d < euclideanR:
        dist_list.append(d)
        points2D.append(p)

print("Num points = {}".format(len(points2D)))
print("Minimal Euclidean Distance = {}".format(min(dist_list)))
print("Maximum Euclidean Distance = {}".format(max(dist_list)))

x, y = list(zip(*points2D))
fig = plt.figure()
plt.scatter(x, y)
plt.title("distanceR = {}, #points = {}, Init_space=[{},{}]".
                format(euclideanR, NUMPOINTS, SPACEMIN, SPACEMAX))
plt.show()

Output Output

Num points = 1000
Minimal Euclidean Distance = 0.22933741053890785
Maximum Euclidean Distance = 4.999991431695809

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 围绕另一个点旋转一组2D点 - Rotating a set of 2D points about another point Python:检查一组 2D 点是否包含至少 2 条水平线 - Python: Check if set of 2D points contain at least 2 horizontal lines 二维平面中任意点(自身除外)的最近邻点 - Closest neighbor to any point (other than itself) in a 2D plane 在二维空间中找到一个点周围的点 - Find the points surrounding a point in a 2D space 如何生成均匀分布在矩形上的二维点? - How to generate point in 2D uniformly distributed on a rectangle? 使用 2 个不同的测量单位在 2d 网格中旋转一个点 - rotate a point in a 2d grid with 2 different measure units 在python中查找给定点和点列表之间的最近和最远点(2D) - Find the closest and the farthest points (2D) between a given point and a list of points in python 在2D numpy矩阵中找到特定点距离1内的所有点 - Find all points within distance 1 of specific point in 2D numpy matrix 如何对图像进行二进制处理,使用python通过阈值点将零和一分配为2D图像并将其转换为RGB? - how to binaries the image, assign the zeros and one into 2D image by a threshold point using python and convert it to RGB? 如何使用python pandas为具有超过1M点的点集中的每个点找到最接近的8个点 - How to find the nearest 8 points for each point in a point set with more than 1M points with python pandas
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM