在圆内均匀分布点

转载自: https://www.cnblogs.com/yunlambert/p/10161339.html

import numpy as np
import matplotlib.pyplot as plt
import random
import math

def random_point(car_num,radius):
    for i in range(1, car_num + 1):
        theta = random.random() * 2 * np.pi
        r = random.uniform(0, radius) #about uniform see http://www.runoob.com/python/func-number-uniform.html
        x = math.cos(theta) * (r ** 0.5)
        y = math.sin(theta) * (r ** 0.5)
        plt.plot(x, y, '*', color="blue") #print the point in the canvas

def main():
    pi = np.pi
    theta = np.linspace(0, pi * 2, 1000) #about linespace see https://blog.csdn.net/You_are_my_dream/article/details/53493752
    R = 1
    x = np.sin(theta) * R
    y = np.cos(theta) * R

    plt.figure(figsize=(6, 6))
    plt.plot(x, y, label="cycle", color="green", linewidth=2)
    plt.title("random_points_in_circle")
    random_point(4000, R)
    plt.legend()
    plt.show()
    os.system("pause")

main()

 

你可能感兴趣的:(数学)