Advertisement
Guest User

Untitled

a guest
Nov 21st, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.76 KB | None | 0 0
  1. public partial class Form1 : Form
  2.     {
  3.         Graphics g;
  4.  
  5.         List<PointF> P = new List<PointF>();
  6.  
  7.         int index = -1;
  8.         int s = 4;
  9.  
  10.         Pen penPoly = new Pen(Color.Gray, 1f);
  11.         Pen penCurve = new Pen(Color.Black, 3f);
  12.  
  13.         Brush brushP = new SolidBrush(Color.Gray);
  14.  
  15.         float B0(float t)
  16.         {
  17.             return (1 - t) * (1 - t) * (1 - t);
  18.         }
  19.         float B1(float t)
  20.         {
  21.             return 3 * t * (1 - t) * (1 - t);
  22.         }
  23.         float B2(float t)
  24.         {
  25.             return 3 * t * t * (1 - t);
  26.         }
  27.         float B3(float t)
  28.         {
  29.             return t * t * t;
  30.         }
  31.         float Bx(float t)
  32.         {
  33.             return B0(t) * P[0].X + B1(t) * P[1].X + B2(t) * P[2].X + B3(t) * P[3].X;
  34.         }
  35.         float By(float t)
  36.         {
  37.             return B0(t) * P[0].Y + B1(t) * P[1].Y + B2(t) * P[2].Y + B3(t) * P[3].Y;
  38.         }
  39.         void DrawBezier()
  40.         {
  41.             float x0, y0, x1, y1;
  42.             float t = 0f;
  43.             float h = 1f / 250f;
  44.             x0 = Bx(t);
  45.             y0 = By(t);
  46.             while(t < 1f)
  47.             {
  48.                 t += h;
  49.                 x1 = Bx(t);
  50.                 y1 = By(t);
  51.                 g.DrawLine(penCurve, x0, y0, x1, y1);
  52.                 x0 = x1;
  53.                 y0 = y1;
  54.             }
  55.         }
  56.  
  57.         public Form1()
  58.         {
  59.             InitializeComponent();
  60.         }
  61.  
  62.         private void canvas_Paint(object sender, PaintEventArgs e)
  63.         {
  64.             g = e.Graphics;
  65.             g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
  66.  
  67.             for (int i = 0; i < P.Count - 1; i++)
  68.                 g.DrawLine(penPoly, P[i], P[i + 1]);
  69.  
  70.             if (P.Count == 4)
  71.                 DrawBezier();
  72.  
  73.             for (int i = 0; i < P.Count; i++)
  74.                 g.FillRectangle(brushP, P[i].X - s, P[i].Y - s, 2 * s, 2 * s);
  75.         }
  76.         private void canvas_MouseDown(object sender, MouseEventArgs e)
  77.         {
  78.             for (int i = 0; i < P.Count; i++)
  79.             {
  80.                 if (Math.Abs(P[i].X - e.X) <= s &&
  81.                     Math.Abs(P[i].Y - e.Y) <= s)
  82.                     index = i;
  83.             }
  84.  
  85.             if (index == -1 && P.Count < 4)
  86.             {
  87.                 P.Add(e.Location);
  88.                 index = P.Count - 1;
  89.                 canvas.Refresh();
  90.             }
  91.         }
  92.         private void canvas_MouseMove(object sender, MouseEventArgs e)
  93.         {
  94.             if (index != -1)
  95.             {
  96.                 P[index] = e.Location;
  97.                 canvas.Refresh();
  98.             }
  99.         }
  100.         private void canvas_MouseUp(object sender, MouseEventArgs e)
  101.         {
  102.             index = -1;
  103.         }
  104.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement