通过在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,550))
    point3 = Pos(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),(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,True,points,1)
    pygame.display.flip()

结果是,有时可以正确地填充三角形而没有任何问题。有时三角形是填充的,但也有一些未填充的像素,有时三角形根本不填充。感谢您的帮助。

解决方法

一个明显的错误是线

for x in range(p1.x,p3.x+1):

如果您未在函数range中指定 step 参数,则 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),(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,p3 = tri[1],tri[0],tri[2]
    elif len20 > len01 and len20 > len12:
        p1,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)+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,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,1)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...