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
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 WindowsFormsApplication5
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
double[] knots;
public double[] getKnots(int n, int k, bool isOpen)
{
int size = n + k;
double[] knots = new double[size];
int temp = 0;
if (isOpen)
{
for (int i = 0; i < size; ++i)
{
if (i >= k && size - i >= k)
{
++temp;
}
knots[i] = temp;
}
}
else
{
for (int i = 0; i < size; ++i)
{
knots[i] = temp;
temp++;
}
}
return knots;
}
private double getN(int i, int k, double t)
{
double rez = 0;
if (k == 1)
{
if (t >= knots[i] && t <= knots[i + 1])
return 1;
else
return 0;
}
else
{
if ((knots[i + k - 1] - knots[i]) != 0)
rez += (t - knots[i]) * getN(i, k - 1, t) / (knots[i + k - 1] - knots[i]);
if ((knots[i + k] - knots[i + 1]) != 0)
rez += (knots[i + k] - t) * getN(i + 1, k - 1, t) / (knots[i + k] - knots[i + 1]);
}
return rez;
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
g.TranslateTransform(this.ClientSize.Width / 2, this.ClientSize.Height / 2);
int n = 4;
int k = 3;
knots = getKnots(n, k, true);
double x = 0, y = 0;
for (int i = 0; i < n; i++)
{
for (double t = knots[0]; t <= knots[knots.Length - 1]; t += 0.001)
{
y = -100 * getN(i, k, t);
x = 50 * t - 100 ;
g.DrawEllipse(new Pen(Color.Black), (int)x, (int)y, 2, 2);
}
}
}
}
}