C# 获取某个平面上多边形的高度和宽度

C# 获取某个平面上多边形的高度和宽度,c#,3d,xna,C#,3d,Xna,我目前正在使用以下方法将纹理应用于由TriangleList形成的多边形 public static VertexPositionColorTexture[] TextureMapping(VertexPositionColorTexture[] vertices, float xScale, float yScale) { bool initialized = false; float x, y; float lX = 0, hX =

我目前正在使用以下方法将纹理应用于由TriangleList形成的多边形

public static VertexPositionColorTexture[] TextureMapping(VertexPositionColorTexture[] vertices, float xScale, float yScale)
    {
        bool initialized = false;

        float x, y;
        float lX = 0, hX = 0, lY = 0, hY = 0;

        for (int i = 0; i < vertices.Length; i++)
        {

            x = vertices[i].Position.X;
            y = vertices[i].Position.Y;

            if (!initialized)
            {
                hX = x;
                lX = x;
                hX = y;
                hY = y;

                initialized = true;
            }
            else
            {
                if (x > hX)
                {
                    hX = x;
                }
                else if (x < lX)
                {
                    lX = x;
                }

                if (y > hY)
                {
                    hY = y;
                }
                else if (y < lY)
                {
                    lY = y;
                }
            }


        }

        float width = (Math.Abs(lX) + Math.Abs(hX)) / xScale;
        float height = (Math.Abs(lY) + Math.Abs(hY)) / yScale;

        for (int i = 0; i < vertices.Length; i++)
        {
            vertices[i].TextureCoordinate.X = vertices[i].Position.X / width;
            vertices[i].TextureCoordinate.Y = vertices[i].Position.Y / height;
        }

        return vertices;
公共静态VertexPositionColorTexture[]纹理映射(VertexPositionColorTexture[]顶点,浮点xScale,浮点yScale)
{
bool初始化=false;
浮动x,y;
浮点数lX=0,hX=0,lY=0,hY=0;
对于(int i=0;ihX)
{
hX=x;
}
else if(xhY)
{
hY=y;
}
否则如果(y

这目前适用于所有点的Z=0的多边形(例如:(0,0,0)(0,10,0)(10,10,0)(10,0,0)),但不适用于沿Z旋转或不平坦的多边形(例如(0,0,0)(0,0,10)(0,10,10)(0,10,0))。我得到的唯一解决方案是得到多边形所在的平面(它将始终是平坦的)然后以某种方式旋转或平移上述方法中的顶点,将其展平到xy线,以便确定正确的高度和宽度。有人给我指出正确的方向,或提出其他建议吗?

通过将多边形重新写入并旋转到z平面,我自己解决了这个问题。

位于xz平面的多边形可能是y两种情况中不起作用的一种(另一种是位于yz平面上的多边形)。这是因为在其中一个方向上没有范围。因此,您不能真正定义“向上”和“向右”。您想如何在多边形上应用纹理?它毕竟可以以某种方式旋转。您可以检查宽度或高度是否为0,并使用z范围。顺便说一句,宽度和高度的计算看起来有点奇怪。它不应该是
hX-lX