2015-05-01 53 views
2

所以,我正在製作一個程序,程序使用隨機顏色製作250個點並將它們打印在窗口上。但他們不能碰到對方。如何讓對象不碰在Python中

這裏是我的代碼

from graphics import * 
from random import * 
import math 

def main(): 
    win = GraphWin("Dots", 1100, 650) 
    dots = [] 
    points = [] 
    for x in range(0,250): 
     drawCircle(win, dots, points) 
    checkOverLap(dots, points) 
    drawAllCircles(win, dots) 

def drawCircle(win, array, points): 
    p1 = randint(15,1085) 
    p2 = randint(15,635) 
    dot = Circle(Point(p1, p2), 15) 
    r = lambda: randint(0,255) 
    dot.setFill('#%02X%02X%02X' % (r(),r(),r())) 
    array.append(dot) 
    points.append(Point(p1, p2)) 


def checkOverLap(array, points): 
    count = 0 
    for x in range(0, 250): 
     for y in range(0, 250): 
      if x != y: 
       if math.hypot(points[y].getX() - points[x].getX(), points[y].getY() - points[x].getY()) < 30: 
        dist = math.hypot(points[y].getX() - points[x].getX(), points[y].getY() - points[x].getY()) 
        newCircle = Circle(Point(points[x].getX() + (abs(dist - 31)), points[x].getY() + (abs(dist - 31))), 15) 
        r = lambda: randint(0,255) 
        newCircle.setFill('#%02X%02X%02X' % (r(),r(),r())) 
        array[x] = newCircle 


def drawAllCircles(win, array): 
    for x in range(0, 250): 
     array[x].draw(win) 



main() 

任何幫助將是巨大的!

謝謝!

+0

檢查是否重疊的工作?所有的圈子都是半徑統一的嗎? –

回答

1
from graphics import * 
from random import * 
import math 

def main(): 
    win = GraphWin("Dots", 1100, 650) 
    dots = [] 
    for x in xrange(250): 
    #Create a random circle 
    circle = getdrawCircle(15, 635, 15, 1085, 15, 15) 
    #Repeat while overlap with other circles 
    while checkOverLap(circle, dots): 
     #Create a new random circle 
     circle = getdrawCircle(15, 635, 15, 1085, 15, 15) 
    #The new circle isn't overlap, then append to list dots 
    dots.append(circle) 
    drawAllCircles(win, dots) 

def getdrawCircle(min_height, max_height, min_width, max_width, min_radius, max_radius): 
    x = randint(min_height, max_height) 
    y = randint(min_width, max_width) 
    dot = Circle(Point(y, x), randint(min_radius, max_radius)) 
    r = lambda: randint(0,255) 
    dot.setFill('#%02X%02X%02X' % (r(),r(),r())) 
    return dot 

#If circle overlap with circles in array then return True 
def checkOverLap(circle, array): 
    for circle_cmp in array: 
    dist = math.hypot(circle.getCenter().getX() - circle_cmp.getCenter().getX(), 
         circle.getCenter().getY() - circle_cmp.getCenter().getY()) 
    if dist < circle.getRadius() + circle_cmp.getRadius(): 
     return True 
    return False 

def drawAllCircles(win, array): 
    for x in range(0, 250): 
    array[x].draw(win) 

main() 
+0

這工作完美無瑕!謝謝! – user3344862

+0

@ user3344862您可能會更改爲'circle = getdrawCircle(15,635,15,1085,10,25)';-) –

1

我沒有Windows電腦,所以我會給你我最好的答案。

嘗試挑選圓的座標的隨機數,並循環通過已繪製的圓,看看在這些座標繪製的圓會觸及其他任何東西。使用while循環,可以保持採摘隨機座標,直到他們不碰任何東西:

circles = list_of_circles_drawn 
radius = radius_of_circles; 
x = random.randint(1, 1000) 
y = random.randint(1, 1000) 
while any([math.sqrt(math.pow(math.fabs(x-c.x), 2)+math.pow(math.fabs(y-c.y), 2)) < radius for c in circles]): 
    x = random.randint(1, 1000) 
    y = random.randint(1, 1000)