using System using System Collections Generic using System ComponentMo

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Bezier
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public PointF[] pt = new PointF[4] {new PointF(4f, 230f),
new PointF(110f, 20f),
new PointF(209f, 128f),
new PointF(360f, 17f)};
private void Form1_Paint(object sender, PaintEventArgs e)
{
PointF[] output = new PointF[1000];
for (int t = 0; t < 1000; ++t)
{
float cur_t = (float)t / 1000f;
float X = (1f - cur_t) * (1f - cur_t) * (1f - cur_t) * pt[0].X +
3f * cur_t * (1f - cur_t) * (1f - cur_t) * pt[1].X +
3f * cur_t * cur_t * (1f - cur_t) * pt[2].X +
cur_t * cur_t * cur_t * pt[3].X;
float Y = (1f - cur_t) * (1f - cur_t) * (1f - cur_t) * pt[0].Y +
3f * cur_t * (1f - cur_t) * (1f - cur_t) * pt[1].Y +
3f * cur_t * cur_t * (1f - cur_t) * pt[2].Y +
cur_t * cur_t * cur_t * pt[3].Y;
output[t] = new PointF(X, Y);
}
Graphics g = e.Graphics;
g.DrawLines(new Pen(Color.Black), pt);
g.DrawEllipse(new Pen(Color.Black), pt[0].X - 5, pt[0].Y - 5, 10, 10);
g.DrawLines(new Pen(Color.Black, 3), output);
}
private bool isDragging = false;
private void Form1_Load(object sender, EventArgs e)
{
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (this.isDragging)
{
pt[0].X = e.X;
pt[0].Y = e.Y;
this.Invalidate();
}
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
if (Math.Abs(pt[0].X - e.X) < 10 && Math.Abs(pt[0].Y - e.Y) < 10)
{
this.isDragging = true;
}
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
this.isDragging = false;
}
}
}