在python中的圆圈中均匀分布的点的生成器 [英] Generator of evenly spaced points in a circle in python

查看:687
本文介绍了在python中的圆圈中均匀分布的点的生成器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的任务是在一个看不见的圆的同心环上均匀(或多或少)地生成点.该函数应使用半径列表以及要为给定半径绘制的点数作为参数.例如,半径为0时,应在(0,0)处绘制1点.对于半径为1的圆,应沿圆的圆周绘制10个点,并以2pi/10的角度间隔开.对于半径为2的圆,沿圆周有20个点,它们之间的夹角为2pi/20.

I am tasked with generating evenly (more or less) spaced points on concentric rings of an invisible circle. The function should take a list of radii, and number of points to plot for a given radius as arguments. For example for a radius of 0 it should plot 1 point at (0,0). For a circle of radius of 1, it should plot 10 points along the circumference of the circle, spaced out by an angle of 2pi/10. For a circle of radius 2, 20 points along the circumference, spaced out by an angle of 2pi/20.

生成器应采用以下参数:

The generator should take the following parameters:

n,r_max,m

n, r_max, m

,并应生成半径为的坐标对环

and should generate rings of coordinate pairs at radii

r_i = i * r_max/n.

r_i = i*r_max/n for i = 0,1,..,n.

每个环应具有均匀分布在θ中的n * i个点,其中 对于i = 0,n_i = 1; n_i = mi for i> 0

Each ring should have n*i points uniformly distributed in θ where n_i=1 for i=0; n_i = mi for i>0

当这样调用函数时:

for r, t in genpolar.rtuniform(n=10, rmax=0.1, m=6):
      plot(r * cos(t), r * sin(t), 'bo')

它应该返回如下图:

it should return a plot that looks like:

这是到目前为止我要提出的:

Here is what I've come up with so far:

def rtpairs(R, N):
        R=[0.0,0.1,0.2]
        N=[1,10,20]
        r=[]
        t=[]
        for i in N:
                theta=2*np.pi/i
            t.append(theta)

        for j in R:
            j=j
            r.append(j)

    plt.plot(r*np.cos(t),r*np.sin(t), 'bo')
    plt.show()

但是我很确定有一种使用两个for循环的更有效的方法.

but I'm pretty sure there is a more efficient method using two for loops.

非常感谢

推荐答案

我知道了.代码如下:

import numpy as np
import matplotlib.pyplot as plt

T = [1, 10, 20, 30, 40, 50, 60]
R = [0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6]



def rtpairs(r, n):

    for i in range(len(r)):
       for j in range(n[i]):    
        yield r[i], j*(2 * np.pi / n[i])

for r, t in rtpairs(R, T):
    plt.plot(r * np.cos(t), r * np.sin(t), 'bo')
plt.show()

这篇关于在python中的圆圈中均匀分布的点的生成器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆