所以,我正在製作一個程序,程序使用隨機顏色製作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()
任何幫助將是巨大的!
謝謝!
檢查是否重疊的工作?所有的圈子都是半徑統一的嗎? –