python边缘检测代码_Python中的边缘检测

我试着写一个程序,用户输入一个数字,它在屏幕上画出那么多的矩形,但是三角形不能重叠。我对最后一部分有问题,我正在寻求帮助。我借用了Al-Sweigart的一本书中的边缘检测方法,他编写的完整程序可以在这里找到:

以下是我正在执行的程序:import pygame, sys, random

from pygame.locals import *

def doRectsOverlap(rect1, rect2):

for a, b in [(rect1, rect2)]:

# Check if a's corners are inside b

if ((isPointInsideRect(a.left, a.top, b)) or

(isPointInsideRect(a.left, a.bottom, b)) or

(isPointInsideRect(a.right, a.top, b)) or

(isPointInsideRect(a.right, a.bottom, b))):

return True

return False

def isPointInsideRect(x, y, rect):

if (x > rect.left) and (x < rect.right) and (y > rect.top) and (y < rect.bottom):

return True

else:

return False

# set up pygame

pygame.init()

# set up the window

WINDOWWIDTH = 600

WINDOWHEIGHT = 600

windowSurface = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT), 0, 32)

pygame.display.set_caption('Rectangles')

# set up the colors

BLACK = (0, 0, 0)

WHITE = (255, 255, 255)

RED = (255, 0, 0)

GREEN = (0, 255, 0)

BLUE = (0, 0, 255)

from random import choice

foo = [BLACK, RED, GREEN, BLUE]

# draw the background

windowSurface.fill(WHITE)

print('Please enter a number:')

number = input()

x = 0

array = []

for i in array:

while int(number) > x:

x = x+1

x1 = random.randint(1, 400)

y1 = random.randint(1, 400)

x2 = random.randint(1, 400)

y2 = random.randint(1, 400)

x3 = random.randint(1, 400)

y3 = random.randint(1, 400)

x4 = random.randint(1, 400)

y4 = random.randint(1, 400)

box = pygame.draw.rect(windowSurface,random.choice(foo), (x1, y1, x2, y2))

if doRectsOverlap(box, box) == False:

box

else:

x = x-1

# draw the window onto the screen

pygame.display.update()

任何帮助都将不胜感激。谢谢您!在

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值