2016-02-26 75 views
0

有沒有一種方法可以在我的圈內龜圖形中生成點(點)? 我想在我的圈子裏面生成10個點。我的代碼由使用模塊中的函數組成。我正在嘗試使用「def」函數來完成此程序。到目前爲止,我知道我需要使用while語句,並且我需要使用「from random import randint」,但就是這樣。我寫了部分代碼,但我不知道這是否正確。這裏是代碼:我可以在圈內生成點嗎?

def randomDotInCircle(): 
    while _in range(10): 
     dots_pos_x = randint(25, 75) 
     dots_pos_y = randint(-37, 37) 
     if (dots_pos_x, dots_pos_y) == 

      turtle.penup() 
      turtle.goto(dots_pos_x, dots_pos_y) 
      turtle.dot(7) 
      turtle.pendown() 

任何幫助如何在圓內創建隨機點?我把魔杖圈在(50,0)和半徑爲50的中心。 任何幫助?

回答

3

我對一無所知,但這是您如何在圓圈內生成隨機點的方法。

import random 
import math 

class Point: 
    def __init__(self, x, y): 
     self.x = x 
     self.y = y 

    def __str__(self): 
     return str((self.x, self.y)) 

class Circle: 
    def __init__(self, origin, radius): 
     self.origin = origin 
     self.radius = radius 

origin = Point(0, 0) 
radius = 50 
circle = Circle(origin, radius) 

for i in range(0, 10): 
    p = random.random() * 2 * math.pi 
    r = circle.radius * math.sqrt(random.random()) 
    x = math.cos(p) * r 
    y = math.sin(p) * r 

    print x, y 

這裏的一般方案是生成一個隨機的角度,p然後預計,角出一個隨機量0radius。這只是一種在圓圈內生成隨機點的方法。

+0

感謝您的幫助。我會嘗試將這些信息應用到烏龜圖形中。 – BenjaminT

+2

這不會選擇統一的點數分佈。如果你願意的話,用生成的隨機數的平方根來計算'r'(例如'r = circle.radius * math.sqrt(random.random()')。 – Blckknght

+0

@Blckknght更新我的答案以包含這個,知道我錯過了一些東西! –

相關問題