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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
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
{
class Mtr
{
double[,] data;
int n, k;
public Mtr(int n, int k)
{
this.n = n;
this.k = k;
data = new double[n, k];
for (int i = 0; i < n; ++i)
for (int j = 0; j < k; ++j)
data[i, j] = 0;
}
public Mtr(Mtr c)
{
this.n = c.n;
this.k = c.k;
data = new double[n, k];
for (int i = 0; i < n; ++i)
for (int j = 0; j < k; ++j)
data[i, j] = c.data[i, j];
}
public int getColumns()
{
return k;
}
public int getRows()
{
return n;
}
public void makeIdentity()
{
if (n != k)
{
Console.WriteLine("MAKE IDENTITY ERROR: N != K");
return;
}
for (int i = 0; i < n; ++i)
for (int j = 0; j < k; ++j)
{
data[i, j] = ((i == j) ? 1 : 0);
}
}
public Mtr(double[,] d)
{
n = d.GetLength(0);
k = d.GetLength(1);
data = new double[n, k];
for (int i = 0; i < n; ++i)
for (int j = 0; j < k; ++j)
data[i, j] = d[i, j];
}
public void setValue(double v, int i, int j)
{
data[i, j] = v;
}
public double getValue(int i, int j)
{
return data[i, j];
}
static public Mtr operator *(Mtr inp1, Mtr inp2)
{
if (inp1.k != inp2.n)
{
Console.WriteLine("MATRIX MULT ERROR");
return new Mtr(1, 1);
}
Mtr rs = new Mtr(inp1.n, inp2.k);
for (int i = 0; i < inp1.n; ++i)
{
for (int j = 0; j < inp2.k; ++j)
{
double tmp = 0;
for (int c = 0; c < inp1.k; ++c)
tmp += inp1.data[i, c] * inp2.data[c, j];
rs.setValue(tmp, i, j);
}
}
return rs;
}
static public Mtr operator *(Mtr inp1, double alpha)
{
Mtr rs = new Mtr(inp1.n, inp1.k);
for (int i = 0; i < inp1.n; ++i)
{
for (int j = 0; j < inp1.k; ++j)
{
rs.setValue(inp1.data[i, j] * alpha, i, j);
}
}
return rs;
}
static public Mtr operator -(Mtr inp1, double alpha)
{
Mtr rs = new Mtr(inp1.n, inp1.k);
for (int i = 0; i < inp1.n; ++i)
{
for (int j = 0; j < inp1.k; ++j)
{
rs.setValue(inp1.data[i, j] - alpha, i, j);
}
}
return rs;
}
static public Mtr operator -(Mtr inp1, Mtr inp2)
{
if (inp1.n != inp2.n || inp1.k != inp2.k)
{
Console.WriteLine("Operator- ERROR: Matr size not equal");
return new Mtr(1, 1);
}
Mtr rs = new Mtr(inp1.n, inp1.k);
for (int i = 0; i < inp1.n; ++i)
{
for (int j = 0; j < inp1.k; ++j)
{
rs.setValue(inp1.data[i, j] - inp2.data[i, j], i, j);
}
}
return rs;
}
static public Mtr operator +(Mtr inp1, Mtr inp2)
{
if (inp1.n != inp2.n || inp1.k != inp2.k)
{
Console.WriteLine("Operator+ ERROR: Matr size not equal");
return new Mtr(1, 1);
}
Mtr rs = new Mtr(inp1.n, inp1.k);
for (int i = 0; i < inp1.n; ++i)
{
for (int j = 0; j < inp1.k; ++j)
{
rs.setValue(inp1.data[i, j] + inp2.data[i, j], i, j);
}
}
return rs;
}
/// Tensor multiplication of vectors
static public Mtr operator ^(Mtr inp1, Mtr inp2)
{
if (inp1.k != inp2.n || inp1.n != inp2.k)
{
Console.WriteLine("Operator^ ERROR: Matr size not equal");
return new Mtr(1, 1);
}
Mtr rs = new Mtr(inp1.n, inp2.k);
for (int i = 0; i < inp1.n; ++i)
{
for (int j = 0; j < inp2.k; ++j)
{
rs.setValue(inp1.data[i, 0] * inp2.data[0, j], i, j);
}
}
return rs;
}
public Mtr getTranspozed()
{
Mtr ret = new Mtr(k, n);
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < k; ++j)
{
ret.setValue(data[i, j], j, i);
}
}
return ret;
}
public void transpoze()
{
double[,] ret = new double[k, n];
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < k; ++j)
{
ret[j, i] = data[i, j];
}
}
data = ret;
}
public Mtr getSubMatrix(int si, int sj, int ei, int ej)
{
if (si >= n || sj >= k || ei >= n || ej >= k ||
si < 0 || sj < 0 || ei < 0 || ej < 0)
{
Console.WriteLine("getSubMatrix RANGE ERROR");
return new Mtr(1, 1);
}
Mtr retMtr = new Mtr(ei - si + 1, ej - sj + 1);
int fi = 0;
int fj = 0;
for (int i = si; i <= ei; ++i)
{
fj = 0;
for (int j = sj; j <= ej; ++j)
{
retMtr.setValue(data[i, j], fi, fj);
++fj;
}
++fi;
}
return retMtr;
}
public bool isZeroMatrix()
{
for (int i = 0; i < n; ++i)
for (int j = 0; j < k; ++j)
if (data[i, j] != 0)
return false;
return true;
}
public void printMatrix()
{
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < k; ++j)
{
Console.Write("{0, 3:F4} ", data[i, j]);
}
Console.WriteLine();
}
Console.WriteLine(); Console.WriteLine(); Console.WriteLine();
}
public double getMaximumAboveDiag(ref int ii, ref int jj)
{
double val = -1000 * 1000 * 1000;
for (int i = 0; i < n; ++i)
{
for (int j = i + 1; j < k; ++j)
{
if (val < Math.Abs(data[i, j]))
{
val = Math.Abs(data[i, j]);
ii = i;
jj = j;
}
}
}
return val;
}
}
public partial class Form1 : Form
{
static void LuDecomposition(Mtr A, ref Mtr L, ref Mtr U)
{
if (A.getRows() != A.getColumns())
return;
int n = A.getRows();
L.makeIdentity();
for (int i = 0; i < n; ++i)
{
for (int j = i; j < n; ++j)
{
double temp = A.getValue(i, j);
for (int k = 0; k < i; ++k)
{
temp = temp - U.getValue(k, j) * L.getValue(i, k);
}
U.setValue(temp, i, j);
}
for (int j = i + 1; j < n; ++j)
{
double temp = A.getValue(j, i);
for (int k = 0; k < i; ++k)
{
temp = temp - U.getValue(k, i) * L.getValue(j, k);
}
temp = temp / U.getValue(i, i);
L.setValue(temp, j, i);
}
}
}
static void inversedLuSolver(ref Mtr A)
{
int n = A.getColumns();
Mtr L = new Mtr(n, n);
Mtr U = new Mtr(n, n);
LuDecomposition(A, ref L, ref U);
Mtr inversed = new Mtr(n, n);
for (int j = n - 1; j >= 0; --j)
{
double tmp = 1;
for (int k = j + 1; k < n; ++k)
tmp -= U.getValue(j, k) * inversed.getValue(k, j);
tmp /= U.getValue(j, j);
inversed.setValue(tmp, j, j);
for (int i = j - 1; i >= 0; --i)
{
tmp = 0;
for (int k = i + 1; k < n; ++k)
tmp += U.getValue(i, k) * inversed.getValue(k, j);
tmp = -tmp / U.getValue(i, i);
inversed.setValue(tmp, i, j);
}
for (int i = j - 1; i >= 0; --i)
{
tmp = 0;
for (int k = i + 1; k < n; ++k)
tmp += inversed.getValue(j, k) * L.getValue(k, i);
inversed.setValue(-tmp, j, i);
}
}
A = inversed;
}
PointF p11, p12, p21, p22, p31, p33;
public Form1()
{
p11 = new PointF(50, 100);
p12 = new PointF(1,1);
p21 = new PointF(150, 100);
p22 = new PointF(100, 50);
p31 = new PointF(1, 1);
p33 = new PointF(150, 100);
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.00001; u += 0.05f)
for (float t = 0; t <= 1.0001; t += 0.05f)
{
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], 3, 3);
}*/
float a = (p33.X - p22.X);
float b = (p33.Y - p22.Y);
float mt2 = (float)Math.Sqrt(a * a + b * b);
a = (p22.X - p11.X);
b = (p22.Y - p11.Y);
float mt = (float)Math.Sqrt(a * a + b * b); ;
v.DrawEllipse(new Pen(Color.Blue), p11.X, p11.Y, 10, 10);
v.DrawEllipse(new Pen(Color.Blue), p22.X, p22.Y, 10, 10);
v.DrawEllipse(new Pen(Color.Blue), p33.X, p33.Y, 10, 10);
double[,] mtrtt = new double[3, 3]{{1, 0.5, 0}, {mt2, (mt2 + mt) * 2, mt}, {0, 2, 4}};
double[,] vecppx = new double[3, 1] { {3.0 / (2.0 * mt2) * (p22.X - p11.X)}, {3.0f / (mt * mt2) * (mt * mt * (p33.X - p22.X) + mt2 * mt2 * (p22.X - p11.X))}, {6.0 / mt2 * (p33.X - p22.X)}};
double[,] vecppy = new double[3, 1] { { 3.0 / (2.0 * mt2) * (p22.Y - p11.Y) }, { 3.0f / (mt * mt2) * (mt * mt * (p33.Y - p22.Y) + mt2 * mt2 * (p22.Y - p11.Y)) }, { 6.0 / mt2 * (p33.Y - p22.Y) } };
Mtr tst = new Mtr(mtrtt);
Mtr check = new Mtr(mtrtt);
Mtr fv = new Mtr(vecppx);
Mtr sv = new Mtr(vecppy);
inversedLuSolver(ref tst);
fv = tst * fv;
sv = tst * sv;
p21.X = (float)fv.getValue(1, 0);
p21.Y = (float)sv.getValue(1, 0);
p12.X = (float)fv.getValue(0, 0);
p12.Y = (float)sv.getValue(0, 0);
p31.X = (float)fv.getValue(2, 0);
p31.Y = (float)sv.getValue(2, 0);
for (float t = 0; t <= mt; t += 0.7f)
{
float x = p11.X + p12.X * t + (3 * a / (mt * mt) - 2 * p12.X / mt - p21.X / mt) * t * t + (-2 * a / (mt * mt * mt) + p12.X / (mt * mt) + p21.X / (mt * mt)) * t * t * t;
float y = p11.Y + p12.Y * t + (3 * b / (mt * mt) - 2 * p12.Y / mt - p21.Y / mt) * t * t + (-2 * b / (mt * mt * mt) + p12.Y / (mt * mt) + p21.Y / (mt * mt)) * t * t * t;
v.DrawEllipse(new Pen(Color.Red), x, y, 3, 3);
}
a = (p33.X - p22.X);
b = (p33.Y - p22.Y);
for (float t = 0; t <= mt2; t += 0.7f)
{
float x = p22.X + p21.X * t + (3 * a / (mt2 * mt2) - 2 * p21.X / mt2 - p31.X / mt2) * t * t + (-2 * a / (mt2 * mt2 * mt2) + p21.X / (mt2 * mt2) + p31.X / (mt2 * mt2)) * t * t * t;
float y = p22.Y + p21.Y * t + (3 * b / (mt2 * mt2) - 2 * p21.Y / mt2 - p31.Y / mt2) * t * t + (-2 * b / (mt2 * mt2 * mt2) + p21.Y / (mt2 * mt2) + p31.Y / (mt2 * mt2)) * t * t * t;
v.DrawEllipse(new Pen(Color.Red), x, y, 3, 3);
}
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (activeId == 1)
{
// mtrx[3, 1] = e.X;
// mtry[3, 1] = e.Y;
p22.X = e.X;
p22.Y = e.Y;
}
Invalidate();
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
activeId = 1;
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
activeId = -1;
}
}
}