Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.85 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. switch (key.KeyData)
  25. {
  26. case Keys.Q:
  27. Shoulder += 0.1;
  28. break;
  29. case Keys.A:
  30. Shoulder -= 0.1;
  31. break;
  32. case Keys.W:
  33. Elbow += 0.1;
  34. break;
  35. case Keys.S:
  36. Elbow -= 0.1;
  37. break;
  38. }
  39. Wrist = -Alpha - Shoulder - Elbow;
  40. form.Invalidate();
  41. }
  42.  
  43.  
  44. public static void MouseMove(Form form, MouseEventArgs e)
  45. {
  46. var shol = GetShoulderPos(form);
  47.  
  48. var es = ConvertWindowToMath(new PointF(e.X, e.Y), new PointF(shol.X, shol.Y));
  49. Y = es.Y;
  50. X = es.X;
  51. // TODO: Измените X и Y пересчитав координаты (e.X, e.Y) в логические.
  52.  
  53. //UpdateManipulator();
  54. form.Invalidate();
  55. }
  56.  
  57. public static void MouseWheel(Form form, MouseEventArgs e)
  58. {
  59. Alpha += e.Delta;
  60. Wrist = -Alpha - Shoulder - Elbow;
  61. // TODO: Измените Alpha, используя e.Delta — размер прокрутки колеса мыши
  62.  
  63. //a UpdateManipulator();
  64. form.Invalidate();
  65. }
  66.  
  67. public static void UpdateManipulator()
  68. {
  69. var a = ManipulatorTask.MoveManipulatorTo(X, Y, Alpha);
  70. Shoulder = a[0] == double.NaN ? Shoulder : a[0];
  71. Elbow = a[1] == double.NaN ? Elbow : a[1];
  72. Wrist = a[2] == double.NaN ? Wrist : a[2];
  73. // Вызовите ManipulatorTask.MoveManipulatorTo и обновите значения полей Shoulder, Elbow и Wrist,
  74. // если они не NaN. Это понадобится для последней задачи.
  75. }
  76.  
  77. public static void DrawManipulator(Graphics graphics, PointF shoulderPos)
  78. {
  79. var joints = AnglesToCoordinatesTask.GetJointPositions(Shoulder, Elbow, Wrist);
  80.  
  81. graphics.DrawString(
  82. $"X={X:0}, Y={Y:0}, Alpha={Alpha:0.00}",
  83. new Font(SystemFonts.DefaultFont.FontFamily, 12),
  84. Brushes.DarkRed,
  85. 10,
  86. 10);
  87. DrawReachableZone(graphics, ReachableAreaBrush, UnreachableAreaBrush, shoulderPos, joints);
  88.  
  89. var h = AnglesToCoordinatesTask.GetJointPositions(Shoulder, Elbow, Wrist);
  90. var elbowPos = new PointF(shoulderPos.X + h[0].X, shoulderPos.Y + h[0].Y);
  91. var wrist = new PointF(elbowPos.X + h[1].X, elbowPos.Y + h[1].Y);
  92.  
  93. graphics.DrawLine(ManipulatorPen, shoulderPos.X, shoulderPos.Y, elbowPos.X, elbowPos.Y);
  94. graphics.DrawLine(ManipulatorPen, elbowPos.X, elbowPos.Y, wrist.X, wrist.Y);
  95. graphics.DrawLine(ManipulatorPen, wrist.X, wrist.Y, wrist.X + h[2].X, wrist.Y + h[2].Y);
  96.  
  97. graphics.FillEllipse(JointBrush, shoulderPos.X-10, shoulderPos.Y-10, 20, 20);
  98. graphics.FillEllipse(JointBrush, elbowPos.X-10, elbowPos.Y-10, 20, 20);
  99. graphics.FillEllipse(JointBrush, wrist.X-10, wrist.Y-10, 20, 20);
  100. // Нарисуйте сегменты манипулятора методом graphics.DrawLine используя ManipulatorPen.
  101. // Нарисуйте суставы манипулятора окружностями методом graphics.FillEllipse используя JointBrush.
  102. // Не забудьте сконвертировать координаты из логических в оконные
  103. }
  104.  
  105. private static void DrawReachableZone(
  106. Graphics graphics,
  107. Brush reachableBrush,
  108. Brush unreachableBrush,
  109. PointF shoulderPos,
  110. PointF[] joints)
  111. {
  112. var rmin = Math.Abs(Manipulator.UpperArm - Manipulator.Forearm);
  113. var rmax = Manipulator.UpperArm + Manipulator.Forearm;
  114. var mathCenter = new PointF(joints[2].X - joints[1].X, joints[2].Y - joints[1].Y);
  115. var windowCenter = ConvertMathToWindow(mathCenter, shoulderPos);
  116. graphics.FillEllipse(reachableBrush, windowCenter.X - rmax, windowCenter.Y - rmax, 2 * rmax, 2 * rmax);
  117. graphics.FillEllipse(unreachableBrush, windowCenter.X - rmin, windowCenter.Y - rmin, 2 * rmin, 2 * rmin);
  118. }
  119.  
  120. public static PointF GetShoulderPos(Form form)
  121. {
  122. return new PointF(form.ClientSize.Width / 2f, form.ClientSize.Height / 2f);
  123. }
  124.  
  125. public static PointF ConvertMathToWindow(PointF mathPoint, PointF shoulderPos)
  126. {
  127. return new PointF(mathPoint.X + shoulderPos.X, shoulderPos.Y - mathPoint.Y);
  128. }
  129.  
  130. public static PointF ConvertWindowToMath(PointF windowPoint, PointF shoulderPos)
  131. {
  132. return new PointF(windowPoint.X - shoulderPos.X, shoulderPos.Y - windowPoint.Y);
  133. }
  134.  
  135. }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement