Advertisement
ddqof

Untitled

Nov 17th, 2019
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.67 KB | None | 0 0
  1. using System;
  2. using System.Drawing;
  3. using System.Linq;
  4. using System.Windows.Forms;
  5.  
  6. namespace Manipulation
  7. {
  8.     public static class VisualizerTask
  9.     {
  10.         public static double X = 220;
  11.         public static double Y = -100;
  12.         public static double Alpha = 0.05;
  13.         public static double Wrist = 2 * Math.PI / 3;
  14.         public static double Elbow = 3 * Math.PI / 4;
  15.         public static double Shoulder = Math.PI / 2;
  16.  
  17.         public static Brush UnreachableAreaBrush = new SolidBrush(Color.FromArgb(255, 255, 230, 230));
  18.         public static Brush ReachableAreaBrush = new SolidBrush(Color.FromArgb(255, 230, 255, 230));
  19.         public static Pen ManipulatorPen = new Pen(Color.Black, 3);
  20.         public static Brush JointBrush = Brushes.Gray;
  21.  
  22.         public static void KeyDown(Form form, KeyEventArgs key)
  23.         {
  24.             if (key.KeyCode == Keys.Q)
  25.                 Shoulder += 0.01;
  26.             if (key.KeyCode == Keys.A)
  27.                 Shoulder -= 0.01;
  28.             if (key.KeyCode == Keys.W)
  29.                 Elbow += 0.01;
  30.             if (key.KeyCode == Keys.S)
  31.                 Elbow -= 0.01;
  32.             Wrist = -Alpha - Shoulder - Elbow;
  33.             // TODO: Добавьте реакцию на QAWS и пересчитывать Wrist
  34.             form.Invalidate(); //
  35.         }
  36.  
  37.  
  38.         public static void MouseMove(Form form, MouseEventArgs e)
  39.         {
  40.             // TODO: Измените X и Y пересчитав координаты (e.X, e.Y) в логические.
  41.             var change = ConvertMathToWindow(new PointF(e.X, e.Y), GetShoulderPos(form));
  42.             X = change.X;
  43.             Y = change.Y;
  44.             UpdateManipulator();
  45.             form.Invalidate();
  46.         }
  47.  
  48.         public static void MouseWheel(Form form, MouseEventArgs e)
  49.         {
  50.             // TODO: Измените Alpha, используя e.Delta — размер прокрутки колеса мыши
  51.             if (e.Delta > 0) Alpha += e.Delta;
  52.             else if (e.Delta < 0) Alpha += e.Delta;
  53.             UpdateManipulator();
  54.             form.Invalidate();
  55.         }
  56.  
  57.         public static void UpdateManipulator()
  58.         {
  59.             // Вызовите ManipulatorTask.MoveManipulatorTo и обновите значения полей Shoulder, Elbow и Wrist,
  60.             // если они не NaN. Это понадобится для последней задачи.
  61.             if (Shoulder != double.NaN && Elbow != double.NaN && Wrist != double.NaN)
  62.                 ManipulatorTask.MoveManipulatorTo(Shoulder, Elbow, Wrist);
  63.         }
  64.  
  65.         public static void DrawManipulator(Graphics graphics, PointF shoulderPos)
  66.         {
  67.             var joints = AnglesToCoordinatesTask.GetJointPositions(Shoulder, Elbow, Wrist);
  68.  
  69.             graphics.DrawString(
  70.                 $"X={X:0}, Y={Y:0}, Alpha={Alpha:0.00}",
  71.                 new Font(SystemFonts.DefaultFont.FontFamily, 12),
  72.                 Brushes.DarkRed,
  73.                 10,
  74.                 10);
  75.             DrawReachableZone(graphics, ReachableAreaBrush, UnreachableAreaBrush, shoulderPos, joints);
  76.  
  77.             for (var i = 0; i < 3; i++)
  78.             {
  79.                 joints[i] = ConvertMathToWindow(joints[i], shoulderPos);
  80.             }
  81.  
  82.             graphics.DrawLine(ManipulatorPen, shoulderPos.X, shoulderPos.Y, joints[0].X, joints[0].Y);
  83.             graphics.DrawLine(ManipulatorPen, joints[0].X, joints[0].Y, joints[1].X, joints[1].Y);
  84.             graphics.DrawLine(ManipulatorPen, joints[1].X, joints[1].Y, joints[2].X, joints[2].Y);
  85.  
  86.  
  87.             graphics.FillEllipse(JointBrush, shoulderPos.X, shoulderPos.Y, 1, 1);
  88.             graphics.FillEllipse(JointBrush, joints[0].X, joints[0].Y, 1, 1);
  89.             graphics.FillEllipse(JointBrush, joints[1].X, joints[1].Y, 1, 1);
  90.             graphics.FillEllipse(JointBrush, joints[2].X, joints[2].Y, 1, 1);
  91.         }
  92.             // Нарисуйте сегменты манипулятора методом graphics.DrawLine используя ManipulatorPen.
  93.             // Нарисуйте суставы манипулятора окружностями методом graphics.FillEllipse используя JointBrush.
  94.             // Не забудьте сконвертировать координаты из логических в оконные
  95.  
  96.         private static void DrawReachableZone(
  97.             Graphics graphics,
  98.             Brush reachableBrush,
  99.             Brush unreachableBrush,
  100.             PointF shoulderPos,
  101.             PointF[] joints)
  102.         {
  103.             var rmin = Math.Abs(Manipulator.UpperArm - Manipulator.Forearm);
  104.             var rmax = Manipulator.UpperArm + Manipulator.Forearm;
  105.             var mathCenter = new PointF(joints[2].X - joints[1].X, joints[2].Y - joints[1].Y);
  106.             var windowCenter = ConvertMathToWindow(mathCenter, shoulderPos);
  107.             graphics.FillEllipse(reachableBrush, windowCenter.X - rmax, windowCenter.Y - rmax, 2 * rmax, 2 * rmax);
  108.             graphics.FillEllipse(unreachableBrush, windowCenter.X - rmin, windowCenter.Y - rmin, 2 * rmin, 2 * rmin);
  109.         }
  110.  
  111.         public static PointF GetShoulderPos(Form form)
  112.         {
  113.             return new PointF(form.ClientSize.Width / 2f, form.ClientSize.Height / 2f);
  114.         }
  115.  
  116.         public static PointF ConvertMathToWindow(PointF mathPoint, PointF shoulderPos)
  117.         {
  118.             return new PointF(mathPoint.X + shoulderPos.X, shoulderPos.Y - mathPoint.Y);
  119.         }
  120.  
  121.         public static PointF ConvertWindowToMath(PointF windowPoint, PointF shoulderPos)
  122.         {
  123.             return new PointF(windowPoint.X - shoulderPos.X, shoulderPos.Y - windowPoint.Y);
  124.         }
  125.  
  126.     }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement