在 pygame 中画线填充三角形

Filling in a triangle by drawing lines in pygame

我的最终目标是填充构成 3d 网格的三角形。为了弄清楚如何做到这一点,我决定生成一个随机三角形并填充它。后来看了一些文章和 youtube 视频后,我发现这通常是通过计算三角形不同线之间的 x 和 y 并填充它们来完成的像素。但在我的例子中 pygame 已经有一个画线函数,所以我可以简单地计算构成最长边的两点之间的所有点,然后从这些点到第三点画线,或者我是这么想的。这是复制粘贴代码:

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):

如果您没有在函数 range 中指定 step 参数,则 start 必须小于停止。计算最小和最大坐标:

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:.
参见 How to test multiple variables against a value?


你必须评估是否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)