Generator of evenly spaced points in a circle in python

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

The generator must take the following parameters:

n, r_max, m

and must generate rings of coordinate pairs at radii

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

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

When a function is called as follows:

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

he should return a plot that looks like this: Plot

Here is what I came 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()

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

Many thanks

+6
source share
4 answers

I understood that. The code is as follows:

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()
+5
source

You need to cycle around the entire circle for all radii, so your plot call is stuck pretty much in the M * N process.

. R ; . t .

, ?

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

python, .

int ringNumber = 0

int n = ringNumber-1

((n/n + 1) * 60) -60 = ( ,

0

.

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

, , , , .

r = [0, 0.1, 0.2]
n = [1, 10, 20]
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()

enter image description here

.

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

enter image description here

0

Source: https://habr.com/ru/post/1614361/


All Articles