Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Drawing;
- using System.Linq;
- using System.Windows.Forms;
- namespace Manipulation
- {
- public static class VisualizerTask
- {
- public static double X = 220;
- public static double Y = -100;
- public static double Alpha = 0.05;
- public static double Wrist = 2 * Math.PI / 3;
- public static double Elbow = 3 * Math.PI / 4;
- public static double Shoulder = Math.PI / 2;
- public static Brush UnreachableAreaBrush = new SolidBrush(Color.FromArgb(255, 255, 230, 230));
- public static Brush ReachableAreaBrush = new SolidBrush(Color.FromArgb(255, 230, 255, 230));
- public static Pen ManipulatorPen = new Pen(Color.Black, 3);
- public static Brush JointBrush = Brushes.Gray;
- public static void KeyDown(Form form, KeyEventArgs key)
- {
- switch (key.KeyData)
- {
- case Keys.Q:
- Shoulder += 0.1;
- break;
- case Keys.A:
- Shoulder -= 0.1;
- break;
- case Keys.W:
- Elbow += 0.1;
- break;
- case Keys.S:
- Elbow -= 0.1;
- break;
- }
- Wrist = -Alpha - Shoulder - Elbow;
- form.Invalidate();
- }
- public static void MouseMove(Form form, MouseEventArgs e)
- {
- var shol = GetShoulderPos(form);
- var es = ConvertWindowToMath(new PointF(e.X, e.Y), new PointF(shol.X, shol.Y));
- Y = es.Y;
- X = es.X;
- // TODO: Измените X и Y пересчитав координаты (e.X, e.Y) в логические.
- //UpdateManipulator();
- form.Invalidate();
- }
- public static void MouseWheel(Form form, MouseEventArgs e)
- {
- Alpha += e.Delta;
- Wrist = -Alpha - Shoulder - Elbow;
- // TODO: Измените Alpha, используя e.Delta — размер прокрутки колеса мыши
- //a UpdateManipulator();
- form.Invalidate();
- }
- public static void UpdateManipulator()
- {
- var a = ManipulatorTask.MoveManipulatorTo(X, Y, Alpha);
- Shoulder = a[0] == double.NaN ? Shoulder : a[0];
- Elbow = a[1] == double.NaN ? Elbow : a[1];
- Wrist = a[2] == double.NaN ? Wrist : a[2];
- // Вызовите ManipulatorTask.MoveManipulatorTo и обновите значения полей Shoulder, Elbow и Wrist,
- // если они не NaN. Это понадобится для последней задачи.
- }
- public static void DrawManipulator(Graphics graphics, PointF shoulderPos)
- {
- var joints = AnglesToCoordinatesTask.GetJointPositions(Shoulder, Elbow, Wrist);
- graphics.DrawString(
- $"X={X:0}, Y={Y:0}, Alpha={Alpha:0.00}",
- new Font(SystemFonts.DefaultFont.FontFamily, 12),
- Brushes.DarkRed,
- 10,
- 10);
- DrawReachableZone(graphics, ReachableAreaBrush, UnreachableAreaBrush, shoulderPos, joints);
- var h = AnglesToCoordinatesTask.GetJointPositions(Shoulder, Elbow, Wrist);
- var elbowPos = new PointF(shoulderPos.X + h[0].X, shoulderPos.Y + h[0].Y);
- var wrist = new PointF(elbowPos.X + h[1].X, elbowPos.Y + h[1].Y);
- graphics.DrawLine(ManipulatorPen, shoulderPos.X, shoulderPos.Y, elbowPos.X, elbowPos.Y);
- graphics.DrawLine(ManipulatorPen, elbowPos.X, elbowPos.Y, wrist.X, wrist.Y);
- graphics.DrawLine(ManipulatorPen, wrist.X, wrist.Y, wrist.X + h[2].X, wrist.Y + h[2].Y);
- graphics.FillEllipse(JointBrush, shoulderPos.X-10, shoulderPos.Y-10, 20, 20);
- graphics.FillEllipse(JointBrush, elbowPos.X-10, elbowPos.Y-10, 20, 20);
- graphics.FillEllipse(JointBrush, wrist.X-10, wrist.Y-10, 20, 20);
- // Нарисуйте сегменты манипулятора методом graphics.DrawLine используя ManipulatorPen.
- // Нарисуйте суставы манипулятора окружностями методом graphics.FillEllipse используя JointBrush.
- // Не забудьте сконвертировать координаты из логических в оконные
- }
- private static void DrawReachableZone(
- Graphics graphics,
- Brush reachableBrush,
- Brush unreachableBrush,
- PointF shoulderPos,
- PointF[] joints)
- {
- var rmin = Math.Abs(Manipulator.UpperArm - Manipulator.Forearm);
- var rmax = Manipulator.UpperArm + Manipulator.Forearm;
- var mathCenter = new PointF(joints[2].X - joints[1].X, joints[2].Y - joints[1].Y);
- var windowCenter = ConvertMathToWindow(mathCenter, shoulderPos);
- graphics.FillEllipse(reachableBrush, windowCenter.X - rmax, windowCenter.Y - rmax, 2 * rmax, 2 * rmax);
- graphics.FillEllipse(unreachableBrush, windowCenter.X - rmin, windowCenter.Y - rmin, 2 * rmin, 2 * rmin);
- }
- public static PointF GetShoulderPos(Form form)
- {
- return new PointF(form.ClientSize.Width / 2f, form.ClientSize.Height / 2f);
- }
- public static PointF ConvertMathToWindow(PointF mathPoint, PointF shoulderPos)
- {
- return new PointF(mathPoint.X + shoulderPos.X, shoulderPos.Y - mathPoint.Y);
- }
- public static PointF ConvertWindowToMath(PointF windowPoint, PointF shoulderPos)
- {
- return new PointF(windowPoint.X - shoulderPos.X, shoulderPos.Y - windowPoint.Y);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement