在 python 中获取正方形边缘的随机点

Get random points at edges of a square in python

问题

给定一个绘图 window,如何在正方形的周边(绘图的周边 window)生成随机点?

背景和尝试

我发现了一个关于 的类似问题。

我设法编写了一个程序来生成限制范围内的随机点,但问题是如何找到随机点,条件是它们位于图的边缘(x 等于 5 或​​ -5 , 或者在这种情况下 y 等于 5 或​​ -5)。

import numpy as np
import matplotlib.pyplot as plt

# Parameters
n = 6 # number of points
a = 5 # upper bound
b = -5 # lower bound

# Random coordinates [b,a) uniform distributed
coordy = (b - a) *  np.random.random_sample((n,)) + a # generate random y
coordx = (b - a) *  np.random.random_sample((n,)) + a # generate random x

# Create limits (x,y)=((-5,5),(-5,5))
plt.xlim((b,a))
plt.ylim((b,a))

# Plot points
for i in range(n):
    plt.plot(coordx[i],coordy[i],'ro')

plt.show()

总结

总而言之,我的问题是如何生成随机坐标,因为它们位于 plot/canvas 的边缘。任何建议或帮助将不胜感激。

一种可能的方法(尽管不是很优雅)如下:划分水平点和垂直点假设你想在 window 的顶部或底部绘制一个点。那么,

  1. Select 随机 y 坐标为 b 或 -b
  2. Select随机(均匀分布)x坐标

window 的左右边缘的类似方法。

希望对您有所帮助。

您可以使用它,但这是假设您希望在发现它们不在边缘时丢弃它们。

for x in coordx:
    if x != a:
        coordx.pop(x)
    else:
        continue

然后对 y 做同样的事情。

这是一种方法:

import numpy as np
import matplotlib.pyplot as plt

# Parameters
n = 6 # number of points
a = 5 # upper bound
b = -5 # lower bound

# Random coordinates [b,a) uniform distributed
coordy = (b - a) *  np.random.random_sample((n,)) + a # generate random y
coordx = (b - a) *  np.random.random_sample((n,)) + a # generate random x

# This is the new code
reset_axis = np.random.choice([True, False], n) # select which axis to reset
reset_direction = np.random.choice([a,b], n) # select to go up / right or down / left

coordx[reset_axis] = reset_direction[reset_axis]
coordy[~reset_axis] = reset_direction[~reset_axis]
# end of new code. 

# Create limits (x,y)=((-5,5),(-5,5))
plt.xlim((b,a))
plt.ylim((b,a))

# Plot points
for i in range(n):
    plt.plot(coordx[i],coordy[i],'ro')

plt.show()

结果是:

您可以执行以下操作:

from random import choice
import matplotlib.pyplot as plt
from numpy.random import random_sample

n = 6
a = 5
b = -5

plt.xlim((b,a))
plt.ylim((b,a))

for i in range(n):
    r = (b - a) * random_sample() + a
    random_point = choice([(choice([a,b]), r),(r, choice([a,b]))])
    plt.scatter(random_point[0],random_point[1])

plt.show()

输出:

从几何上讲,在边上需要一个点满足一定的条件。假设我们正在谈论一个网格,其尺寸由 x ~ [0, a]y ~ [0, b]:

定义
  • y 坐标为 0 或 b,x 坐标在 [0, a] 范围内,或
  • x 坐标为 0 或 a,y 坐标在 [0, b]
  • 范围内

显然有不止一种方法可以解决这个问题,但这里有一个简单的方法可以帮助您入门。

def plot_edges(n_points, x_max, y_max, x_min=0, y_min=0):
    # if x_max - x_min = y_max - y_min, plot a square
    # otherwise, plot a rectangle

    vertical_edge_x = np.random.uniform(x_min, x_max, n_points)
    vertical_edige_y = np.asarray([y_min, y_max])[
        np.random.randint(2, size=n_points)
    ]
    horizontal_edge_x = np.asarray([x_min, x_max])[
        np.random.randint(2, size=n_points)
    ]
    horizontal_edge_y = np.random.uniform(x_min, x_max, n_points)

    # plot generated points
    plt.scatter(vertical_edge_x, vertical_edige_y)
    plt.scatter(horizontal_edge_x, horizontal_edge_y)
    plt.show()

你能试试这个吗?

import numpy as np
import matplotlib.pyplot as plt

# Parameters
n = 6 # number of points
a = 5 # upper bound
b = -5 # lower bound

import random

coordx,coordy=[],[]
for i in range(n):
    xy = random.choice(['x','y'])
    if xy=='x':
        coordx.append(random.choice([b,a])) # generate random x
        coordy.append(random.random()) # generate random y
    if xy=='y':
        coordx.append(random.random()) # generate random x
        coordy.append(random.choice([b,a])) # generate random y

# Create limits (x,y)=((-5,5),(-5,5))
plt.xlim((b,a))
plt.ylim((b,a))

# Plot points
for i in range(n):
    plt.plot(coordx[i],coordy[i],'ro')

plt.show()

这是一个示例输出: