Generator of evenly spaced points in a circle in python

断了今生、忘了曾经 提交于 2019-11-30 07:29:32

I figured it out. The code goes like this:

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()

Here is one way to do this.

import numpy as np
import matplotlib.pyplot as plt

def circle_points(r, n):
    circles = []
    for r, n in zip(r, n):
        t = np.linspace(0, two_pi, n)
        x = r * np.cos(t)
        y = r * np.sin(t)
        circles.append(np.c_[x, y])
    return circles

When you pass this function in the appropriate lists, one with the radius for each circle and the other with the desired number of points it returns a list of coordinate arrays, one for each circle.

r = [0, 0.1, 0.2]
n = [1, 10, 20]
circles = circle_points(r, n)

These can be plotted as follows.

fig, ax = plt.subplots()
for circle in circles:
    ax.scatter(circle[:, 0], circle[:, 1])
ax.set_aspect('equal')
plt.show()

Here we see the result for more circles.

n = [1, 10, 20, 30, 40, 50, 60]
r = [0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6]
circles = circle_points(r, n)

fig, ax = plt.subplots()
for circle in circles:
    ax.scatter(circle[:, 0], circle[:, 1])
ax.set_aspect('equal')
plt.show()

I don't know python but this formula should help.

int ringNumber = 0

int n = ringNumber-1

((n/n+1)*60)-60 = degrees between points(except for ring zero, which the point is the center

You do have to cycle through the entire circle for all radii, so your plot call is pretty much stuck with some M*N process.

The code details can be slightly improved. For starters, your R list already holds the values you want; there's no need to construct a new list with the same values. You can build the t list with a straightforward list comprehension.

Is this what you wanted?

    N=[1,10,20]
    t = [2*np.pi/i for i in N]
    for R in N:
        plt.plot(R*np.cos(t),R*np.sin(t), 'bo')

    plt.show()
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!