使用四边形的重心坐标

发布于 2024-09-07 22:09:37 字数 145 浏览 1 评论 0原文

你们中的一些人知道如何使用重心填充二维四边形 坐标?目前,我将四边形分成2个三角形, 但这种方式效率低下,因为我必须迭代第二个 边界框重复先前填充的像素(通过 例如,为了填充第二个三角形,我遍历了第一个三角形 属于由第二个三角形形成的边界框) 谢谢埃斯米特

some of you know how fill a quads in 2D using barycentric
coordinates?At the present, I'm splitting the quads into 2 triangles,
but that way is inefficient because I have to iterate over the second
bounding box which repeats pixel that were filled previously (by
example, to fill the 2nd triangle I traversed the 1st triangle that
belongs at bounding box formed by 2nd triangle)
Thanks

esmitt

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

捂风挽笑 2024-09-14 22:09:37

这是一个 python 示例,应该就是您正在寻找的。您可能知道,二维四边形没有唯一定义的重心坐标(三维四面体有重心坐标,但这是另一回事)。

import sys

def fill(xa, ya, xb, yb, xc, yc, xd, yd):
    abx = yb - ya
    aby = xa - xb
    kab = - (xa*abx + ya*aby)

    bcx = yc - yb
    bcy = xb - xc
    kbc = - (xb*bcx + yb*bcy)

    cdx = yd - yc
    cdy = xc - xd
    kcd = - (xc*cdx + yc*cdy)

    dax = ya - yd
    day = xd - xa
    kda = - (xd*dax + yd*day)

    for y in xrange(25):
        for x in xrange(79):
            if (x*abx + y*aby + kab >= 0 and
                x*bcx + y*bcy + kbc >= 0 and
                x*cdx + y*cdy + kcd >= 0 and
                x*dax + y*day + kda >= 0):
                sys.stdout.write('+')
            else:
                sys.stdout.write('-')
        sys.stdout.write('\n')

fill( 10, 5,
      6, 22,
      60, 17,
      70, 9 )

基本上我正在计算每个边缘的线系数,然后检查该点是否位于每个边缘的正确一侧。线系数未标准化,因为如果您只想进行命中/未命中测试,则不需要(您将仅检查符号,而不检查 xnx + yny + nk 的大小)。

请注意,这种方法需要凸向四边形......

Here is a python example that should be what you're looking for. As you probably know there are no uniquely defined barycentric coordinates for quads in two dimension (there are barycentric coordinates for tetrahedra in three dimensions, but that's another thing).

import sys

def fill(xa, ya, xb, yb, xc, yc, xd, yd):
    abx = yb - ya
    aby = xa - xb
    kab = - (xa*abx + ya*aby)

    bcx = yc - yb
    bcy = xb - xc
    kbc = - (xb*bcx + yb*bcy)

    cdx = yd - yc
    cdy = xc - xd
    kcd = - (xc*cdx + yc*cdy)

    dax = ya - yd
    day = xd - xa
    kda = - (xd*dax + yd*day)

    for y in xrange(25):
        for x in xrange(79):
            if (x*abx + y*aby + kab >= 0 and
                x*bcx + y*bcy + kbc >= 0 and
                x*cdx + y*cdy + kcd >= 0 and
                x*dax + y*day + kda >= 0):
                sys.stdout.write('+')
            else:
                sys.stdout.write('-')
        sys.stdout.write('\n')

fill( 10, 5,
      6, 22,
      60, 17,
      70, 9 )

Basically I'm computing the line coefficients for every edge and then check if the point is on the correct side of each of them. The line coefficients are not normalized because if you only want an hit/miss test that is not needed (you will only check the sign and not the magnitude of xnx + yny + nk).

Note that this approach requires convex oriented quads ...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文