在C#中更改PrintEventArgs.Graphics的屏幕原点

时间:2011-10-03 17:46:14

标签: c# matrix rotation screen

我一直在使用PaintEventArgs.Graphics和以下代码在C#中绘制3D对象?

public void findPoints()
{
    xp1 = (p1x * 1000) / (p1z + 1000);
    xp2 = (p2x * 1000) / (p2z + 1000);
    xp3 = (p3x * 1000) / (p3z + 1000);
    xp4 = (p4x * 1000) / (p4z + 1000);
    yp1 = (p1y * 1000) / (p1z + 1000);
    yp2 = (p2y * 1000) / (p2z + 1000);
    yp3 = (p3y * 1000) / (p3z + 1000);
    yp4 = (p4y * 1000) / (p4z + 1000);
    xp5 = (p5x * 1000) / (p5z + 1000);
    xp6 = (p6x * 1000) / (p6z + 1000);
    xp7 = (p7x * 1000) / (p7z + 1000);
    xp8 = (p8x * 1000) / (p8z + 1000);
    yp5 = (p5y * 1000) / (p5z + 1000);
    yp6 = (p6y * 1000) / (p6z + 1000);
    yp7 = (p7y * 1000) / (p7z + 1000);
    yp8 = (p8y * 1000) / (p8z + 1000);
}

private void drawCube(PaintEventArgs e)
{
    /*
     * Bottom left = 1
     * Bottom right = 2
     * Top right = 3
     * Top left = 4
     * Back Top left = 5
     * Back Top right = 6
     * Back bottom right = 7
     * Back bottom left = 8
     */
    findPoints();
    Graphics g = e.Graphics;
    g.DrawLine(pen, xp1, yp1, xp2, yp2);
    g.DrawLine(pen, xp2, yp2, xp3, yp3);
    g.DrawLine(pen, xp3, yp3, xp4, yp4);
    g.DrawLine(pen, xp4, yp4, xp1, yp1);
    g.DrawLine(pen, xp1, yp1, xp8, yp8);
    g.DrawLine(pen, xp8, yp8, xp7, yp7);
    g.DrawLine(pen, xp7, yp7, xp2, yp2);
    g.DrawLine(pen, xp2, yp2, xp7, yp7);
    g.DrawLine(pen, xp7, yp7, xp6, yp6);
    g.DrawLine(pen, xp6, yp6, xp3, yp3);
    g.DrawLine(pen, xp3, yp3, xp6, yp6);
    g.DrawLine(pen, xp6, yp6, xp5, yp5);
    g.DrawLine(pen, xp5, yp5, xp4, yp4);
    g.DrawLine(pen, xp4, yp4, xp5, yp5);
    g.DrawLine(pen, xp5, yp5, xp8, yp8);
}

当我尝试操作使用Matrix.Rotate绘制的多维数据集并嵌套for循环时出现问题。

例如,我可以使用以下代码制作一个圆圈。

private void rotateCube(PaintEventArgs e, int value)
{
    Matrix myMatrix = new Matrix();
    myMatrix.Rotate(value, MatrixOrder.Append);
    e.Graphics.Transform = myMatrix;
}

private void Form1_Paint(object sender, PaintEventArgs e)
{
    for (int x = 0; x < 360; ++x)
    {
        rotateCube(e, x);
        Thread.Sleep(3);
        drawCube(e);
    }
}

当我使用该代码绘制圆形时,我只得到圆圈的右下角部分,因为在坐标系统或左上方的屏幕原点(和圆心)是0,0监视器。我的问题是......有没有办法将屏幕或圆圈的原点移动到屏幕中间?

1 个答案:

答案 0 :(得分:1)

将旋转矩阵乘以平移矩阵。

请记住,将相机移动(100px,100px)相当于将模型移动(-100px,-100px)。