假設你的大圈半徑有R
,具有半徑r
你的小了一圈。這意味着你的小圓圈的中心必須位於半徑爲R-r
的圓圈內,以便小圓圈不會超過較大圓圈。
假設你的大圈在circle_x
,circle_y
,半徑爲R
。較小的圓圈將具有半徑r
。
進口:
from math import sqrt
from random import uniform
所需的變量:
center_x: Center x of large circle
center_y: Center y of large circle
R: radius of large circle
r: radius of small circle
代碼:
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)
我們挑選-radius_bound
和radius_bound
之間的circle_x
值,然後限度內circle_y
值爲圈內