Background and attempt
我发现一个类似的问题,关于
javascript中的矩形
.
我设法写了一个程序来生成限制范围内的随机点,但问题是关于如何找到随机点,其条件是它们位于图的边缘(在这种情况下,要么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()
Summary
因此,总结起来,我的问题是,考虑到它们处于绘图/画布的边缘,我如何生成随机坐标。如果有任何建议或帮助,我们将不胜感激。
6 个回答
0 人赞同
以下是你可以做的事情。
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()
Output:
0 人赞同
一种可能的方法(尽管不是很优雅)如下:划分水平和垂直的点 假设你想在窗口的顶部或底部画一个点。那么
Select randomly the y coordinate as b or -b
Select randomly (uniform distribution) the x coordinate
窗口的右边和左边也有类似的方法。
希望这有帮助。
0 人赞同
你可以使用这个,但这是假设你想在发现它们不在边缘时丢弃它们。
for x in coordx:
if x != a:
coordx.pop(x)
else:
continue
然后对y做同样的处理。
0 人赞同
从几何学上讲,处于边缘需要一个点满足某些条件。假设我们谈论的是一个网格,其尺寸由x ~ [0, a]和y ~ [0, b]定义。
The y-coordinate is either 0 or b, with the x-coordinate within [0, a], or
The x-coordinate is either 0 or a, with the y-coordinate within [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()
0 人赞同
你可以试试这个吗?
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()
下面是一个输出样本。
0 人赞同
这里有一个方法。
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')