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
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
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 void displayfunc(int pointCount, int degree, float[] arr, out int sz)
{
int k = degree + 1;
int n = pointCount - 1;
for (int i = 1; i <= n + k + 1; ++i)
{
if (i <= k)
{
arr[i - 1] = 0;
}
else
if (i <= n + 1)
{
arr[i - 1] = i - k;
}
else
{
arr[i - 1] = n - k + 2;
}
}
sz = n + k + 1;
}
public Form1()
{
InitializeComponent();
displayfunc(pCount, degree, knotArr, out knotSize);
tmax = knotArr[knotSize - 1];
}
int activeId = -1;
float[] knotArr = new float[]{0.0f, 0.0f, 0.0f, 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 6.0f, 6.0f};
int degree = 2;
int pCount = 6;
float tmin = 0;
float tmax = 0;
int knotSize = 11;
float getNRec(float t, int ind, int k)
{
if (k == 1)
{
if (knotArr[ind] <= t && knotArr[ind + 1] > t)
return 1;
else return 0;
}
float retval = 0;
float vl1 = ((t - knotArr[ind]) * getNRec(t, ind, k - 1));
float vl2 = (knotArr[ind + k - 1] - knotArr[ind]);
if (vl2 > 0.00000001)
retval += vl1 / vl2;
vl1 = (knotArr[ind + k] - t) * getNRec(t, ind + 1, k - 1);
vl2 = (knotArr[ind + k] - knotArr[ind + 1]);
if (vl2 > 0.00000001)
retval += vl1 / vl2;
return retval;
}
float getN(float t, int ind, int k)
{
int n = pCount;
float[,] koeffs = new float[knotArr.Length, knotArr.Length];
for (int i = 0; i < n; ++i)
{
if (knotArr[i] <= t && knotArr[i + 1] > t)
{
koeffs[1, i] = 1;
}
else {
koeffs[1, i] = 0;
}
}
for (int lk = 2; lk <= k; ++lk)
{
for (int i = 0; i < n; ++i)
{
float retval = 0;
float vl1 = ((t - knotArr[i]) * koeffs[lk - 1, i]);
float vl2 = (knotArr[i + lk - 1] - knotArr[i]);
if (vl2 > 0.00000001)
retval += vl1 / vl2;
vl1 = (knotArr[i + lk] - t) * koeffs[lk - 1, i + 1];
vl2 = (knotArr[i + lk] - knotArr[i + 1]);
if (vl2 > 0.00000001)
retval += vl1 / vl2;
koeffs[lk, i] = retval;
}
}
return koeffs[k, ind];
}
float[] xpnt = new float[8] { 10, 90, 120, 180, 230, 260, 270, 340 };
float[] ypnt = new float[8] { 10, -20, 10, -10, 20, -40, 20, 0};
float[] h = new float[8] { 1, 1, 1, 1, 1, 1, 1, 1 };
void drawBasis(Graphics v)
{
for (float t = tmin; t <= tmax; t += 0.01f)
{
for (int i = 0; i < xpnt.Length; ++i)
{
float basis = getN(t, i, degree + 1);
v.DrawEllipse(new Pen(Color.Black), t * 100, 200 - basis * 100, 1, 1);
}
}
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics v = e.Graphics;
v.TranslateTransform(250, 250);
for (int i = 0; i < pCount; ++i)
{
v.DrawEllipse(new Pen(Color.Red), xpnt[i], ypnt[i], 3, 3);
}
for (float t = tmin; t <= tmax; t += 0.01f)
{
float x = 0, y = 0;
float sm = 0;
for (int i = 0; i < xpnt.Length; ++i)
{
float basis = getN(t, i, degree + 1);
x += xpnt[i] * basis * h[i];
y += ypnt[i] * basis * h[i];
sm += basis * h[i];
}
v.DrawEllipse(new Pen(Color.Black), x / sm, y / sm, 1, 1);
}
drawBasis(v);
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
Invalidate();
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
activeId = 1;
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
activeId = -1;
}
}
}