I'm trying to randomly place a number of small circles within a larger circle using turtle.
The size of the larger circle depends on whether "small", "medium" or "large" are called, and I need the small circles to stay within the bounds of the radius of each circle.
def drawCircle(myTurtle, radius):
circumference = 2 * 3.1415 * radius
sideLength = circumference / 360
drawPolygon(myTurtle,sideLength,360)
How do I use random to place circles with smaller radii within the initial circle?
You could try something like this
from turtle import *
def drawcircles(circum): #multiples of 10
while circum > 0:
circle(circum)
circum -= 10
penup()
left(90)
forward(10)
right(90)
pendown()
drawcircles(50)
exitonclick()
I draw a circle, reset the position and draw again, the only catch with this code is that the circumference is taken in multiples of 10. Now I know your want random circles, so you can modify this to work to way you want. This is the idea though.
To make sure that the nested circles stay within the enclosing circle, the best strategy might be to always return to the centre of the outer circle. You should do this in your draw_circle
function. This way you always know where the turtle is and how far out it can move without leaving the enclosing circle.
After drawing the outer circle (and returning to its centre), you can then turn a random amount, determine the random smaller-radius , move a random amount up to greater-radius - smaller-radius from the centre of the outer circle, draw the smaller circle, return to the centre of the outer circle, and reapeat.
Here's some code that does that:
from turtle import *
import random
def draw_circle(radius, angle):
circumference = 2 * 3.1415 * radius
side_length = circumference / (360 / angle)
penup()
forward(radius)
right(90)
pendown()
for i in range(360 / angle):
forward(side_length)
right(angle)
penup()
left(90)
backward(radius)
def draw_random_circle(outer_radius, angle):
radius = random.randint(1, outer_radius/2)
distance = random.randint(0, outer_radius - radius)
segment = random.randint(0, 360)
right(segment)
forward(distance)
draw_circle(radius, angle)
backward(distance)
left(segment)
draw_circle(100, 6)
for i in range(10):
draw_random_circle(100, 6)
done()
Example:
Suppose your larger circle has radius R
, with your smaller circle having radius r
. That means your small circle's center has to be within a circle of radius Rr
, so that the smaller circle does not exceed the larger one.
Suppose your larger circle is at circle_x
, circle_y
, with radius R
. The smaller circles will have radius r
.
Imports:
from math import sqrt
from random import uniform
Required variables:
center_x: Center x of large circle
center_y: Center y of large circle
R: radius of large circle
r: radius of small circle
Code:
def draw_circle():
radius_bound = R - r
center_x = uniform(circle_x - radius_bound, circle_x + radius_bound)
x_offset = center_x - circle_x
y_limit = sqrt(radius_bound ** 2 - x_offset ** 2)
center_y = uniform(circle_y - y_limit, circle_y + y_limit)
my_turtle.penup()
my_turtle.goto(center_x, center_y - r)
my_turtle.seth(0)
my_turtle.pendown()
my_turtle.circle(r)
We pick a circle_x
value between -radius_bound
and radius_bound
, then a circle_y
value within limits to be inside the circle
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.