绘制顺时针三角形重新排序点

匿名先生

我正在用c ++制作光线跟踪器,下载对象时遇到了一个问题,因为它只能渲染某些三角形,因为它们是逆时针绘制的。

我认为这是一个问题,因为它会再次出现。但是林对您实际上应该如何仅通过顶点知道感到困惑。如果有人可以帮助我创建一个函数,以便为三角形的每个点取三个向量,并对它们重新排序,以便可以顺时针绘制。

谢谢。

Reblochon面膜

您可以计算由三个点定义的三角形形成的有符号区域-这等效于代表边缘的向量的2D“叉积”(有时称为perp积):

这是python中的一个简单的实现,显示了计算结果;您可以在闲暇时将其转录为C ++。

从那里开始,交换三角形中两个点的位置将使转弯从ccw变为cw,反之亦然

class CollinearpointsError(ValueError):
    pass


def ccw(triangle):
    """returns True if the triangle points are counter clock wise,
    False otherwise, and raises a CollinearpointsError when the 
    three points are collinear 
    """
    A, B, C = triangle
    ax, ay = A
    bx, by = B
    cx, cy = C
    
    AB = (bx - ax, by - ay)
    AC = (cx - ax, cy - ay)

    a, b = AB
    c, d = AC
    
    signed_area_x2 = a * d - b * c
    if signed_area == 0:
        raise CollinearpointsError('the three points are collinear')

    return (a * d - b * c) > 0


def cw(triangle):
    """returns True if the triangle points are clock wise,
    False otherwise, and raises a CollinearpointsError when the 
    three points are collinear 
    """
    return not ccw(triangle)


A = (0, 0)
B = (0, 1)
C = (1, 0)

triangle = (A, B, C)
print(cw(triangle))

triangle = (A, C, B)
print(cw(triangle))

triangle = (A, B, (0, 2))
print(cw(triangle))

输出:

True
False
CollinearpointsError: the three points are collinear

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章