python中圆形中均匀间隔点的生成器 [英] Generator of evenly spaced points in a circle in python

查看:26
本文介绍了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 对于 i = 0,1,..,n.

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

每个环应该有 n*i 个点均匀分布在 θ 中,其中n_i=1 代表 i=0;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')

它应该返回如下图:

这是我目前的想法:

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天全站免登陆