四边形密铺平面

任意四边形可以密铺整个二维平面,这里将给出其代码实现.

void CTileQuadrangleGridDrawer::BuildTileQuadrangle(Vector3* v)
{
    Vector2 vDis01 = m_quadPositions[0] - m_quadPositions[1];
    Vector2 vDis32 = m_quadPositions[3] - m_quadPositions[2];
    Vector2 vDis03 = m_quadPositions[0] - m_quadPositions[3];
    Vector2 vDis12 = m_quadPositions[1] - m_quadPositions[2];

    Vector2 vExtend = ((vDis01 + vDis32)*((float)m_uiCol) + (vDis03 + vDis12)*((float)m_uiRow))*0.5f;

    Vector2 vStart = -vExtend*0.5f;

    Vector2 vCurrentRow = vStart;
    Vector2 vCurrentCol;

    unsigned int index;

    for (unsigned int i = 0; i <= m_uiRow; i++)
    {
        vCurrentCol = vCurrentRow;

        for (Yuint j = 0; j <= m_uiCol; j++)
        {
            index = i*(m_uiCol + 1) + j;

            v[index].x = vCurrentCol.x;
            v[index].y = vCurrentCol.y;
            v[index].z = 0.0f;

            vCurrentCol += ((i+j)&1) ? vDis01 : vDis32;
        }

        vCurrentRow += (i&1) ? vDis03 : vDis12;
    }
}

代码中m_quadPositions表示四边形的四个顶点.v为计算输出的顶点Mesh.

在窗体右下角有个四边形,鼠标拖动四边形的顶点,可以改变四边形的形状,从而改变平铺网格.

软件下载地址:https://files.cnblogs.com/files/WhyEngine/TileQuadrangle.7z

这是个3D程序,鼠标右键的拖动可以改变视角.
X用于恢复为默认视角.
F11用于全屏切换.

 

posted on 2015-12-04 20:02  叶飞影  阅读(1749)  评论(0编辑  收藏  举报