开发者

Change Screen Origin Of PrintEventArgs.Graphics in C#

I been messing around with drawing 3D Objects in C# using PaintEventArgs.Graphics and the following code

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);
}

The issue comes when I try to manipulate the cube drawn using Matrix.Rotate and nested for loops.

For example I can make a circle with the following code.

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

private void Form1_Paint(o开发者_JAVA技巧bject sender, PaintEventArgs e)
{
    for (int x = 0; x < 360; ++x)
    {
        rotateCube(e, x);
        Thread.Sleep(3);
        drawCube(e);
    }
}

When I use that code to draw a circle I only get the bottom right part of the circle because the origin of the screen (and center of the circle) is 0,0 on a co-ordinate system or the top left of the monitor. My question is... is there a way to move the origin of the screen or circle to perhaps the middle of the screen?


Multiply your rotation matrix by a translation matrix.

Keep in mind that moving the camera by (100px,100px) is equivalent to moving your model by (-100px,-100px).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜