改变PrintEventArgs的屏幕原点.c#中的图形

本文关键字:图形 屏幕 PrintEventArgs 改变 原点 | 更新日期: 2023-09-27 18:10:31

我一直在c#中使用PaintEventArgs绘制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);
}

问题来了,当我试图操纵使用矩阵绘制的立方体。旋转和嵌套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或显示器的左上方。我的问题是……有没有一种方法可以将屏幕或圆圈的原点移动到屏幕的中间?

改变PrintEventArgs的屏幕原点.c#中的图形

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

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