private void button1_Click(object sender, EventArgs e) { d = button1_Click; Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); pictureBox1.Image = new Bitmap(pictureBox1.Width, pictureBox1.Height); using (var g = Graphics.FromImage(pictureBox1.Image)) { Brush b = Brushes.Black; Pen p = Pens.Black; int x = Convert.ToInt32(textBox1.Text); int y = Convert.ToInt32(textBox3.Text); int z = Convert.ToInt32(textBox2.Text); int XSize = Convert.ToInt32(textBox4.Text); int YSize = Convert.ToInt32(textBox5.Text); int dist = Convert.ToInt32(textBox6.Text); int XAngle = Convert.ToInt32(trackBar1.Value); int YAngle = Convert.ToInt32(trackBar2.Value); int Size = Convert.ToInt32(textBox7.Text); PointF[] point3D = new PointF[4]; View3D.Point3D point0 = checkBox1.Checked ? new View3D.Point3D(0, 0, 0) : new View3D.Point3D(x, y, z); View3D.Point3D[] xyzPoints = XYZPoints((int)Size, (int)Size, (int)Size); View3D.Point3D cubeOrigin = new View3D.Point3D((int)(Size / 2), (int)(Size / 2), (int)(Size / 2)); xyzPoints = View3D.Translate(xyzPoints, cubeOrigin, point0); xyzPoints = View3D.RotateX(xyzPoints, XAngle); xyzPoints = View3D.RotateY(xyzPoints, YAngle); xyzPoints = View3D.Translate(xyzPoints, point0, cubeOrigin); for (int i = 0; i < point3D.Length; i++) { point3D[i].X = ((XSize / 2) + (((float)xyzPoints[i].X * dist) / ((float)xyzPoints[i].Z + dist))); point3D[i].Y = (YSize / 2) - (((float)xyzPoints[i].Y * dist) / ((float)xyzPoints[i].Z + dist)); } g.DrawLine(Pens.Red, point3D[0], point3D[1]); g.DrawLine(Pens.Green, point3D[0], point3D[2]); g.DrawLine(Pens.Blue, point3D[0], point3D[3]); pictureBox1.Refresh(); } stopwatch.Stop(); label1.Text = "Runtime: " + stopwatch.Elapsed.ToString(); }
private void button1_Click(object sender, EventArgs e) { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); pictureBox1.Image = new Bitmap(pictureBox1.Width, pictureBox1.Height); using (var g = Graphics.FromImage(pictureBox1.Image)) { Brush b = Brushes.Green; Pen p = Pens.Black; PointF[] point3D = new PointF[6]; int x = Convert.ToInt32(textBox1.Text); int y = Convert.ToInt32(textBox3.Text); int z = Convert.ToInt32(textBox2.Text); int XSize = Convert.ToInt32(textBox4.Text); int YSize = Convert.ToInt32(textBox5.Text); int dist = Convert.ToInt32(textBox6.Text); int Size = Convert.ToInt32(textBox7.Text); int LampZ = Convert.ToInt32(textBox8.Text); int XAngle = Convert.ToInt32(trackBar1.Value); int YAngle = Convert.ToInt32(trackBar2.Value); int ZAngle = Convert.ToInt32(trackBar3.Value); View3D.Point3D point0 = new View3D.Point3D(x, y, z); View3D.Point3D[] pyramidPoints = PiramidPoints(Size, Size, Size); View3D.Point3D pyramidOrigin = new View3D.Point3D((int)(Size / 2), (int)(Size / 2), (int)(Size / 2)); pyramidPoints = View3D.Translate(pyramidPoints, pyramidOrigin, point0); pyramidPoints = View3D.RotateX(pyramidPoints, XAngle); pyramidPoints = View3D.RotateY(pyramidPoints, YAngle); pyramidPoints = View3D.RotateZ(pyramidPoints, ZAngle); pyramidPoints = View3D.Translate(pyramidPoints, point0, pyramidOrigin); for (int i = 0; i < point3D.Length; i++) { point3D[i].X = ((XSize / 2) + (((float)pyramidPoints[i].X * dist) / ((float)pyramidPoints[i].Z + dist))); point3D[i].Y = (YSize / 2) - (((float)pyramidPoints[i].Y * dist) / ((float)pyramidPoints[i].Z + dist)); } if (!checkBox1.Checked) { View3D.Point3D n = View3D.CalcNormal(pyramidPoints[3], pyramidPoints[0], pyramidPoints[1]); if (n.Z * dist + n.X * pyramidPoints[3].X + n.Y * pyramidPoints[3].Y + n.Z * pyramidPoints[3].Z > 0) { //1 g.DrawLine(p, point3D[1].X, point3D[1].Y, point3D[0].X, point3D[0].Y); g.DrawLine(p, point3D[3].X, point3D[3].Y, point3D[0].X, point3D[0].Y); g.DrawLine(p, point3D[3].X, point3D[3].Y, point3D[1].X, point3D[1].Y); } n = View3D.CalcNormal(pyramidPoints[5], pyramidPoints[0], pyramidPoints[3]); if (n.Z * dist + n.X * pyramidPoints[5].X + n.Y * pyramidPoints[5].Y + n.Z * pyramidPoints[5].Z > 0) { //2 g.DrawLine(p, point3D[3].X, point3D[3].Y, point3D[0].X, point3D[0].Y); g.DrawLine(p, point3D[5].X, point3D[5].Y, point3D[0].X, point3D[0].Y); g.DrawLine(p, point3D[3].X, point3D[3].Y, point3D[5].X, point3D[5].Y); } n = View3D.CalcNormal(pyramidPoints[5], pyramidPoints[3], pyramidPoints[1]); if (n.Z * dist + n.X * pyramidPoints[5].X + n.Y * pyramidPoints[5].Y + n.Z * pyramidPoints[5].Z > 0) { //3 g.DrawLine(p, point3D[3].X, point3D[3].Y, point3D[5].X, point3D[5].Y); g.DrawLine(p, point3D[5].X, point3D[5].Y, point3D[1].X, point3D[1].Y); g.DrawLine(p, point3D[3].X, point3D[3].Y, point3D[1].X, point3D[1].Y); } n = View3D.CalcNormal(pyramidPoints[5], pyramidPoints[1], pyramidPoints[0]); if (n.Z * dist + n.X * pyramidPoints[5].X + n.Y * pyramidPoints[5].Y + n.Z * pyramidPoints[5].Z > 0) { //4 g.DrawLine(p, point3D[5].X, point3D[5].Y, point3D[0].X, point3D[0].Y); g.DrawLine(p, point3D[5].X, point3D[5].Y, point3D[1].X, point3D[1].Y); g.DrawLine(p, point3D[1].X, point3D[1].Y, point3D[0].X, point3D[0].Y); } } else { View3D.Point3D n = View3D.CalcNormal(pyramidPoints[3], pyramidPoints[0], pyramidPoints[1]); if (n.Z * dist + n.X * pyramidPoints[3].X + n.Y * pyramidPoints[3].Y + n.Z * pyramidPoints[3].Z > 0 && n.Z * dist + n.X * pyramidPoints[0].X + n.Y * pyramidPoints[0].Y + n.Z * pyramidPoints[0].Z > 0 && n.Z * dist + n.X * pyramidPoints[1].X + n.Y * pyramidPoints[1].Y + n.Z * pyramidPoints[1].Z > 0) { //1 var s = Math.Abs((n.X * button2.Location.X + n.Y * button2.Location.Y + n.Z * LampZ + n.X * pyramidPoints[3].X + n.Y * pyramidPoints[3].Y + n.Z * pyramidPoints[3].Z) + (n.Z * dist + n.X * pyramidPoints[0].X + n.Y * pyramidPoints[0].Y + n.Z * pyramidPoints[0].Z) + n.Z * dist + n.X * pyramidPoints[1].X + n.Y * pyramidPoints[1].Y + n.Z * pyramidPoints[1].Z); Color c = new Color(); s = (s - 0) / (25500 - 0); if (s < 255) { c = Color.FromArgb(0, (int)s, 0); } else { c = Color.FromArgb(0, 255, 0); } b = new SolidBrush(c); g.FillPolygon(b, new PointF[3] { new PointF(point3D[1].X, point3D[1].Y), new PointF(point3D[0].X, point3D[0].Y), new PointF(point3D[3].X, point3D[3].Y) }); g.DrawLine(p, point3D[1].X, point3D[1].Y, point3D[0].X, point3D[0].Y); g.DrawLine(p, point3D[3].X, point3D[3].Y, point3D[0].X, point3D[0].Y); g.DrawLine(p, point3D[3].X, point3D[3].Y, point3D[1].X, point3D[1].Y); } n = View3D.CalcNormal(pyramidPoints[5], pyramidPoints[0], pyramidPoints[3]); if (n.Z * dist + n.X * pyramidPoints[5].X + n.Y * pyramidPoints[5].Y + n.Z * pyramidPoints[5].Z > 0 && n.Z * dist + n.X * pyramidPoints[0].X + n.Y * pyramidPoints[0].Y + n.Z * pyramidPoints[0].Z > 0 && n.Z * dist + n.X * pyramidPoints[3].X + n.Y * pyramidPoints[3].Y + n.Z * pyramidPoints[3].Z > 0) { var s = Math.Abs((n.X * button2.Location.X + n.Y * button2.Location.Y + n.Z * LampZ + n.X * pyramidPoints[5].X + n.Y * pyramidPoints[5].Y + n.Z * pyramidPoints[5].Z) + (n.Z * dist + n.X * pyramidPoints[0].X + n.Y * pyramidPoints[0].Y + n.Z * pyramidPoints[0].Z) + n.Z * dist + n.X * pyramidPoints[3].X + n.Y * pyramidPoints[3].Y + n.Z * pyramidPoints[3].Z); Color c = new Color(); s = (s - 0) / (25500 - 0); if (s < 255) { c = Color.FromArgb(0, (int)s, 0); } else { c = Color.FromArgb(0, 255, 0); } b = new SolidBrush(c); //2 g.FillPolygon(b, new PointF[3] { new PointF(point3D[3].X, point3D[3].Y), new PointF(point3D[0].X, point3D[0].Y), new PointF(point3D[5].X, point3D[5].Y) }); g.DrawLine(p, point3D[3].X, point3D[3].Y, point3D[0].X, point3D[0].Y); g.DrawLine(p, point3D[5].X, point3D[5].Y, point3D[0].X, point3D[0].Y); g.DrawLine(p, point3D[3].X, point3D[3].Y, point3D[5].X, point3D[5].Y); } n = View3D.CalcNormal(pyramidPoints[5], pyramidPoints[3], pyramidPoints[1]); if (n.Z * dist + n.X * pyramidPoints[5].X + n.Y * pyramidPoints[5].Y + n.Z * pyramidPoints[5].Z > 0 && n.Z * dist + n.X * pyramidPoints[3].X + n.Y * pyramidPoints[3].Y + n.Z * pyramidPoints[3].Z > 0 && n.Z * dist + n.X * pyramidPoints[1].X + n.Y * pyramidPoints[1].Y + n.Z * pyramidPoints[1].Z > 0) { var s = Math.Abs(n.X * button2.Location.X + n.Y * button2.Location.Y + n.Z * LampZ + n.X * pyramidPoints[5].X + n.Y * pyramidPoints[5].Y + n.Z * pyramidPoints[5].Z + (n.Z * dist + n.X * pyramidPoints[3].X + n.Y * pyramidPoints[3].Y + n.Z * pyramidPoints[3].Z) + n.Z * dist + n.X * pyramidPoints[1].X + n.Y * pyramidPoints[1].Y + n.Z * pyramidPoints[1].Z); Color c = new Color(); s = (s - 0) / (25500 - 0); if (s < 255) { c = Color.FromArgb(0, (int)s, 0); } else { c = Color.FromArgb(0, 255, 0); } b = new SolidBrush(c); //3 g.FillPolygon(b, new PointF[3] { new PointF(point3D[3].X, point3D[3].Y), new PointF(point3D[5].X, point3D[5].Y), new PointF(point3D[1].X, point3D[1].Y) }); g.DrawLine(p, point3D[3].X, point3D[3].Y, point3D[5].X, point3D[5].Y); g.DrawLine(p, point3D[5].X, point3D[5].Y, point3D[1].X, point3D[1].Y); g.DrawLine(p, point3D[3].X, point3D[3].Y, point3D[1].X, point3D[1].Y); } n = View3D.CalcNormal(pyramidPoints[5], pyramidPoints[1], pyramidPoints[0]); if (n.Z * dist + n.X * pyramidPoints[5].X + n.Y * pyramidPoints[5].Y + n.Z * pyramidPoints[5].Z > 0 && n.Z * dist + n.X * pyramidPoints[1].X + n.Y * pyramidPoints[1].Y + n.Z * pyramidPoints[1].Z > 0 && n.Z * dist + n.X * pyramidPoints[0].X + n.Y * pyramidPoints[0].Y + n.Z * pyramidPoints[0].Z > 0) { var s = Math.Abs(n.X * button2.Location.X + n.Y * button2.Location.Y + n.Z * LampZ + n.X * pyramidPoints[5].X + n.Y * pyramidPoints[5].Y + n.Z * pyramidPoints[5].Z + (n.Z * dist + n.X * pyramidPoints[1].X + n.Y * pyramidPoints[1].Y + n.Z * pyramidPoints[1].Z) + n.Z * dist + n.X * pyramidPoints[0].X + n.Y * pyramidPoints[0].Y + n.Z * pyramidPoints[0].Z); Color c = new Color(); s = (s - 0) / (25500 - 0); if (s < 255) { c = Color.FromArgb(0, (int)s, 0); } else { c = Color.FromArgb(0, 255, 0); } b = new SolidBrush(c); //4 g.FillPolygon(b, new PointF[3] { new PointF(point3D[5].X, point3D[5].Y), new PointF(point3D[0].X, point3D[0].Y), new PointF(point3D[1].X, point3D[1].Y) }); g.DrawLine(p, point3D[5].X, point3D[5].Y, point3D[0].X, point3D[0].Y); g.DrawLine(p, point3D[5].X, point3D[5].Y, point3D[1].X, point3D[1].Y); g.DrawLine(p, point3D[1].X, point3D[1].Y, point3D[0].X, point3D[0].Y); } } pictureBox1.Refresh(); } stopwatch.Stop(); label11.Text = "Runtime: " + stopwatch.Elapsed.ToString(); GC.Collect(); }
private void button4_Click(object sender, EventArgs e) { d = new Del(button4_Click); Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); pictureBox1.Image = new Bitmap(pictureBox1.Width, pictureBox1.Height); using (var g = Graphics.FromImage(pictureBox1.Image)) { Brush b = Brushes.Black; Pen p = Pens.Black; int x = Convert.ToInt32(textBox1.Text); int y = Convert.ToInt32(textBox3.Text); int z = Convert.ToInt32(textBox2.Text); int XSize = Convert.ToInt32(textBox4.Text); int YSize = Convert.ToInt32(textBox5.Text); int dist = Convert.ToInt32(textBox6.Text); int Size = Convert.ToInt32(textBox7.Text); int XAngle = Convert.ToInt32(trackBar1.Value); int YAngle = Convert.ToInt32(trackBar2.Value) + 90; int notRotatedX = x; int notRotatedY = y; PointF[] point3D = new PointF[24]; View3D.Point3D point0 = checkBox1.Checked ? new View3D.Point3D(0, 0, 0) : new View3D.Point3D(x, y, z); View3D.Point3D[] cubePoints = CubePoints(Size, Size, Size); View3D.Point3D cubeOrigin = new View3D.Point3D((int)(Size / 2), (int)(Size / 2), (int)(Size / 2)); cubePoints = View3D.Translate(cubePoints, cubeOrigin, point0); cubePoints = View3D.RotateX(cubePoints, XAngle); cubePoints = View3D.RotateY(cubePoints, YAngle); cubePoints = View3D.Translate(cubePoints, point0, cubeOrigin); for (int i = 0; i < point3D.Length; i++) { point3D[i].X = ((XSize / 2) + (((float)cubePoints[i].X * dist) / ((float)cubePoints[i].Z + dist))); point3D[i].Y = (YSize / 2) - (((float)cubePoints[i].Y * dist) / ((float)cubePoints[i].Z + dist)); } g.DrawLine(Pens.Red, point3D[0], point3D[1]); g.DrawLine(Pens.Red, point3D[1], point3D[2]); g.DrawLine(Pens.Red, point3D[2], point3D[3]); g.DrawLine(Pens.Red, point3D[3], point3D[0]); 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]); 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]); 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]); g.DrawLine(Pens.Green, point3D[16], point3D[17]); g.DrawLine(Pens.Green, point3D[17], point3D[18]); g.DrawLine(Pens.Green, point3D[18], point3D[19]); g.DrawLine(Pens.Green, point3D[19], point3D[16]); 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]); pictureBox1.Refresh(); } stopwatch.Stop(); label1.Text = "Runtime: " + stopwatch.Elapsed.ToString(); }