Beispiel #1
0
 public Cube(int Width, int Height, int Depth, Math3D.Point3D origin)
 {
     width      = Width;
     height     = Height;
     depth      = Depth;
     cubeOrigin = origin;
 }
Beispiel #2
0
 public Cube(int Width, int Height, int Depth)
 {
     width      = Width;
     height     = Height;
     depth      = Depth;
     cubeOrigin = new Math3D.Point3D(width / 2, height / 2, depth / 2);
 }
Beispiel #3
0
 public Cube(int side, Math3D.Point3D origin)
 {
     width      = side;
     height     = side;
     depth      = side;
     cubeOrigin = origin;
 }
Beispiel #4
0
 public Cube(int side)
 {
     width      = side;
     height     = side;
     depth      = side;
     cubeOrigin = new Math3D.Point3D(width / 2, height / 2, depth / 2);
 }
Beispiel #5
0
        public static Math3D.Point3D[] fillCubeVertices(int width, int height, int depth)
        {
            Math3D.Point3D[] verts = new Math3D.Point3D[24];

            //front face
            //force points to the middle
            //verts[0] = new Math3D.Point3D(0, 0, 0);
            verts[0] = new Math3D.Point3D(width / 2, 0, depth / 2);
            verts[1] = new Math3D.Point3D(0, height, 0);
            verts[2] = new Math3D.Point3D(width, height, 0);
            verts[3] = new Math3D.Point3D(width, 0, 0);

            //back face
            verts[4] = new Math3D.Point3D(0, 0, depth);
            verts[5] = new Math3D.Point3D(0, height, depth);
            verts[6] = new Math3D.Point3D(width, height, depth);
            verts[7] = new Math3D.Point3D(width, 0, depth);

            //left face
            verts[8]  = new Math3D.Point3D(0, 0, 0);
            verts[9]  = new Math3D.Point3D(0, 0, depth);
            verts[10] = new Math3D.Point3D(0, height, depth);
            verts[11] = new Math3D.Point3D(0, height, 0);

            //right face
            verts[12] = new Math3D.Point3D(width, 0, 0);
            verts[13] = new Math3D.Point3D(width, 0, depth);
            verts[14] = new Math3D.Point3D(width, height, depth);
            verts[15] = new Math3D.Point3D(width, height, 0);

            //top face
            verts[16] = new Math3D.Point3D(0, height, 0);
            verts[17] = new Math3D.Point3D(0, height, depth);
            verts[18] = new Math3D.Point3D(width, height, depth);
            verts[19] = new Math3D.Point3D(width, height, 0);

            //bottom face
            verts[20] = new Math3D.Point3D(0, 0, 0);
            verts[21] = new Math3D.Point3D(0, 0, depth);
            verts[22] = new Math3D.Point3D(width, 0, depth);
            verts[23] = new Math3D.Point3D(width, 0, 0);
            //make magenta!

            return(verts);
        }
Beispiel #6
0
        public Bitmap drawCube(Point drawOrigin)
        {
            //FRONT FACE
            //Top Left - 7
            //Top Right - 4
            //Bottom Left - 6
            //Bottom Right - 5

            //Vars
            PointF[] point3D = new PointF[24]; //Will be actual 2D drawing points
            Point tmpOrigin = new Point(0, 0);

            Math3D.Point3D point0 = new Math3D.Point3D(0, 0, 0); //Used for reference

            //Zoom factor is set with the monitor width to keep the cube from being distorted
            double zoom = (double)Screen.PrimaryScreen.Bounds.Width / 1.5;

            //Set up the cube
            Math3D.Point3D[] cubePoints = fillCubeVertices(width, height, depth);

            //Calculate the camera Z position to stay constant despite rotation
            Math3D.Point3D anchorPoint = (Math3D.Point3D)cubePoints[4]; //anchor point
            double cameraZ = -(((anchorPoint.X - cubeOrigin.X) * zoom) / cubeOrigin.X) + anchorPoint.Z;
            camera1.Position = new Math3D.Point3D(cubeOrigin.X, cubeOrigin.Y, cameraZ);

            //Apply Rotations, moving the cube to a corner then back to middle
            cubePoints = Math3D.Translate(cubePoints, cubeOrigin, point0);
            cubePoints = Math3D.RotateX(cubePoints, xRotation); //The order of these
            cubePoints = Math3D.RotateY(cubePoints, yRotation); //rotations is the source
            cubePoints = Math3D.RotateZ(cubePoints, zRotation); //of Gimbal Lock
            cubePoints = Math3D.Translate(cubePoints, point0, cubeOrigin);

            //Convert 3D Points to 2D
            Math3D.Point3D vec;
            for (int i = 0; i < point3D.Length; i++)
            {
                vec = cubePoints[i];
                if (vec.Z - camera1.Position.Z >= 0)
                {
                    point3D[i].X = (int)((double)-(vec.X - camera1.Position.X) / (-0.1f) * zoom) + drawOrigin.X;
                    point3D[i].Y = (int)((double)(vec.Y - camera1.Position.Y) / (-0.1f) * zoom) + drawOrigin.Y;
                }
                else
                {
                    tmpOrigin.X = (int)((double)(cubeOrigin.X - camera1.Position.X) / (double)(cubeOrigin.Z - camera1.Position.Z) * zoom) + drawOrigin.X;
                    tmpOrigin.Y = (int)((double)-(cubeOrigin.Y - camera1.Position.Y) / (double)(cubeOrigin.Z - camera1.Position.Z) * zoom) + drawOrigin.Y;

                    point3D[i].X = (float)((vec.X - camera1.Position.X) / (vec.Z - camera1.Position.Z) * zoom + drawOrigin.X);
                    point3D[i].Y = (float)(-(vec.Y - camera1.Position.Y) / (vec.Z - camera1.Position.Z) * zoom + drawOrigin.Y);

                    point3D[i].X = (int)point3D[i].X;
                    point3D[i].Y = (int)point3D[i].Y;
                }
            }

            //Now to plot out the points
            Rectangle bounds = getBounds(point3D);
            bounds.Width += drawOrigin.X;
            bounds.Height += drawOrigin.Y;

            Bitmap tmpBmp = new Bitmap(bounds.Width, bounds.Height);
            Graphics g = Graphics.FromImage(tmpBmp);

            //Back Face
            g.DrawLine(Pens.Black, point3D[0], point3D[1]);
            g.DrawLine(Pens.Black, point3D[1], point3D[2]);
            g.DrawLine(Pens.Black, point3D[2], point3D[3]);
            g.DrawLine(Pens.Black, point3D[3], point3D[0]);

            //Front Face
            g.DrawLine(Pens.Black, point3D[4], point3D[5]);
            g.DrawLine(Pens.Black, point3D[5], point3D[6]);
            g.DrawLine(Pens.Black, point3D[6], point3D[7]);
            g.DrawLine(Pens.Black, point3D[7], point3D[4]);

            //Right Face
            g.DrawLine(Pens.Black, point3D[8], point3D[9]);
            g.DrawLine(Pens.Black, point3D[9], point3D[10]);
            g.DrawLine(Pens.Black, point3D[10], point3D[11]);
            g.DrawLine(Pens.Black, point3D[11], point3D[8]);

            //Left Face
            g.DrawLine(Pens.Black, point3D[12], point3D[13]);
            g.DrawLine(Pens.Black, point3D[13], point3D[14]);
            g.DrawLine(Pens.Black, point3D[14], point3D[15]);
            g.DrawLine(Pens.Black, point3D[15], point3D[12]);

            //Bottom Face
            g.DrawLine(Pens.Black, point3D[16], point3D[17]);
            g.DrawLine(Pens.Black, point3D[17], point3D[18]);
            g.DrawLine(Pens.Black, point3D[18], point3D[19]);
            g.DrawLine(Pens.Black, point3D[19], point3D[16]);

            //Top Face
            g.DrawLine(Pens.Black, point3D[20], point3D[21]);
            g.DrawLine(Pens.Black, point3D[21], point3D[22]);
            g.DrawLine(Pens.Black, point3D[22], point3D[23]);
            g.DrawLine(Pens.Black, point3D[23], point3D[20]);

            g.Dispose(); //Clean-up

            return tmpBmp;
        }
Beispiel #7
0
        public static Math3D.Point3D[] fillCubeVertices(int width, int height, int depth)
        {
            Math3D.Point3D[] verts = new Math3D.Point3D[24];

            //front face
            verts[0] = new Math3D.Point3D(0, 0, 0);
            verts[1] = new Math3D.Point3D(0, height, 0);
            verts[2] = new Math3D.Point3D(width, height, 0);
            verts[3] = new Math3D.Point3D(width, 0, 0);

            //back face
            verts[4] = new Math3D.Point3D(0, 0, depth);
            verts[5] = new Math3D.Point3D(0, height, depth);
            verts[6] = new Math3D.Point3D(width, height, depth);
            verts[7] = new Math3D.Point3D(width, 0, depth);

            //left face
            verts[8] = new Math3D.Point3D(0, 0, 0);
            verts[9] = new Math3D.Point3D(0, 0, depth);
            verts[10] = new Math3D.Point3D(0, height, depth);
            verts[11] = new Math3D.Point3D(0, height, 0);

            //right face
            verts[12] = new Math3D.Point3D(width, 0, 0);
            verts[13] = new Math3D.Point3D(width, 0, depth);
            verts[14] = new Math3D.Point3D(width, height, depth);
            verts[15] = new Math3D.Point3D(width, height, 0);

            //top face
            verts[16] = new Math3D.Point3D(0, height, 0);
            verts[17] = new Math3D.Point3D(0, height, depth);
            verts[18] = new Math3D.Point3D(width, height, depth);
            verts[19] = new Math3D.Point3D(width, height, 0);

            //bottom face
            verts[20] = new Math3D.Point3D(0, 0, 0);
            verts[21] = new Math3D.Point3D(0, 0, depth);
            verts[22] = new Math3D.Point3D(width, 0, depth);
            verts[23] = new Math3D.Point3D(width, 0, 0);

            return verts;
        }
Beispiel #8
0
 public Cube(int Width, int Height, int Depth, Math3D.Point3D origin)
 {
     width = Width;
     height = Height;
     depth = Depth;
     cubeOrigin = origin;
 }
Beispiel #9
0
 public Cube(int Width, int Height, int Depth)
 {
     width = Width;
     height = Height;
     depth = Depth;
     cubeOrigin = new Math3D.Point3D(width / 2, height / 2, depth / 2);
 }
Beispiel #10
0
 public Cube(int side, Math3D.Point3D origin)
 {
     width = side;
     height = side;
     depth = side;
     cubeOrigin = origin;
 }
Beispiel #11
0
 public Cube(int side)
 {
     width = side;
     height = side;
     depth = side;
     cubeOrigin = new Math3D.Point3D(width / 2, height / 2, depth / 2);
 }
Beispiel #12
0
        public Bitmap drawCube(Point drawOrigin)
        {
            //FRONT FACE
            //Top Left - 7
            //Top Right - 4
            //Bottom Left - 6
            //Bottom Right - 5

            //Vars
            PointF[] point3D   = new PointF[24]; //Will be actual 2D drawing points
            Point    tmpOrigin = new Point(0, 0);

            Math3D.Point3D point0 = new Math3D.Point3D(0, 0, 0); //Used for reference

            //Zoom factor is set with the monitor width to keep the cube from being distorted
            double zoom = (double)Screen.PrimaryScreen.Bounds.Width / 1.5;

            //Set up the cube
            Math3D.Point3D[] cubePoints = fillCubeVertices(width, height, depth);

            //Calculate the camera Z position to stay constant despite rotation
            Math3D.Point3D anchorPoint = (Math3D.Point3D)cubePoints[4]; //anchor point
            double         cameraZ     = -(((anchorPoint.X - cubeOrigin.X) * zoom) / cubeOrigin.X) + anchorPoint.Z;

            camera1.Position = new Math3D.Point3D(cubeOrigin.X, cubeOrigin.Y, cameraZ);

            //Apply Rotations, moving the cube to a corner then back to middle
            cubePoints = Math3D.Translate(cubePoints, cubeOrigin, point0);
            cubePoints = Math3D.RotateX(cubePoints, xRotation); //The order of these
            cubePoints = Math3D.RotateY(cubePoints, yRotation); //rotations is the source
            cubePoints = Math3D.RotateZ(cubePoints, zRotation); //of Gimbal Lock
            cubePoints = Math3D.Translate(cubePoints, point0, cubeOrigin);

            //Convert 3D Points to 2D
            Math3D.Point3D vec;
            for (int i = 0; i < point3D.Length; i++)
            {
                vec = cubePoints[i];
                if (vec.Z - camera1.Position.Z >= 0)
                {
                    point3D[i].X = (int)((double)-(vec.X - camera1.Position.X) / (-0.1f) * zoom) + drawOrigin.X;
                    point3D[i].Y = (int)((double)(vec.Y - camera1.Position.Y) / (-0.1f) * zoom) + drawOrigin.Y;
                }
                else
                {
                    tmpOrigin.X = (int)((double)(cubeOrigin.X - camera1.Position.X) / (double)(cubeOrigin.Z - camera1.Position.Z) * zoom) + drawOrigin.X;
                    tmpOrigin.Y = (int)((double)-(cubeOrigin.Y - camera1.Position.Y) / (double)(cubeOrigin.Z - camera1.Position.Z) * zoom) + drawOrigin.Y;

                    point3D[i].X = (float)((vec.X - camera1.Position.X) / (vec.Z - camera1.Position.Z) * zoom + drawOrigin.X);
                    point3D[i].Y = (float)(-(vec.Y - camera1.Position.Y) / (vec.Z - camera1.Position.Z) * zoom + drawOrigin.Y);

                    point3D[i].X = (int)point3D[i].X;
                    point3D[i].Y = (int)point3D[i].Y;
                }
            }

            //Now to plot out the points
            Rectangle bounds = getBounds(point3D);

            bounds.Width  += drawOrigin.X;
            bounds.Height += drawOrigin.Y;

            Bitmap   tmpBmp = new Bitmap(bounds.Width, bounds.Height);
            Graphics g      = Graphics.FromImage(tmpBmp);

            //Back Face
            //g.DrawLine(Pens.Black, point3D[0], point3D[1]);
            //g.DrawLine(Pens.Black, point3D[1], point3D[2]);
            //g.DrawLine(Pens.Black, point3D[2], point3D[3]);
            //g.DrawLine(Pens.Black, point3D[3], point3D[0]);

            //PointF[] p2 = new PointF[3];
            //for (int i = 0; i < p2.Length; ++i)
            //    p2[i] = point3D[i];
            //SolidBrush brush = new SolidBrush(Color.Magenta);
            //g.FillPolygon((Brush)brush, p2);

            //Front Face
            //g.DrawLine(Pens.Black, point3D[4], point3D[5]);
            //g.DrawLine(Pens.Black, point3D[5], point3D[6]);
            //g.DrawLine(Pens.Black, point3D[6], point3D[7]);
            //g.DrawLine(Pens.Black, point3D[7], point3D[4]);

            //Right Face
            //g.DrawLine(Pens.Black, point3D[8], point3D[9]);
            //g.DrawLine(Pens.Black, point3D[9], point3D[10]);
            //g.DrawLine(Pens.Black, point3D[10], point3D[11]);
            //g.DrawLine(Pens.Black, point3D[11], point3D[8]);

            //Left Face
            //g.DrawLine(Pens.Black, point3D[12], point3D[13]);
            //g.DrawLine(Pens.Black, point3D[13], point3D[14]);
            //g.DrawLine(Pens.Black, point3D[14], point3D[15]);
            //g.DrawLine(Pens.Black, point3D[15], point3D[12]);

            //Bottom Face
            //g.DrawLine(Pens.Black, point3D[16], point3D[17]);
            //g.DrawLine(Pens.Black, point3D[17], point3D[18]);
            //g.DrawLine(Pens.Black, point3D[18], point3D[19]);
            //g.DrawLine(Pens.Black, point3D[19], point3D[16]);
            g.DrawLine(Pens.Black, point3D[16], point3D[17]);
            g.DrawLine(Pens.Black, point3D[17], point3D[18]);
            g.DrawLine(Pens.Black, point3D[18], point3D[19]);
            g.DrawLine(Pens.Black, point3D[19], point3D[16]);

            PointF[] fillBase = { point3D[16], point3D[17], point3D[18], point3D[19] };
            g.FillPolygon(Brushes.Magenta, fillBase);

            //conect bottom corners
            g.DrawLine(Pens.Black, point3D[16], point3D[0]);
            g.DrawLine(Pens.Black, point3D[17], point3D[0]);
            g.DrawLine(Pens.Black, point3D[18], point3D[0]);
            g.DrawLine(Pens.Black, point3D[19], point3D[0]);

            //Top Face
            //g.DrawLine(Pens.Magenta, point3D[20], point3D[21]);
            //g.DrawLine(Pens.Magenta, point3D[21], point3D[22]);
            //g.DrawLine(Pens.Magenta, point3D[22], point3D[23]);
            //g.DrawLine(Pens.Magenta, point3D[23], point3D[20]);

            g.Dispose(); //Clean-up

            return(tmpBmp);
        }