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

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

问题描述

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

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

推荐答案

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

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 )

基本上我在计算线路每个边缘的系数,然后检查该点是否在每个边的正确一侧。线系数没有标准化,因为如果你只想要一个不需要的命中/未命中测试(你只需要检查符号而不是x nx + y ny + nk的大小)。

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天全站免登陆