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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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 BSpline
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int activeId = -1;
float[,] mtrx = new float[4, 4] { { 100, 100, 100, 100 }, { 200, 200, 200, 200 }, { 300, 300, 300, 300 }, { 400, 400, 400, 400 } };
float[,] mtry = new float[4, 4] { { 100, 200, 300, 400 }, { 100, 200, 300, 400 }, { 100, 200, 300, 400 }, { 100, 200, 300, 400 } };
float[,] mult(float[,] a, float[,] b)
{
float[,] temp = new float[a.GetLength(0), b.GetLength(1)];
for (int i = 0; i < a.GetLength(0); ++i)
{
for (int l = 0; l < b.GetLength(1); ++l)
{
float ans = 0;
for (int k = 0; k < a.GetLength(1); ++k)
{
ans += a[i, k] * b[k, l];
}
temp[i, l] = ans;
}
}
return temp;
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics v = e.Graphics;
// v.TranslateTransform(100, 100);
for (int i = 0; i < 4; ++i)
for (int j = 0; j < 4; ++j)
{
v.DrawEllipse(new Pen(Color.Purple), mtrx[i, j], mtry[i, j], 3, 3);
}
for (float u = 0; u <= 1; u += 0.06f)
for (float t = 0; t <= 1; t += 0.06f)
{
float[,] a = new float[1, 4] {{ u * u * u, 3 * u * u * (1 - u), 3 * u * (1 - u) * (1 - u), (1 - u) * (1 - u) * (1 - u)}};
float[,] b = new float[4, 1] { { t * t * t}, {3 * t * t * (1 - t)}, {3 * t * (1 - t) * (1 - t)}, {(1 - t) * (1 - t) * (1 - t) } };
float[,] cx = mult(a, mtrx);
cx = mult(cx, b);
float[,] bx = mult(a, mtry);
bx = mult(bx, b);
v.DrawEllipse(new Pen(Color.Red), cx[0, 0], bx[0, 0], 1, 1);
}
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (activeId == 1)
{
mtrx[2, 1] = e.X;
mtry[2, 1] = e.Y;
}
Invalidate();
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
activeId = 1;
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
activeId = -1;
}
}
}