使用四边形的重心坐标 [英] barycentric coordinates using quads

查看:53
本文介绍了使用四边形的重心坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

艾米特

解决方案

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

导入系统定义填充(xa,ya,xb,yb,xc,yc,xd,yd):abx = yb - yaaby = xa - xbkab = - (xa*abx + ya*aby)bcx = yc - ybbcy = xb - xckbc = - (xb*bcx + yb*bcy)cdx = yd - yccdy = xc - xdkcd = - (xc*cdx + yc*cdy)dax = ya - yd天 = xd - xakda = - (xd*dax + yd*day)对于 xrange(25) 中的 y:对于 x 范围内的 x(79):如果 (x*abx + y*aby + kab >= 0 和x*bcx + y*bcy + kbc >= 0 和x*cdx + y*cdy + kcd >= 0 和x*dax + y*day + kda >= 0):sys.stdout.write('+')别的:sys.stdout.write('-')sys.stdout.write('
')填充( 10, 5,6, 22,60, 17,70, 9 )

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

请注意,此方法需要面向凸面的四边形...

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

解决方案

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('
')

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 ...

这篇关于使用四边形的重心坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆