Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Python中使对象不接触_Python_Graphics - Fatal编程技术网

如何在Python中使对象不接触

如何在Python中使对象不接触,python,graphics,Python,Graphics,所以,我正在制作一个程序,该程序以随机颜色制作250个点,并将它们打印在一个窗口上。但是他们不能互相接触 这是我的密码 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, d

所以,我正在制作一个程序,该程序以随机颜色制作250个点,并将它们打印在一个窗口上。但是他们不能互相接触

这是我的密码

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()
从图形导入*
从随机导入*
输入数学
def main():
win=图形宽度(“点”,1100,650)
点=[]
点数=[]
对于范围(0250)内的x:
画圈(赢、点、分)
检查重叠(点、点)
drawAllCircles(赢,点)
def drawCircle(胜利、阵列、积分):
p1=randint(151085)
p2=randint(15635)
点=圆(点(p1,p2),15)
r=lambda:randint(0255)
dot.setFill(“#%02X%02X%02X%”(r(),r(),r())
array.append(点)
点。追加(点(p1,p2))
def检查重叠(阵列、点):
计数=0
对于范围(0,250)内的x:
对于范围(0,250)内的y:
如果x!=y:
如果math.hypot(点[y].getX()-点[x].getX(),点[y].getY()-点[x].getY())<30:
dist=math.hypot(点[y].getX()-点[x].getX(),点[y].getY()-点[x].getY())
newCircle=Circle(点[x].getX()+(abs(dist-31)),点[x].getY()+(abs(dist-31)),15)
r=lambda:randint(0255)
newCircle.setFill(“#%02X%02X%02X%”(r(),r(),r())
数组[x]=newCircle
def drawAllCircles(赢,阵列):
对于范围(0,250)内的x:
数组[x]。绘制(win)
main()
任何帮助都会很好


谢谢

我没有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)
circles=绘制的圆列表
半径=圆的半径;
x=random.randint(11000)
y=random.randint(11000)
而任何([math.sqrt(math.pow(math.fabs(x-c.x),2)+math.pow(math.fabs(y-c.y),2))<圆中c的半径]:
x=random.randint(11000)
y=random.randint(11000)
从图形导入*
从随机导入*
输入数学
def main():
win=图形宽度(“点”,1100,650)
点=[]
对于x范围内的x(250):
#创建一个随机圆
圆圈=getdrawCircle(15,635,15,1085,15,15)
#与其他圆重叠时重复此操作
选中重叠时(圆、点):
#创建一个新的随机圆
圆圈=getdrawCircle(15,635,15,1085,15,15)
#新圆不重叠,然后附加到列表点
点。附加(圆)
drawAllCircles(赢,点)
def getdrawCircle(最小高度、最大高度、最小宽度、最大宽度、最小半径、最大半径):
x=randint(最小高度、最大高度)
y=randint(最小宽度,最大宽度)
点=圆(点(y,x),点(最小半径,最大半径))
r=lambda:randint(0255)
dot.setFill(“#%02X%02X%02X%”(r(),r(),r())
返回点
#如果圆与数组中的圆重叠,则返回True
def检查重叠(圆、阵列):
对于阵列中的圆\u cmp:
dist=math.hypot(circle.getCenter().getX()-circle\u cmp.getCenter().getX(),
circle.getCenter().getY()-circle\u cmp.getCenter().getY())
如果距离
检查重叠是否有效?所有圆的半径是否一致?@user3344862您可以更改为
circle=getdrawCircle(15635,151085,10,25)
;-)
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()