Advertisement
inf926k

Untitled

Nov 12th, 2016
351
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 15.76 KB | None | 0 0
  1. using System;
  2. using System.Drawing;
  3. using System.Windows.Forms;
  4. using static System.Math;
  5. namespace cglab1
  6. {
  7.     public partial class Form1 : Form
  8.     {
  9.         const float TEXT_SIZE_EM = 10;
  10.  
  11.         const float SCALE_COM = 0.65f;
  12.  
  13.         const float SCALE_DIM = 0.65f;
  14.  
  15.         // Переменные для хранения значений ползунковых переключателей
  16.         float X;
  17.         float Y;
  18.         float Z;
  19.  
  20.         float A;
  21.         float B;
  22.         float G;
  23.  
  24.         Pen dimAxisPen;
  25.         Pen comAxisPen;
  26.         Pen normalPen;
  27.         Font textFont;
  28.  
  29.         RectangleF arcRect = new RectangleF();
  30.         int angleFrom;
  31.         int angleSweep;
  32.  
  33.         // Массив для трехмерных точек
  34.         Point3DF[] points3d = new Point3DF[14];
  35.  
  36.         // Массив для точек пространственного чертежа
  37.         PointF[] dimPoints = new PointF[14];
  38.  
  39.         // Массив для точек комлексного чертежа
  40.         PointF[] comPoints = new PointF[12];
  41.  
  42.         public Form1()
  43.         {
  44.             InitializeComponent();
  45.             for (int i = 0; i < 14; ++i)
  46.                 points3d[i] = new Point3DF();
  47.            
  48.             // Инициализируем объект Pen для рисования осей координат пространственного чертежа
  49.             dimAxisPen = new Pen(Color.Black, 1);
  50.             dimAxisPen.EndCap = System.Drawing.Drawing2D.LineCap.ArrowAnchor;
  51.  
  52.             // Инициализируем объект Pen для рисования осей координат пространственного чертежа
  53.             comAxisPen = new Pen(Color.Black);
  54.             comAxisPen.EndCap = comAxisPen.StartCap = System.Drawing.Drawing2D.LineCap.ArrowAnchor;
  55.             normalPen = new Pen(Color.Black);
  56.             textFont = new Font(FontFamily.GenericMonospace, TEXT_SIZE_EM);
  57.  
  58.             Update();
  59.         }
  60.         void Update() { Input(); Calculate(); Output(); }
  61.         void Input()
  62.         {
  63.             X = trackBarX.Value;
  64.             Y = trackBarY.Value;
  65.             Z = trackBarZ.Value;
  66.  
  67.             A = trackBarA.Value;
  68.             B = trackBarB.Value;
  69.             G = trackBarG.Value;
  70.  
  71.  
  72.             points3d[0].Set(0f, 0f, 0f); //O
  73.             points3d[1].Set(X, Y, Z);    //T
  74.             points3d[2].Set(X, Y, 0);    //T1
  75.             points3d[3].Set(X, 0, Z);    //T2
  76.             points3d[4].Set(0, Y, Z);    //T3
  77.             points3d[5].Set(X, 0, 0);    //TX
  78.             points3d[6].Set(0, Y, 0);    //TY
  79.             points3d[7].Set(0, 0, Z);    //TZ
  80.  
  81.             // Значения отступа от начала координат для рисования осей
  82.             float axisLength = dimPictureBox.Width * 0.7f;
  83.             points3d[8].Set(axisLength, 0, 0);    //X
  84.             points3d[9].Set(0, axisLength, 0);    //Y
  85.             points3d[10].Set(0, 0, axisLength);   //Z
  86.             points3d[11].Set(-axisLength, 0, 0);  //_X
  87.             points3d[12].Set(0, -axisLength, 0);  //_Y
  88.             points3d[13].Set(0, 0, -axisLength);  //_Z
  89.         }
  90.         void Output()
  91.         {
  92.             DrawDim();
  93.             DrawCom();
  94.             coordinates.Text =
  95.                 "X: " + X +
  96.                 ", Y: " + Y +
  97.                 ", Z: " + Z;
  98.             angles.Text =
  99.                 "A: " + A +
  100.                 ", B: " + B +
  101.                 ", G: " + G;
  102.         }
  103.         void Calculate()
  104.         {
  105.             // Рассчитаем точки для пространственного и комплексного чертежа
  106.             calcDimPoints();
  107.             calcComPoints();
  108.  
  109.             Point3DF T = points3d[1];
  110.             PointF comO = comPoints[0];
  111.  
  112.             float rx = comO.X - Abs(T.Y) * SCALE_COM;
  113.             float ry = comO.Y - Abs(T.Y) * SCALE_COM;
  114.             float rw = Abs(T.Y) * 2 * SCALE_COM;
  115.             float rh = Abs(T.Y) * 2 * SCALE_COM;
  116.             arcRect = new RectangleF(rx, ry, rw, rh);
  117.  
  118.             if (T.Y > 0)
  119.             {
  120.                 angleFrom = 0;
  121.                 angleSweep = 90;
  122.             }
  123.             else if (T.Y < 0)
  124.             {
  125.                 angleFrom = 180;
  126.                 angleSweep = 90;
  127.             }
  128.             else
  129.             {
  130.                 arcRect = new RectangleF();
  131.                 angleFrom = angleSweep = 0;
  132.             }
  133.         }
  134.  
  135.         // Функция берет точку i из массива points3d, вычисляет его двумерные координаты
  136.         // и записывает эту двумерную точку в comPoints[i]
  137.         PointF calcDimPoint(int i)
  138.         {
  139.             Point3DF p = points3d[i];
  140.             float dx1 = (float) ((-p.X * Cos(DegToRad(A))));
  141.             float dx2 = (float) ((p.Y * Cos(DegToRad(180 - B))));
  142.             float dx3 = (float) ((p.Z * Sin(DegToRad(270 - G))));
  143.             float dy1 = (float) ((p.X * Sin(DegToRad(A))));
  144.             float dy2 = (float) ((p.Y * Sin(DegToRad(180 - B))));
  145.             float dy3 = (float) ((-p.Z * Cos(DegToRad(270 - G))));
  146.  
  147.  
  148.             // Итоговые координаты точки
  149.             float x2d = dimPictureBox.Width * 0.5f + (dx1 + dx2 + dx3)  * SCALE_DIM;
  150.             float y2d = dimPictureBox.Height * 0.5f + (dy1 + dy2 + dy3) * SCALE_DIM;
  151.  
  152.             return new PointF(x2d, y2d);
  153.             //dimPoints[i].X = x2d;
  154.             //dimPoints[i].Y = y2d;
  155.         }
  156.         void calcDimPoints()
  157.         {
  158.             for (int i = 0; i < 14; ++i)
  159.                 dimPoints[i] = calcDimPoint(i);
  160.         }
  161.         void calcComPoints()
  162.         {
  163.             float Ox = comPictureBox.Width * 0.5f;
  164.             float Oy = comPictureBox.Height * 0.5f;
  165.  
  166.             PointF O = comPoints[0];
  167.             PointF T1 = comPoints[1];
  168.             PointF T2 = comPoints[2];
  169.             PointF T3 = comPoints[3];
  170.             PointF TX = comPoints[4];
  171.             PointF TY1 = comPoints[5];
  172.             PointF TY2 = comPoints[6];
  173.             PointF TZ = comPoints[7];
  174.             PointF X = comPoints[8];
  175.             PointF Y2 = comPoints[9];
  176.             PointF Z = comPoints[10];
  177.             PointF Y1 = comPoints[11];
  178.  
  179.             comPoints[0].X = Ox; comPoints[0].Y = Oy; //GetComPointXZ(points3d[0] /*O*/);
  180.             comPoints[2] = GetComPointXZ(points3d[3] /*T2*/);
  181.             comPoints[4] = GetComPointXZ(points3d[5] /*TX*/);
  182.             comPoints[7] = GetComPointXZ(points3d[7] /*TZ*/);
  183.  
  184.             comPoints[1] = GetComPointXY(points3d[2] /*T1*/);
  185.             comPoints[5] = GetComPointXY(points3d[6] /*TY1*/);
  186.  
  187.             comPoints[3] = GetComPointYZ(points3d[4] /*T3*/);
  188.             comPoints[6] = GetComPointYZ(points3d[6] /*TY2*/);
  189.  
  190.             comPoints[8] =  GetComPointXZ(points3d[8]  /*X*/);
  191.             comPoints[10] = GetComPointXZ(points3d[10] /*Z*/);
  192.             comPoints[9] =  GetComPointXZ(points3d[11] /*_X*/);
  193.             comPoints[11] = GetComPointXZ(points3d[13] /*_Z*/);
  194.         }
  195.         PointF GetComPoint(Point3DF p)
  196.         {
  197.             float Ox = comPictureBox.Width * 0.5f;
  198.             float Oy = comPictureBox.Height * 0.5f;
  199.  
  200.             float x = Ox - p.X * 1 + p.Y * 1;
  201.             float y = Oy + p.Y * 1 - p.Z * 1;
  202.  
  203.             return new PointF(x, y);
  204.         }
  205.         PointF GetComPointXZ(Point3DF p)
  206.         {
  207.             float Ox = comPictureBox.Width * 0.5f;
  208.             float Oy = comPictureBox.Height * 0.5f;
  209.  
  210.             float x = Ox - p.X * SCALE_COM;
  211.             float y = Oy - p.Z * SCALE_COM;
  212.  
  213.             return new PointF(x, y);
  214.         }
  215.         PointF GetComPointXY(Point3DF p)
  216.         {
  217.             float Ox = comPictureBox.Width * 0.5f;
  218.             float Oy = comPictureBox.Height * 0.5f;
  219.  
  220.             float x = Ox - p.X * SCALE_COM;
  221.             float y = Oy + p.Y * SCALE_COM;
  222.  
  223.             return new PointF(x, y);
  224.         }
  225.         PointF GetComPointYZ(Point3DF p)
  226.         {
  227.             float Ox = comPictureBox.Width * 0.5f;
  228.             float Oy = comPictureBox.Height * 0.5f;
  229.  
  230.             float x = Ox + p.Y * SCALE_COM;
  231.             float y = Oy - p.Z * SCALE_COM;
  232.  
  233.             return new PointF(x, y);
  234.         }
  235.         float DegToRad(float v)
  236.         {
  237.             return (float)((v / 180) * Math.PI);
  238.         }
  239.         private class Point3DF
  240.         {
  241.             public float X;
  242.             public float Y;
  243.             public float Z;
  244.  
  245.             public Point3DF()
  246.             {
  247.                 X = Y = Z = 0;
  248.             }
  249.  
  250.             public Point3DF(float v1, float v2, float v3)
  251.             {
  252.                 this.X = v1;
  253.                 this.Y = v2;
  254.                 this.Z = v3;
  255.             }
  256.  
  257.             public void Set (float x, float y, float z)
  258.             {
  259.                 X = x; Y = y; Z = z;
  260.             }
  261.         }
  262.         private void exitButton_Click(object sender, EventArgs e)
  263.         {
  264.             Environment.Exit(0);
  265.         }
  266.         private void trackBar_Scroll(object sender, EventArgs e)
  267.         {
  268.             Update();
  269.         }
  270.         private void button1_Click(object sender, EventArgs e)
  271.         {
  272.             int h = (int)(dimPictureBox.Width * 0.5);
  273.             trackBarX.Value = h / 2;
  274.             trackBarY.Value = h / 2;
  275.             trackBarZ.Value = h / 2;
  276.             trackBarA.Value = 0;
  277.             trackBarB.Value = 135;
  278.             trackBarG.Value = 270;
  279.  
  280.             Input();
  281.             Calculate();
  282.             Output();
  283.         }
  284.  
  285.         void DrawDim()
  286.         {
  287.             //points3d[0] - O
  288.             //points3d[1] - T
  289.             //points3d[2] - T1
  290.             //points3d[3] - T2
  291.             //points3d[4] - T3
  292.             //points3d[5] - TX
  293.             //points3d[6] - TY
  294.             //points3d[7] - TZ
  295.             //points3d[8] - X
  296.             //points3d[9] - Y
  297.             //points3d[10] - Z
  298.             //points3d[11] - _X
  299.             //points3d[12] - _Y
  300.             //points3d[13] - _Z
  301.  
  302.             Bitmap pictureBoxBitmap = new Bitmap(dimPictureBox.Width, dimPictureBox.Height);
  303.             Graphics g = Graphics.FromImage(pictureBoxBitmap);
  304.             g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
  305.             g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
  306.             g.Clear(Color.White);
  307.  
  308.             // Для удобства запишем точки массива в именованные переменные
  309.             PointF O = dimPoints[0];
  310.             PointF T = dimPoints[1];
  311.  
  312.             PointF TX = dimPoints[5];
  313.             PointF TY = dimPoints[6];
  314.             PointF TZ = dimPoints[7];
  315.  
  316.             PointF T1 = dimPoints[2];
  317.             PointF T2 = dimPoints[3];
  318.             PointF T3 = dimPoints[4];
  319.  
  320.             PointF X = dimPoints[8];
  321.             PointF Y = dimPoints[9];
  322.             PointF Z = dimPoints[10];
  323.             PointF _X = dimPoints[11];
  324.             PointF _Y = dimPoints[12];
  325.             PointF _Z = dimPoints[13];
  326.  
  327.             // Отрисуем оси
  328.             g.DrawLine(dimAxisPen, _X, X);
  329.             g.DrawLine(dimAxisPen, _Y, Y);
  330.             g.DrawLine(dimAxisPen, _Z, Z);
  331.  
  332.             // Отрисуем линии
  333.             g.DrawLine(normalPen, T, T1);
  334.             g.DrawLine(normalPen, T, T2);
  335.             g.DrawLine(normalPen, T, T3);
  336.  
  337.             g.DrawLine(normalPen, O, TX);
  338.             g.DrawLine(normalPen, O, TY);
  339.             g.DrawLine(normalPen, O, TZ);
  340.  
  341.             g.DrawLine(normalPen, TX, T1);
  342.             g.DrawLine(normalPen, TX, T2);
  343.  
  344.             g.DrawLine(normalPen, TY, T1);
  345.             g.DrawLine(normalPen, TY, T3);
  346.  
  347.             g.DrawLine(normalPen, TZ, T2);
  348.             g.DrawLine(normalPen, TZ, T3);
  349.  
  350.             // Отрисуем вершины - точки
  351.             for (int i = 0; i < 8; ++i)
  352.                 DrawVertexAt(g, dimPoints[i]);
  353.  
  354.             // Отрисуем надписи
  355.             g.DrawString("X", textFont, Brushes.Black, X);
  356.             g.DrawString("Y", textFont, Brushes.Black, Y);
  357.             g.DrawString("Z", textFont, Brushes.Black, Z);
  358.             g.DrawString("T",  textFont, Brushes.Black, T);
  359.             g.DrawString("T1", textFont, Brushes.Black, T1);
  360.             g.DrawString("T2", textFont, Brushes.Black, T2);
  361.             g.DrawString("T3", textFont, Brushes.Black, T3);
  362.             g.DrawString("TX", textFont, Brushes.Black, TX);
  363.             g.DrawString("TY", textFont, Brushes.Black, TY);
  364.             g.DrawString("TZ", textFont, Brushes.Black, TZ);
  365.             g.DrawString("O",  textFont, Brushes.Black, O);
  366.  
  367.             dimPictureBox.Image = pictureBoxBitmap;
  368.         }
  369.         void DrawCom()
  370.         {
  371.             Bitmap pictureBoxBitmap = new Bitmap(comPictureBox.Width, comPictureBox.Height);
  372.             Graphics g = Graphics.FromImage(pictureBoxBitmap);
  373.             g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
  374.             g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
  375.             g.Clear(Color.White);
  376.  
  377.             for (int i = 0; i < 8; ++i)
  378.                 DrawVertexAt(g, comPoints[i]);
  379.            
  380.  
  381.             PointF O = comPoints[0];
  382.             PointF T1 = comPoints[1];
  383.             PointF T2 = comPoints[2];
  384.             PointF T3 = comPoints[3];
  385.             PointF TX = comPoints[4];
  386.             PointF TY1 = comPoints[5];
  387.             PointF TY2 = comPoints[6];
  388.             PointF TZ = comPoints[7];
  389.             PointF X = comPoints[8];
  390.             PointF Y2 = comPoints[9];
  391.             PointF Z = comPoints[10];
  392.             PointF Y1 = comPoints[11];
  393.  
  394.  
  395.             g.DrawLine(comAxisPen, X, Y2);
  396.             g.DrawLine(comAxisPen, Z, Y1);
  397.  
  398.             g.DrawLine(normalPen, O, TY1);
  399.             g.DrawLine(normalPen, O, TY2);
  400.             g.DrawLine(normalPen, O, TZ);
  401.             g.DrawLine(normalPen, O, TX);
  402.  
  403.             g.DrawLine(normalPen, T2, TZ);
  404.             g.DrawLine(normalPen, T2, TX);
  405.             g.DrawLine(normalPen, T1, TX);
  406.             g.DrawLine(normalPen, T1, TY1);
  407.             g.DrawLine(normalPen, T3, TZ);
  408.             g.DrawLine(normalPen, T3, TY2);
  409.  
  410.             g.DrawString("T1", textFont, Brushes.Black, T1);
  411.             g.DrawString("T2", textFont, Brushes.Black, T2);
  412.             g.DrawString("T3", textFont, Brushes.Black, T3);
  413.             g.DrawString("TX", textFont, Brushes.Black, TX);
  414.             g.DrawString("TZ", textFont, Brushes.Black, TZ);
  415.             g.DrawString("O", textFont, Brushes.Black, O);
  416.             g.DrawString("TY1", textFont, Brushes.Black, TY1);
  417.             g.DrawString("TY2", textFont, Brushes.Black, TY2);
  418.  
  419.  
  420.             g.DrawString(" X", textFont, Brushes.Black, X.X, X.Y + comAxisPen.Width);
  421.             g.DrawString("-Y", textFont, Brushes.Black, X.X, X.Y - 2 * TEXT_SIZE_EM);
  422.  
  423.             g.DrawString("-X", textFont, Brushes.Black, Y2.X - 2 * TEXT_SIZE_EM, X.Y + comAxisPen.Width);
  424.             g.DrawString(" Y", textFont, Brushes.Black, Y2.X - 2 * TEXT_SIZE_EM, X.Y - 2 * TEXT_SIZE_EM);
  425.  
  426.             g.DrawString(" Z", textFont, Brushes.Black, Z.X - comAxisPen.Width - 2 * TEXT_SIZE_EM, Z.Y);
  427.             g.DrawString("-Y", textFont, Brushes.Black, Z.X, Z.Y);
  428.  
  429.             g.DrawString("-Z", textFont, Brushes.Black, Y1.X - comAxisPen.Width - 2 * TEXT_SIZE_EM, Y1.Y - 2 * TEXT_SIZE_EM);
  430.             g.DrawString("Y", textFont, Brushes.Black, Y1.X + comAxisPen.Width, Y1.Y - 2 * TEXT_SIZE_EM);
  431.  
  432.             if (angleSweep != 0)
  433.                 g.DrawArc(Pens.Black, arcRect, angleFrom, angleSweep);
  434.  
  435.             comPictureBox.Image = pictureBoxBitmap;
  436.         }
  437.         private void DrawVertexAt(Graphics g, PointF t)
  438.         {
  439.             const float r = 3f;
  440.             g.FillEllipse(Brushes.Black, t.X - r, t.Y - r, 2 * r, 2 * r);
  441.         }
  442.     }
  443. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement