Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Drawing;
- using System.Windows.Forms;
- using static System.Math;
- namespace cglab1
- {
- public partial class Form1 : Form
- {
- const float TEXT_SIZE_EM = 10;
- const float SCALE_COM = 0.65f;
- const float SCALE_DIM = 0.65f;
- // Переменные для хранения значений ползунковых переключателей
- float X;
- float Y;
- float Z;
- float A;
- float B;
- float G;
- Pen dimAxisPen;
- Pen comAxisPen;
- Pen normalPen;
- Font textFont;
- RectangleF arcRect = new RectangleF();
- int angleFrom;
- int angleSweep;
- // Массив для трехмерных точек
- Point3DF[] points3d = new Point3DF[14];
- // Массив для точек пространственного чертежа
- PointF[] dimPoints = new PointF[14];
- // Массив для точек комлексного чертежа
- PointF[] comPoints = new PointF[12];
- public Form1()
- {
- InitializeComponent();
- for (int i = 0; i < 14; ++i)
- points3d[i] = new Point3DF();
- // Инициализируем объект Pen для рисования осей координат пространственного чертежа
- dimAxisPen = new Pen(Color.Black, 1);
- dimAxisPen.EndCap = System.Drawing.Drawing2D.LineCap.ArrowAnchor;
- // Инициализируем объект Pen для рисования осей координат пространственного чертежа
- comAxisPen = new Pen(Color.Black);
- comAxisPen.EndCap = comAxisPen.StartCap = System.Drawing.Drawing2D.LineCap.ArrowAnchor;
- normalPen = new Pen(Color.Black);
- textFont = new Font(FontFamily.GenericMonospace, TEXT_SIZE_EM);
- Update();
- }
- void Update() { Input(); Calculate(); Output(); }
- void Input()
- {
- X = trackBarX.Value;
- Y = trackBarY.Value;
- Z = trackBarZ.Value;
- A = trackBarA.Value;
- B = trackBarB.Value;
- G = trackBarG.Value;
- points3d[0].Set(0f, 0f, 0f); //O
- points3d[1].Set(X, Y, Z); //T
- points3d[2].Set(X, Y, 0); //T1
- points3d[3].Set(X, 0, Z); //T2
- points3d[4].Set(0, Y, Z); //T3
- points3d[5].Set(X, 0, 0); //TX
- points3d[6].Set(0, Y, 0); //TY
- points3d[7].Set(0, 0, Z); //TZ
- // Значения отступа от начала координат для рисования осей
- float axisLength = dimPictureBox.Width * 0.7f;
- points3d[8].Set(axisLength, 0, 0); //X
- points3d[9].Set(0, axisLength, 0); //Y
- points3d[10].Set(0, 0, axisLength); //Z
- points3d[11].Set(-axisLength, 0, 0); //_X
- points3d[12].Set(0, -axisLength, 0); //_Y
- points3d[13].Set(0, 0, -axisLength); //_Z
- }
- void Output()
- {
- DrawDim();
- DrawCom();
- coordinates.Text =
- "X: " + X +
- ", Y: " + Y +
- ", Z: " + Z;
- angles.Text =
- "A: " + A +
- ", B: " + B +
- ", G: " + G;
- }
- void Calculate()
- {
- // Рассчитаем точки для пространственного и комплексного чертежа
- calcDimPoints();
- calcComPoints();
- Point3DF T = points3d[1];
- PointF comO = comPoints[0];
- float rx = comO.X - Abs(T.Y) * SCALE_COM;
- float ry = comO.Y - Abs(T.Y) * SCALE_COM;
- float rw = Abs(T.Y) * 2 * SCALE_COM;
- float rh = Abs(T.Y) * 2 * SCALE_COM;
- arcRect = new RectangleF(rx, ry, rw, rh);
- if (T.Y > 0)
- {
- angleFrom = 0;
- angleSweep = 90;
- }
- else if (T.Y < 0)
- {
- angleFrom = 180;
- angleSweep = 90;
- }
- else
- {
- arcRect = new RectangleF();
- angleFrom = angleSweep = 0;
- }
- }
- // Функция берет точку i из массива points3d, вычисляет его двумерные координаты
- // и записывает эту двумерную точку в comPoints[i]
- PointF calcDimPoint(int i)
- {
- Point3DF p = points3d[i];
- float dx1 = (float) ((-p.X * Cos(DegToRad(A))));
- float dx2 = (float) ((p.Y * Cos(DegToRad(180 - B))));
- float dx3 = (float) ((p.Z * Sin(DegToRad(270 - G))));
- float dy1 = (float) ((p.X * Sin(DegToRad(A))));
- float dy2 = (float) ((p.Y * Sin(DegToRad(180 - B))));
- float dy3 = (float) ((-p.Z * Cos(DegToRad(270 - G))));
- // Итоговые координаты точки
- float x2d = dimPictureBox.Width * 0.5f + (dx1 + dx2 + dx3) * SCALE_DIM;
- float y2d = dimPictureBox.Height * 0.5f + (dy1 + dy2 + dy3) * SCALE_DIM;
- return new PointF(x2d, y2d);
- //dimPoints[i].X = x2d;
- //dimPoints[i].Y = y2d;
- }
- void calcDimPoints()
- {
- for (int i = 0; i < 14; ++i)
- dimPoints[i] = calcDimPoint(i);
- }
- void calcComPoints()
- {
- float Ox = comPictureBox.Width * 0.5f;
- float Oy = comPictureBox.Height * 0.5f;
- PointF O = comPoints[0];
- PointF T1 = comPoints[1];
- PointF T2 = comPoints[2];
- PointF T3 = comPoints[3];
- PointF TX = comPoints[4];
- PointF TY1 = comPoints[5];
- PointF TY2 = comPoints[6];
- PointF TZ = comPoints[7];
- PointF X = comPoints[8];
- PointF Y2 = comPoints[9];
- PointF Z = comPoints[10];
- PointF Y1 = comPoints[11];
- comPoints[0].X = Ox; comPoints[0].Y = Oy; //GetComPointXZ(points3d[0] /*O*/);
- comPoints[2] = GetComPointXZ(points3d[3] /*T2*/);
- comPoints[4] = GetComPointXZ(points3d[5] /*TX*/);
- comPoints[7] = GetComPointXZ(points3d[7] /*TZ*/);
- comPoints[1] = GetComPointXY(points3d[2] /*T1*/);
- comPoints[5] = GetComPointXY(points3d[6] /*TY1*/);
- comPoints[3] = GetComPointYZ(points3d[4] /*T3*/);
- comPoints[6] = GetComPointYZ(points3d[6] /*TY2*/);
- comPoints[8] = GetComPointXZ(points3d[8] /*X*/);
- comPoints[10] = GetComPointXZ(points3d[10] /*Z*/);
- comPoints[9] = GetComPointXZ(points3d[11] /*_X*/);
- comPoints[11] = GetComPointXZ(points3d[13] /*_Z*/);
- }
- PointF GetComPoint(Point3DF p)
- {
- float Ox = comPictureBox.Width * 0.5f;
- float Oy = comPictureBox.Height * 0.5f;
- float x = Ox - p.X * 1 + p.Y * 1;
- float y = Oy + p.Y * 1 - p.Z * 1;
- return new PointF(x, y);
- }
- PointF GetComPointXZ(Point3DF p)
- {
- float Ox = comPictureBox.Width * 0.5f;
- float Oy = comPictureBox.Height * 0.5f;
- float x = Ox - p.X * SCALE_COM;
- float y = Oy - p.Z * SCALE_COM;
- return new PointF(x, y);
- }
- PointF GetComPointXY(Point3DF p)
- {
- float Ox = comPictureBox.Width * 0.5f;
- float Oy = comPictureBox.Height * 0.5f;
- float x = Ox - p.X * SCALE_COM;
- float y = Oy + p.Y * SCALE_COM;
- return new PointF(x, y);
- }
- PointF GetComPointYZ(Point3DF p)
- {
- float Ox = comPictureBox.Width * 0.5f;
- float Oy = comPictureBox.Height * 0.5f;
- float x = Ox + p.Y * SCALE_COM;
- float y = Oy - p.Z * SCALE_COM;
- return new PointF(x, y);
- }
- float DegToRad(float v)
- {
- return (float)((v / 180) * Math.PI);
- }
- private class Point3DF
- {
- public float X;
- public float Y;
- public float Z;
- public Point3DF()
- {
- X = Y = Z = 0;
- }
- public Point3DF(float v1, float v2, float v3)
- {
- this.X = v1;
- this.Y = v2;
- this.Z = v3;
- }
- public void Set (float x, float y, float z)
- {
- X = x; Y = y; Z = z;
- }
- }
- private void exitButton_Click(object sender, EventArgs e)
- {
- Environment.Exit(0);
- }
- private void trackBar_Scroll(object sender, EventArgs e)
- {
- Update();
- }
- private void button1_Click(object sender, EventArgs e)
- {
- int h = (int)(dimPictureBox.Width * 0.5);
- trackBarX.Value = h / 2;
- trackBarY.Value = h / 2;
- trackBarZ.Value = h / 2;
- trackBarA.Value = 0;
- trackBarB.Value = 135;
- trackBarG.Value = 270;
- Input();
- Calculate();
- Output();
- }
- void DrawDim()
- {
- //points3d[0] - O
- //points3d[1] - T
- //points3d[2] - T1
- //points3d[3] - T2
- //points3d[4] - T3
- //points3d[5] - TX
- //points3d[6] - TY
- //points3d[7] - TZ
- //points3d[8] - X
- //points3d[9] - Y
- //points3d[10] - Z
- //points3d[11] - _X
- //points3d[12] - _Y
- //points3d[13] - _Z
- Bitmap pictureBoxBitmap = new Bitmap(dimPictureBox.Width, dimPictureBox.Height);
- Graphics g = Graphics.FromImage(pictureBoxBitmap);
- g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
- g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
- g.Clear(Color.White);
- // Для удобства запишем точки массива в именованные переменные
- PointF O = dimPoints[0];
- PointF T = dimPoints[1];
- PointF TX = dimPoints[5];
- PointF TY = dimPoints[6];
- PointF TZ = dimPoints[7];
- PointF T1 = dimPoints[2];
- PointF T2 = dimPoints[3];
- PointF T3 = dimPoints[4];
- PointF X = dimPoints[8];
- PointF Y = dimPoints[9];
- PointF Z = dimPoints[10];
- PointF _X = dimPoints[11];
- PointF _Y = dimPoints[12];
- PointF _Z = dimPoints[13];
- // Отрисуем оси
- g.DrawLine(dimAxisPen, _X, X);
- g.DrawLine(dimAxisPen, _Y, Y);
- g.DrawLine(dimAxisPen, _Z, Z);
- // Отрисуем линии
- g.DrawLine(normalPen, T, T1);
- g.DrawLine(normalPen, T, T2);
- g.DrawLine(normalPen, T, T3);
- g.DrawLine(normalPen, O, TX);
- g.DrawLine(normalPen, O, TY);
- g.DrawLine(normalPen, O, TZ);
- g.DrawLine(normalPen, TX, T1);
- g.DrawLine(normalPen, TX, T2);
- g.DrawLine(normalPen, TY, T1);
- g.DrawLine(normalPen, TY, T3);
- g.DrawLine(normalPen, TZ, T2);
- g.DrawLine(normalPen, TZ, T3);
- // Отрисуем вершины - точки
- for (int i = 0; i < 8; ++i)
- DrawVertexAt(g, dimPoints[i]);
- // Отрисуем надписи
- g.DrawString("X", textFont, Brushes.Black, X);
- g.DrawString("Y", textFont, Brushes.Black, Y);
- g.DrawString("Z", textFont, Brushes.Black, Z);
- g.DrawString("T", textFont, Brushes.Black, T);
- g.DrawString("T1", textFont, Brushes.Black, T1);
- g.DrawString("T2", textFont, Brushes.Black, T2);
- g.DrawString("T3", textFont, Brushes.Black, T3);
- g.DrawString("TX", textFont, Brushes.Black, TX);
- g.DrawString("TY", textFont, Brushes.Black, TY);
- g.DrawString("TZ", textFont, Brushes.Black, TZ);
- g.DrawString("O", textFont, Brushes.Black, O);
- dimPictureBox.Image = pictureBoxBitmap;
- }
- void DrawCom()
- {
- Bitmap pictureBoxBitmap = new Bitmap(comPictureBox.Width, comPictureBox.Height);
- Graphics g = Graphics.FromImage(pictureBoxBitmap);
- g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
- g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
- g.Clear(Color.White);
- for (int i = 0; i < 8; ++i)
- DrawVertexAt(g, comPoints[i]);
- PointF O = comPoints[0];
- PointF T1 = comPoints[1];
- PointF T2 = comPoints[2];
- PointF T3 = comPoints[3];
- PointF TX = comPoints[4];
- PointF TY1 = comPoints[5];
- PointF TY2 = comPoints[6];
- PointF TZ = comPoints[7];
- PointF X = comPoints[8];
- PointF Y2 = comPoints[9];
- PointF Z = comPoints[10];
- PointF Y1 = comPoints[11];
- g.DrawLine(comAxisPen, X, Y2);
- g.DrawLine(comAxisPen, Z, Y1);
- g.DrawLine(normalPen, O, TY1);
- g.DrawLine(normalPen, O, TY2);
- g.DrawLine(normalPen, O, TZ);
- g.DrawLine(normalPen, O, TX);
- g.DrawLine(normalPen, T2, TZ);
- g.DrawLine(normalPen, T2, TX);
- g.DrawLine(normalPen, T1, TX);
- g.DrawLine(normalPen, T1, TY1);
- g.DrawLine(normalPen, T3, TZ);
- g.DrawLine(normalPen, T3, TY2);
- g.DrawString("T1", textFont, Brushes.Black, T1);
- g.DrawString("T2", textFont, Brushes.Black, T2);
- g.DrawString("T3", textFont, Brushes.Black, T3);
- g.DrawString("TX", textFont, Brushes.Black, TX);
- g.DrawString("TZ", textFont, Brushes.Black, TZ);
- g.DrawString("O", textFont, Brushes.Black, O);
- g.DrawString("TY1", textFont, Brushes.Black, TY1);
- g.DrawString("TY2", textFont, Brushes.Black, TY2);
- g.DrawString(" X", textFont, Brushes.Black, X.X, X.Y + comAxisPen.Width);
- g.DrawString("-Y", textFont, Brushes.Black, X.X, X.Y - 2 * TEXT_SIZE_EM);
- g.DrawString("-X", textFont, Brushes.Black, Y2.X - 2 * TEXT_SIZE_EM, X.Y + comAxisPen.Width);
- g.DrawString(" Y", textFont, Brushes.Black, Y2.X - 2 * TEXT_SIZE_EM, X.Y - 2 * TEXT_SIZE_EM);
- g.DrawString(" Z", textFont, Brushes.Black, Z.X - comAxisPen.Width - 2 * TEXT_SIZE_EM, Z.Y);
- g.DrawString("-Y", textFont, Brushes.Black, Z.X, Z.Y);
- g.DrawString("-Z", textFont, Brushes.Black, Y1.X - comAxisPen.Width - 2 * TEXT_SIZE_EM, Y1.Y - 2 * TEXT_SIZE_EM);
- g.DrawString("Y", textFont, Brushes.Black, Y1.X + comAxisPen.Width, Y1.Y - 2 * TEXT_SIZE_EM);
- if (angleSweep != 0)
- g.DrawArc(Pens.Black, arcRect, angleFrom, angleSweep);
- comPictureBox.Image = pictureBoxBitmap;
- }
- private void DrawVertexAt(Graphics g, PointF t)
- {
- const float r = 3f;
- g.FillEllipse(Brushes.Black, t.X - r, t.Y - r, 2 * r, 2 * r);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement