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); }
public Cube(int Width, int Height, int Depth) { width = Width; height = Height; depth = Depth; cubeOrigin = new Math3D.Point3D(width / 2, height / 2, depth / 2); }
public Cube(int side, Math3D.Point3D origin) : base(side, origin) { width = side; height = side; depth = side; Origin = new Math3D.Point3D(origin.X, origin.Y, 0); }
public Cube(int Width, int Height, int Depth, Math3D.Point3D origin) : base(Width, Height, Depth, origin) { width = Width; height = Height; depth = Depth; Origin = new Math3D.Point3D(origin.X, origin.Y, 0); }
public Cube(int side) { width = side; height = side; depth = side; cubeOrigin = new Math3D.Point3D(width, height, depth); camera1.Position = new Math3D.Point3D(cubeOrigin.X, cubeOrigin.Y, 1000); }
public Cube(int side, Math3D.Point3D origin) { width = side; height = side; depth = side; cubeOrigin = origin; camera1.Position = new Math3D.Point3D(cubeOrigin.X, cubeOrigin.Y, 1000); }
//отрисовка куба в перспективной проекции public Bitmap drawCubePespect(Point drawOrigin) { PointF[] point3D = new PointF[8]; Point tmpOrigin = new Point(0, 0); Math3D.Point3D point0 = new Math3D.Point3D(0, 0, 0); Math3D.Point3D[] cubePoints = fillCubeVertices(width, height, depth); //Перевод 3д точек в 2д //Перспективная проекция Math3D.Point3D vec; for (int i = 0; i < point3D.Length; i++) { vec = cubePoints[i]; //double k = -(camera1.Position.Z); //point3D[i].X = (int)((k * vec.X) / (vec.Z + k)) + drawOrigin.X; //point3D[i].Y = (int)((k * vec.Y) / (vec.Z + k)) + drawOrigin.Y; point3D[i].X = (int)((vec.X * camera1.Position.Z - vec.Z * camera1.Position.X) / (camera1.Position.Z - vec.Z) + drawOrigin.X); point3D[i].Y = (int)((vec.Y * camera1.Position.Z - vec.Z * camera1.Position.Y) / (camera1.Position.Z - vec.Z) + drawOrigin.Y); } //расчёт точек Rectangle bounds = getBounds(point3D); bounds.Width += drawOrigin.X; bounds.Height += drawOrigin.Y; Bitmap tmpBmp = new Bitmap(bounds.Width, bounds.Height); //задняя грань Drawing.BresenhamLine(tmpBmp, Color.Black, Color.Black, (int)point3D[0].X, (int)point3D[0].Y, (int)point3D[1].X, (int)point3D[1].Y); Drawing.BresenhamLine(tmpBmp, Color.Black, Color.Black, (int)point3D[1].X, (int)point3D[1].Y, (int)point3D[2].X, (int)point3D[2].Y); Drawing.BresenhamLine(tmpBmp, Color.Black, Color.Black, (int)point3D[2].X, (int)point3D[2].Y, (int)point3D[3].X, (int)point3D[3].Y); Drawing.BresenhamLine(tmpBmp, Color.Black, Color.Black, (int)point3D[3].X, (int)point3D[3].Y, (int)point3D[0].X, (int)point3D[0].Y); //передняя грань Drawing.BresenhamLine(tmpBmp, Color.Black, Color.Black, (int)point3D[4].X, (int)point3D[4].Y, (int)point3D[5].X, (int)point3D[5].Y); Drawing.BresenhamLine(tmpBmp, Color.Black, Color.Black, (int)point3D[5].X, (int)point3D[5].Y, (int)point3D[6].X, (int)point3D[6].Y); Drawing.BresenhamLine(tmpBmp, Color.Black, Color.Black, (int)point3D[6].X, (int)point3D[6].Y, (int)point3D[7].X, (int)point3D[7].Y); Drawing.BresenhamLine(tmpBmp, Color.Black, Color.Black, (int)point3D[7].X, (int)point3D[7].Y, (int)point3D[4].X, (int)point3D[4].Y); //боковые грани Drawing.BresenhamLine(tmpBmp, Color.Black, Color.Black, (int)point3D[0].X, (int)point3D[0].Y, (int)point3D[4].X, (int)point3D[4].Y); Drawing.BresenhamLine(tmpBmp, Color.Black, Color.Black, (int)point3D[1].X, (int)point3D[1].Y, (int)point3D[5].X, (int)point3D[5].Y); Drawing.BresenhamLine(tmpBmp, Color.Black, Color.Black, (int)point3D[2].X, (int)point3D[2].Y, (int)point3D[6].X, (int)point3D[6].Y); Drawing.BresenhamLine(tmpBmp, Color.Black, Color.Black, (int)point3D[3].X, (int)point3D[3].Y, (int)point3D[7].X, (int)point3D[7].Y); return(tmpBmp); }
public static Math3D.Point3D[] fillCubeVertices(int width, int height, int depth) { Math3D.Point3D[] verts = new Math3D.Point3D[8]; verts[0] = new Math3D.Point3D(-width / 2, -height / 2, -depth / 2); verts[1] = new Math3D.Point3D(-width / 2, height / 2, -depth / 2); verts[2] = new Math3D.Point3D(width / 2, height / 2, -depth / 2); verts[3] = new Math3D.Point3D(width / 2, -height / 2, -depth / 2); verts[4] = new Math3D.Point3D(-width / 2, -height / 2, depth / 2); verts[5] = new Math3D.Point3D(-width / 2, height / 2, depth / 2); verts[6] = new Math3D.Point3D(width / 2, height / 2, depth / 2); verts[7] = new Math3D.Point3D(width / 2, -height / 2, depth / 2); return(verts); }
//отрисовка куба в параллельной проекции public Bitmap drawCubeParallel(Point drawOrigin) { PointF[] point3D = new PointF[8]; Point tmpOrigin = new Point(0, 0); Math3D.Point3D point0 = new Math3D.Point3D(0, 0, 0); Math3D.Point3D[] cubePoints = fillCubeVertices(width, height, depth); //Перевод 3д точек в 2д //Параллельная проекция Math3D.Point3D vec; for (int i = 0; i < point3D.Length; i++) { vec = cubePoints[i]; point3D[i].X = (int)(vec.X + vec.Z / 2 + drawOrigin.X); point3D[i].Y = (int)(vec.Y + vec.Z / 2 + drawOrigin.Y); } //расчёт точек Rectangle bounds = getBounds(point3D); bounds.Width += drawOrigin.X; bounds.Height += drawOrigin.Y; Bitmap tmpBmp = new Bitmap(bounds.Width, bounds.Height); //задняя грань Drawing.BresenhamLine(tmpBmp, Color.Black, Color.Black, (int)point3D[0].X, (int)point3D[0].Y, (int)point3D[1].X, (int)point3D[1].Y); Drawing.BresenhamLine(tmpBmp, Color.Black, Color.Black, (int)point3D[1].X, (int)point3D[1].Y, (int)point3D[2].X, (int)point3D[2].Y); Drawing.BresenhamLine(tmpBmp, Color.Black, Color.Black, (int)point3D[2].X, (int)point3D[2].Y, (int)point3D[3].X, (int)point3D[3].Y); Drawing.BresenhamLine(tmpBmp, Color.Black, Color.Black, (int)point3D[3].X, (int)point3D[3].Y, (int)point3D[0].X, (int)point3D[0].Y); //передняя грань Drawing.BresenhamLine(tmpBmp, Color.Black, Color.Black, (int)point3D[4].X, (int)point3D[4].Y, (int)point3D[5].X, (int)point3D[5].Y); Drawing.BresenhamLine(tmpBmp, Color.Black, Color.Black, (int)point3D[5].X, (int)point3D[5].Y, (int)point3D[6].X, (int)point3D[6].Y); Drawing.BresenhamLine(tmpBmp, Color.Black, Color.Black, (int)point3D[6].X, (int)point3D[6].Y, (int)point3D[7].X, (int)point3D[7].Y); Drawing.BresenhamLine(tmpBmp, Color.Black, Color.Black, (int)point3D[7].X, (int)point3D[7].Y, (int)point3D[4].X, (int)point3D[4].Y); //боковые грани Drawing.BresenhamLine(tmpBmp, Color.Black, Color.Black, (int)point3D[0].X, (int)point3D[0].Y, (int)point3D[4].X, (int)point3D[4].Y); Drawing.BresenhamLine(tmpBmp, Color.Black, Color.Black, (int)point3D[1].X, (int)point3D[1].Y, (int)point3D[5].X, (int)point3D[5].Y); Drawing.BresenhamLine(tmpBmp, Color.Black, Color.Black, (int)point3D[2].X, (int)point3D[2].Y, (int)point3D[6].X, (int)point3D[6].Y); Drawing.BresenhamLine(tmpBmp, Color.Black, Color.Black, (int)point3D[3].X, (int)point3D[3].Y, (int)point3D[7].X, (int)point3D[7].Y); return(tmpBmp); }
public void calcCube(Point drawOrigin) { PointF[] point3D = new PointF[24]; Point tmpOrigin = new Point(0, 0); Math3D.Point3D point0 = new Math3D.Point3D(0, 0, 0); double zoom = (double)Screen.PrimaryScreen.Bounds.Width / 1.5; Math3D.Point3D[] cubePoints = fillCubeVertices(width, height, depth); Math3D.Point3D anchorPoint = (Math3D.Point3D)cubePoints[4]; double cameraZ = -(((anchorPoint.X - cubeOrigin.X) * zoom) / cubeOrigin.X) + anchorPoint.Z; camera1.Position = new Math3D.Point3D(cubeOrigin.X, cubeOrigin.Y, cameraZ); cubePoints = Math3D.Translate(cubePoints, cubeOrigin, point0); cubePoints = Math3D.RotateX(cubePoints, xRotation); cubePoints = Math3D.RotateY(cubePoints, yRotation); cubePoints = Math3D.RotateZ(cubePoints, zRotation); cubePoints = Math3D.Translate(cubePoints, point0, cubeOrigin); 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; } } a = Point.Round(point3D[4]); b = Point.Round(point3D[5]); c = Point.Round(point3D[6]); d = Point.Round(point3D[7]); }
//отрисовка куба в параллельной проекции public Bitmap drawCubeParallel(Point drawOrigin) { PointF[] point3D = new PointF[8]; Point tmpOrigin = new Point(0, 0); Math3D.Point3D point0 = new Math3D.Point3D(0, 0, 0); Math3D.Point3D[] cubePoints = fillCubeVertices(width, height, depth); //перемещение точек cubePoints = Math3D.RotateX(cubePoints, xRotation); cubePoints = Math3D.RotateY(cubePoints, yRotation); cubePoints = Math3D.RotateZ(cubePoints, zRotation); //Перевод 3д точек в 2д //Параллельная проекция Math3D.Point3D vec; for (int i = 0; i < point3D.Length; i++) { vec = cubePoints[i]; point3D[i].X = (int)(vec.X /*+ vec.Z / 2*/ + drawOrigin.X); point3D[i].Y = (int)(vec.Y /*+ vec.Z / 2*/ + drawOrigin.Y); } //расчёт точек Rectangle bounds = getBounds(point3D); bounds.Width += drawOrigin.X; bounds.Height += drawOrigin.Y; Bitmap tmpBmp = new Bitmap(bounds.Width, bounds.Height); float[] dist = new float[8]; for (int i = 0; i < dist.Length; i++) { dist[i] = Dist(cubePoints[i], camera1.Position); } Color[] colorDist = new Color[8]; float minDist = dist[0]; float maxDist = dist[0]; for (int i = 0; i < dist.Length; i++) { if (maxDist < dist[i]) { maxDist = dist[i]; } if (minDist > dist[i]) { minDist = dist[i]; } } for (int i = 0; i < colorDist.Length; i++) { colorDist[i] = Drawing.Interpolate(Color.Black, Color.Red, ((dist[i] - minDist) / (maxDist - minDist))); } //задняя грань Drawing.BresenhamLine(tmpBmp, colorDist[0], colorDist[1], (int)point3D[0].X, (int)point3D[0].Y, (int)point3D[1].X, (int)point3D[1].Y); Drawing.BresenhamLine(tmpBmp, colorDist[1], colorDist[2], (int)point3D[1].X, (int)point3D[1].Y, (int)point3D[2].X, (int)point3D[2].Y); Drawing.BresenhamLine(tmpBmp, colorDist[2], colorDist[3], (int)point3D[2].X, (int)point3D[2].Y, (int)point3D[3].X, (int)point3D[3].Y); Drawing.BresenhamLine(tmpBmp, colorDist[3], colorDist[0], (int)point3D[3].X, (int)point3D[3].Y, (int)point3D[0].X, (int)point3D[0].Y); //передняя грань Drawing.BresenhamLine(tmpBmp, colorDist[4], colorDist[5], (int)point3D[4].X, (int)point3D[4].Y, (int)point3D[5].X, (int)point3D[5].Y); Drawing.BresenhamLine(tmpBmp, colorDist[5], colorDist[6], (int)point3D[5].X, (int)point3D[5].Y, (int)point3D[6].X, (int)point3D[6].Y); Drawing.BresenhamLine(tmpBmp, colorDist[6], colorDist[7], (int)point3D[6].X, (int)point3D[6].Y, (int)point3D[7].X, (int)point3D[7].Y); Drawing.BresenhamLine(tmpBmp, colorDist[7], colorDist[4], (int)point3D[7].X, (int)point3D[7].Y, (int)point3D[4].X, (int)point3D[4].Y); //боковые грани Drawing.BresenhamLine(tmpBmp, colorDist[0], colorDist[4], (int)point3D[0].X, (int)point3D[0].Y, (int)point3D[4].X, (int)point3D[4].Y); Drawing.BresenhamLine(tmpBmp, colorDist[1], colorDist[5], (int)point3D[1].X, (int)point3D[1].Y, (int)point3D[5].X, (int)point3D[5].Y); Drawing.BresenhamLine(tmpBmp, colorDist[2], colorDist[6], (int)point3D[2].X, (int)point3D[2].Y, (int)point3D[6].X, (int)point3D[6].Y); Drawing.BresenhamLine(tmpBmp, colorDist[3], colorDist[7], (int)point3D[3].X, (int)point3D[3].Y, (int)point3D[7].X, (int)point3D[7].Y); return(tmpBmp); }
public float Dist(Math3D.Point3D a, Math3D.Point3D b) { return((float)Math.Sqrt((b.X - a.X) * (b.X - a.X) + (b.Y - a.Y) * (b.Y - a.Y) + (b.Z - a.Z) * (b.Z - a.Z))); }