current position:Home>Python----Draw a square and inscribed circle, enter the total number of points, so that the points are randomly and evenly distributed in the square and circle ()

Python----Draw a square and inscribed circle, enter the total number of points, so that the points are randomly and evenly distributed in the square and circle ()

2022-11-08 07:20:04will be open today

turtle.geto(x,y): Set the brush start point function (use the center of the screen as the origin to establish a plane rectangular coordinate system)

turtle.dot(number): The size of the dot when drawing a dot

turtle.tracer(1000) and turtle.update():
Speed ​​up the drawing, refresh every 1000 times, otherwise there will be a drawing process (slower)

import random,turtle,math#turtle.goto(x,y) turtle.dot(5), the side length is 800n=eval(input())#Enter the total number of pointsturtle.tracer(1000)turtle.update()#Speed ​​up the painting, refresh once every 1000 times, if not, there will be a painting processturtle.pensize(1)turtle.pencolor("blue")turtle.penup()turtle.goto(-400,-400)turtle.pendown()for i in range(4):turtle.fd(800)turtle.left(90)#'To the leftturtle.fd(400)#straightforward400pxturtle.pencolor("red")turtle.circle(400)for i in range(n):x,y=random.random()*400,random.random()*400signx,signy=random.choice("+-"),random.choice("+-")turtle.penup()#Randomly generate positive and negative coordinatesturtle.goto(eval(signx+str(x)), eval(signy+str(y)))if math. sqrt(eval(signx+str(x))**2+eval(signy+str(y))**2)<=400:#Distinguish inside the circle (red) and outside the circle (blue)turtle.pencolor("red")else:turtle.pencolor("blue")turtle.pendown()turtle.dot(10)turtle.done()

copyright notice
author[will be open today],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/312/202211080628358251.html

Random recommended