Advertisement
ddqof

Untitled

Nov 17th, 2019
302
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.             var upd = ManipulatorTask.MoveManipulatorTo(X, Y, Alpha);
  62.             if (!double.IsNaN(upd[0]) && !double.IsNaN(upd[1]) && !double.IsNaN(upd[2]))
  63.             {
  64.                 Shoulder = upd[0];
  65.                 Elbow = upd[1];
  66.                 Wrist = upd[2];
  67.             }
  68.         }
  69.  
  70.         public static void DrawManipulator(Graphics graphics, PointF shoulderPos)
  71.         {
  72.             var joints = AnglesToCoordinatesTask.GetJointPositions(Shoulder, Elbow, Wrist);
  73.  
  74.             graphics.DrawString(
  75.                 $"X={X:0}, Y={Y:0}, Alpha={Alpha:0.00}",
  76.                 new Font(SystemFonts.DefaultFont.FontFamily, 12),
  77.                 Brushes.DarkRed,
  78.                 10,
  79.                 10);
  80.             DrawReachableZone(graphics, ReachableAreaBrush, UnreachableAreaBrush, shoulderPos, joints);
  81.  
  82.             for (var i = 0; i < 3; i++)
  83.             {
  84.                 joints[i] = ConvertMathToWindow(joints[i], shoulderPos);
  85.             }
  86.  
  87.             graphics.DrawLine(ManipulatorPen, shoulderPos.X, shoulderPos.Y, joints[0].X, joints[0].Y);
  88.             graphics.DrawLine(ManipulatorPen, joints[0].X, joints[0].Y, joints[1].X, joints[1].Y);
  89.             graphics.DrawLine(ManipulatorPen, joints[1].X, joints[1].Y, joints[2].X, joints[2].Y);
  90.  
  91.  
  92.             graphics.FillEllipse(JointBrush, shoulderPos.X, shoulderPos.Y, 1, 1);
  93.             graphics.FillEllipse(JointBrush, joints[0].X, joints[0].Y, 1, 1);
  94.             graphics.FillEllipse(JointBrush, joints[1].X, joints[1].Y, 1, 1);
  95.             graphics.FillEllipse(JointBrush, joints[2].X, joints[2].Y, 1, 1);
  96.         }
  97.             // Нарисуйте сегменты манипулятора методом graphics.DrawLine используя ManipulatorPen.
  98.             // Нарисуйте суставы манипулятора окружностями методом graphics.FillEllipse используя JointBrush.
  99.             // Не забудьте сконвертировать координаты из логических в оконные
  100.  
  101.         private static void DrawReachableZone(
  102.             Graphics graphics,
  103.             Brush reachableBrush,
  104.             Brush unreachableBrush,
  105.             PointF shoulderPos,
  106.             PointF[] joints)
  107.         {
  108.             var rmin = Math.Abs(Manipulator.UpperArm - Manipulator.Forearm);
  109.             var rmax = Manipulator.UpperArm + Manipulator.Forearm;
  110.             var mathCenter = new PointF(joints[2].X - joints[1].X, joints[2].Y - joints[1].Y);
  111.             var windowCenter = ConvertMathToWindow(mathCenter, shoulderPos);
  112.             graphics.FillEllipse(reachableBrush, windowCenter.X - rmax, windowCenter.Y - rmax, 2 * rmax, 2 * rmax);
  113.             graphics.FillEllipse(unreachableBrush, windowCenter.X - rmin, windowCenter.Y - rmin, 2 * rmin, 2 * rmin);
  114.         }
  115.  
  116.         public static PointF GetShoulderPos(Form form)
  117.         {
  118.             return new PointF(form.ClientSize.Width / 2f, form.ClientSize.Height / 2f);
  119.         }
  120.  
  121.         public static PointF ConvertMathToWindow(PointF mathPoint, PointF shoulderPos)
  122.         {
  123.             return new PointF(mathPoint.X + shoulderPos.X, shoulderPos.Y - mathPoint.Y);
  124.         }
  125.  
  126.         public static PointF ConvertWindowToMath(PointF windowPoint, PointF shoulderPos)
  127.         {
  128.             return new PointF(windowPoint.X - shoulderPos.X, shoulderPos.Y - windowPoint.Y);
  129.         }
  130.  
  131.     }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement