python和pygame中的随机非重叠圆(控制圆数) [英] Random non overlapping circles(with circle number controlled) in python and pygame

查看:80
本文介绍了python和pygame中的随机非重叠圆(控制圆数)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写具有不同半径的非重叠随机圆的代码.我得到了应得的,但是我检查重叠或非重叠的if"语句排除了许多圆.所以,我得到的圈数较少.代码如下:

导入pygame将 numpy 导入为 nppygame.init()显示宽度 = 800显示高度 = 500黑色 = [0, 0, 0]白色 = [255, 255, 255]红色 = [255, 0, 0]display_surface = pygame.display.set_mode((display_width, display_height))时钟 = pygame.time.Clock()pygame.display.set_caption("随机圆")定义圆(x,y,r):pygame.draw.circle(display_surface, red, (int(x), int(y)), int(r), 2)定义距离(x1,y1,x2,y2):dsq = (x1 - x2) ** 2 + (y1 - y2) ** 2d = np.sqrt(dsq)返回n = 100r = np.random.randint(10, 20, size=n)x = np.random.randint(r, display_width - r, size=n)y = np.random.randint(r, display_height - r, size=n)display_surface.fill(黑色)对于范围内的 i(len(r)):有效 = 真对于范围内的 j(len(r)):如果我 != j:d = 距离(x[i], y[i], x[j], y[j])如果 d 

我看到了一个问题的答案

随机导入导入数学导入pygamedef euclidean_distance(x1, y1, x2, y2):#return math.sqrt((x1 - x2) ** 2 + (y1 - y2) ** 2)返回 math.hypot((x1 - x2), (y1 - y2))pygame.init()窗口 = pygame.display.set_mode((400, 400))font50 = pygame.font.SysFont(None, 50)时钟 = pygame.time.Clock()pygame.time.delay(10000)运行 = 真circle_list = []运行时:时钟滴答(100)对于 pygame.event.get() 中的事件:如果 event.type == pygame.QUIT:运行 = 错误r = random.randint(10, 20)x = random.randint(10+r, window.get_width()- r-10)y = random.randint(10+r, window.get_height()-r-10)如果没有任何((x2, y2, r2) for x2, y2, r2 in circle_list if euclidean_distance(x, y, x2, y2) 

I was writing the code for non overlapping random circles with different radii.I kind of got the deserved, but my 'if' statement that checks overlap or non overlap excludes a number of circles. So, I get lesser number of circles. Here's the code:

import pygame
import numpy as np

pygame.init()
display_width = 800
display_height = 500

black = [0, 0, 0]
white = [255, 255, 255]
red = [255, 0, 0]

display_surface = pygame.display.set_mode((display_width, display_height))
clock = pygame.time.Clock()
pygame.display.set_caption("Random Circle")


def circle(x, y, r):
    pygame.draw.circle(display_surface, red, (int(x), int(y)), int(r), 2)


def distance(x1, y1, x2, y2):
    dsq = (x1 - x2) ** 2 + (y1 - y2) ** 2
    d = np.sqrt(dsq)
    return d


n = 100

r = np.random.randint(10, 20, size=n)
x = np.random.randint(r, display_width - r, size=n)
y = np.random.randint(r, display_height - r, size=n)

display_surface.fill(black)

for i in range(len(r)):
    valid = True
    for j in range(len(r)):
        if i != j:
            d = distance(x[i], y[i], x[j], y[j])
            if d < r[i] + r[j]:
                valid = False

    if valid:

        circle(x[i], y[i], r[i])

    pygame.display.update()
    clock.tick(100)

exit = False
while not exit:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()

I saw an answer to a question here which does exactly what this code does( except it uses a class of circles which I didn't use). I sense that I have to use some kind of while loop which doesn't end until I get my desired number o circles. But I find difficulty in actually writing the code. Can anyone help?

解决方案

Create a list of circles Each circle is a tuple with 3 components, the x and y coordinate and the radius. Append new circles as long the length of the list is less than n:

circle_list = []
while len(circle_list) < n:
    # [...]

Create a random position and radius:

r = random.randint(10, 20)
x = random.randint(r, display_width - r)
y = random.randint(r, display_height - r)

Evaluate if the circle intersects with a other circle wich is in the list:

collide = False
for x2, y2, r2 in circle_list:
    d = distance(x, y, x2, y2)
    if d < r + r2:
        collide = True
        break

Append the circle if it does not collide:

if not collide:
    circle_list.append((x, y, r))


Minimal example:

import random
import math
import pygame

def euclidean_distance(x1, y1, x2, y2):
    #return math.sqrt((x1 - x2) ** 2 + (y1 - y2) ** 2)
    return math.hypot((x1 - x2), (y1 - y2))

pygame.init()
window = pygame.display.set_mode((400, 400))
font50 = pygame.font.SysFont(None, 50)
clock = pygame.time.Clock()

pygame.time.delay(10000)

run = True
circle_list = []
while run:
    clock.tick(100)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    r = random.randint(10, 20)
    x = random.randint(10+r, window.get_width()- r-10)
    y = random.randint(10+r, window.get_height()-r-10)
    if not any((x2, y2, r2) for x2, y2, r2 in circle_list if euclidean_distance(x, y, x2, y2) < r + r2):
        circle_list.append((x, y, r))    

    window.fill((255, 255, 255))
    for x, y, r in circle_list:
        pygame.draw.circle(window, (0, 0, 0), (round(x), round(y)), int(r), 3)
    window.blit(font50.render(str(len(circle_list)), True, (255, 0, 0)), (10, 10))
    pygame.display.flip()
 
pygame.quit()
exit()

这篇关于python和pygame中的随机非重叠圆(控制圆数)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆