绘制顺时针三角形重新排序点
Drawing Clockwise Triangles Reordering Points
我正在用 C++ 制作光线追踪器,在下载对象时遇到了一个问题,因为它只会渲染一些三角形,因为它们是逆时针绘制的。
我查到这是一个问题,因为它会背对着。但是我对你实际上应该如何仅通过顶点知道而感到困惑。如果有人能帮我做一个函数,为三角形的每个点取三个向量,并对它们重新排序,以便顺时针绘制。
谢谢。
您可以计算由三个点定义的三角形形成的带符号区域 - 这相当于表示边的向量的二维 'cross product'(有时称为 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
我正在用 C++ 制作光线追踪器,在下载对象时遇到了一个问题,因为它只会渲染一些三角形,因为它们是逆时针绘制的。
我查到这是一个问题,因为它会背对着。但是我对你实际上应该如何仅通过顶点知道而感到困惑。如果有人能帮我做一个函数,为三角形的每个点取三个向量,并对它们重新排序,以便顺时针绘制。
谢谢。
您可以计算由三个点定义的三角形形成的带符号区域 - 这相当于表示边的向量的二维 'cross product'(有时称为 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