Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public partial class Form1 : Form
- {
- Graphics g;
- List<PointF> P = new List<PointF>();
- int index = -1;
- int s = 4;
- Pen penPoly = new Pen(Color.Gray, 1f);
- Pen penCurve = new Pen(Color.Black, 3f);
- Brush brushP = new SolidBrush(Color.Gray);
- float B0(float t)
- {
- return (1 - t) * (1 - t) * (1 - t);
- }
- float B1(float t)
- {
- return 3 * t * (1 - t) * (1 - t);
- }
- float B2(float t)
- {
- return 3 * t * t * (1 - t);
- }
- float B3(float t)
- {
- return t * t * t;
- }
- float Bx(float t)
- {
- return B0(t) * P[0].X + B1(t) * P[1].X + B2(t) * P[2].X + B3(t) * P[3].X;
- }
- float By(float t)
- {
- return B0(t) * P[0].Y + B1(t) * P[1].Y + B2(t) * P[2].Y + B3(t) * P[3].Y;
- }
- void DrawBezier()
- {
- float x0, y0, x1, y1;
- float t = 0f;
- float h = 1f / 250f;
- x0 = Bx(t);
- y0 = By(t);
- while(t < 1f)
- {
- t += h;
- x1 = Bx(t);
- y1 = By(t);
- g.DrawLine(penCurve, x0, y0, x1, y1);
- x0 = x1;
- y0 = y1;
- }
- }
- public Form1()
- {
- InitializeComponent();
- }
- private void canvas_Paint(object sender, PaintEventArgs e)
- {
- g = e.Graphics;
- g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
- for (int i = 0; i < P.Count - 1; i++)
- g.DrawLine(penPoly, P[i], P[i + 1]);
- if (P.Count == 4)
- DrawBezier();
- for (int i = 0; i < P.Count; i++)
- g.FillRectangle(brushP, P[i].X - s, P[i].Y - s, 2 * s, 2 * s);
- }
- private void canvas_MouseDown(object sender, MouseEventArgs e)
- {
- for (int i = 0; i < P.Count; i++)
- {
- if (Math.Abs(P[i].X - e.X) <= s &&
- Math.Abs(P[i].Y - e.Y) <= s)
- index = i;
- }
- if (index == -1 && P.Count < 4)
- {
- P.Add(e.Location);
- index = P.Count - 1;
- canvas.Refresh();
- }
- }
- private void canvas_MouseMove(object sender, MouseEventArgs e)
- {
- if (index != -1)
- {
- P[index] = e.Location;
- canvas.Refresh();
- }
- }
- private void canvas_MouseUp(object sender, MouseEventArgs e)
- {
- index = -1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement