python - 在pygame中画线填充三角形
问题描述
我的最终目标是填充构成 3d 网格的三角形。为了弄清楚如何做到这一点,我决定生成一个随机三角形并填充它。在后来的一些文章和 youtube 视频之后,我发现它通常是通过计算三角形不同线之间的 x 和 y 并填充这些来完成的像素。但在我的情况下,pygame 已经有一个画线功能,所以我可以简单地计算构成最长边的 2 个点之间的所有点,并从这些点画线到第三个点,或者我是这么认为的。下面是复制粘贴代码:
import pygame
from random import randint
from math import sqrt
pygame.init()
D = pygame.display.set_mode((1200, 600))
class Pos:
def __init__(self, x, y):
self.x = x
self.y = y
def genTriangle():
point1 = Pos(randint(50, 1000), randint(50, 550))
point2 = Pos(randint(50, 1000), randint(50, 550))
point3 = Pos(randint(50, 1000), randint(50, 550))
return [point1, point2, point3]
tri = genTriangle()
def getLength(p1, p2):
x = p1.x - p2.x
y = p1.y - p2.y
return sqrt(x**2 + y**2)
def fill(tri):
len01 = getLength(tri[0], tri[1]) #Length between tri[0] and tri[1]
len12 = getLength(tri[1], tri[2]) #Length between tri[1] and tri[2]
len20 = getLength(tri[2], tri[0]) #Length between tri[2] and tri[0]
# Assinging the points making up the longest side to p1 and p3
# and the third point to p2 becasue that is how the calculation is carried
# out later (i feel like this is now the best way to figure out the longest side
# so any help with this would be appreciated as well)
if len01 > len12 and len20:
p1 = tri[0]
p2 = tri[2]
p3 = tri[1]
elif len12 > len20 and len01:
p1 = tri[1]
p2 = tri[0]
p3 = tri[2]
elif len20 > len01 and len12:
p1 = tri[2]
p2 = tri[1]
p3 = tri[0]
# calculates all the points along the longest side of the triangle
# and draws lines from those points to the third point of the triangle
for x in range(p1.x, p3.x+1):
m = ((p1.y - p3.y)/ (p1.x - p3.x)) # slope
y = m*(x - p3.x) + p3.y # rearranged point-slope formula
pygame.draw.line(D, (255, 0, 0), (int(x), int(y)), (p2.x, p2.y), 1)
while True:
pygame.event.get()
D.fill((255, 255, 255))
fill(tri)
points = [(point.x, point.y) for point in tri]
pygame.draw.lines(D, (0, 0, 0), True, points, 1)
pygame.display.flip()
结果是有时三角形被正确填充而完全没有问题。有时三角形是填充的,但也有一些未填充的像素,有时三角形根本没有填充。感谢您的帮助。
解决方案
一个明显的错误是这条线
for x in range(p1.x, p3.x+1):
如果您未在函数中指定step参数range
,则start必须小于stop。计算最小和最大坐标:
for x in range(min(p1.x, p3.x), max(p1.x, p3.x)+1):
m = ((p1.y - p3.y)/ (p1.x - p3.x)) # slope
y = m*(x - p3.x) + p3.y # rearranged point-slope formula
pygame.draw.line(D, (255, 0, 0), (int(x), int(y)), (p2.x, p2.y), 1)
此外,该条件if len01 > len12 and len20:
不会按照您的预期进行。它必须是if len01 > len12 and len01 > len20:
。
请参阅如何针对一个值测试多个变量?.
你必须评估 if abs(p3.x-p1.x) > abs(p3.y-p1.y)
。如果条件是True
沿 x 轴迭代,否则沿 y 轴迭代:
def fill(tri):
len01 = getLength(tri[0], tri[1]) #Length between tri[0] and tri[1]
len12 = getLength(tri[1], tri[2]) #Length between tri[1] and tri[2]
len20 = getLength(tri[2], tri[0]) #Length between tri[2] and tri[0]
# Assinging the points making up the longest side to p1 and p3
# and the third point to p2 becasue that is how the calculation is carried
# out later (i feel like this is now the best way to figure out the longest side
# so any help with this would be appreciated as well)
if len01 > len12 and len01 > len20:
p1, p2, p3 = tri[0], tri[2], tri[1]
elif len12 > len20 and len12 > len01:
p1, p2, p3 = tri[1], tri[0], tri[2]
elif len20 > len01 and len20 > len12:
p1, p2, p3 = tri[2], tri[1], tri[0]
# calculates all the points along the longest side of the triangle
# and draws lines from those points to the third point of the triangle
if abs(p3.x-p1.x) > abs(p3.y-p1.y):
for x in range(min(p1.x, p3.x), max(p1.x, p3.x)+1):
m = ((p1.y - p3.y)/ (p1.x - p3.x)) # slope
y = m*(x - p3.x) + p3.y # rearranged point-slope formula
pygame.draw.line(D, (255, 0, 0), (int(x), int(y)), (p2.x, p2.y), 1)
else:
for y in range(min(p1.y, p3.y), max(p1.y, p3.y)+1):
m = ((p1.x - p3.x)/ (p1.y - p3.y)) # slope
x = m*(y - p3.y) + p3.x # rearranged point-slope formula
pygame.draw.line(D, (255, 0, 0), (int(x), int(y)), (p2.x, p2.y), 1)
推荐阅读
- reactjs - 对表行做出反应以绑定(this)单击事件处理程序
- google-cloud-platform - 如何从 Dataprep 获取列列表?
- fortran - gfortran:符号“网格”没有隐式类型
- c# - 如果我在实体框架代码优先方法中从静态方法保存数据,为什么记录没有保存在数据库中?
- java - 删除下游 ChangeLog 对象 KafkaStreams
- c++ - DLLImport C++ To .NET - char*(指向字符串的指针)
- julia - 如何在 MacOS 上为 Julia 测试做 PackageSpec()?
- python-3.x - Python - 如果打印特定单词,我如何附加数据?
- conemu - cd 回到 Cmder 中的先前位置
- excel - 允许基于数据验证的非空白单元格