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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace BSpline
{
public partial class Form1 : Form
{
Bitmap bmp;
Graphics g;
public Form1()
{
InitializeComponent();
bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height);
g = Graphics.FromImage(bmp);
g.TranslateTransform(pictureBox1.Width / 2f, pictureBox1.Height / 2f);
//g.TranslateTransform(0, pictureBox1.Height);
g.ScaleTransform(1, -1);
g.SmoothingMode = SmoothingMode.HighQuality;
//PointF[] pts = new PointF[]
//{
// new PointF(0, 0),
// new PointF(30 * 3, 30 * 10),
// new PointF(30 * 6, 30 * 2.5f),
// new PointF(30 * 9, 30 * 5)
//};
PointF[] pts = new PointF[]
{
new PointF(-90*2, -80*2),
new PointF(-20*2, 80*2),
new PointF(20*2, -50*2),
new PointF(90*2, 80*2)
};
DrawSpline(g, pts, CreateOpenKnotsArray(4, 2), Pens.Blue);
DrawSpline(g, pts, CreateOpenKnotsArray(4, 3), Pens.Green);
DrawSpline(g, pts, CreateOpenKnotsArray(4, 4), Pens.Red);
DrawSpline(g, pts, CreatePeriodicKnotsArray(4, 3), Pens.Yellow);
DrawSpline(g, pts, CreatePeriodicKnotsArray(4, 4), Pens.Violet);
DrawPoints(pts, Brushes.Aqua);
pictureBox1.Image = bmp;
}
public static void DrawSpline(Graphics g, PointF[] pts, float[] knots, Pen pen, bool clear = false)
{
if (clear) g.Clear(Color.White);
int n = pts.Count() - 1;
int k = knots.Count() - pts.Count();
float[,] N = new float[knots.Count() - 1, k];
const int m = 100;
PointF pred = default(PointF);
float tL = knots[k - 1];
float tR = knots[knots.Count() - k];
for (int j = 0; j <= m; ++j)
{
float t = tL + (tR - tL) * j / m;
if (j == m) t -= 1e-4f;
for (int i = 0; i + 1 < knots.Count(); ++i)
N[i, 0] = (knots[i] <= t && t < knots[i + 1])? 1 : 0;
for (int _k = 1; _k < k; ++_k)
{
for (int i = 0; i < knots.Count() - 1 - _k; ++i)
{
float a = (t - knots[i]) * N[i, _k - 1];
float b = knots[i + _k] - knots[i];
float c = (knots[i + _k + 1] - t) * N[i + 1, _k - 1];
float d = knots[i + _k + 1] - knots[i + 1];
if (a != 0) a /= b;
if (c != 0) c /= d;
N[i, _k] = a + c;
}
}
PointF cur = new PointF(0, 0);
for (int i = 0; i < n + 1; ++i)
cur = cur.Add(pts[i].Multiply(N[i, k - 1]));
if (j != 0)
g.DrawLine(pen, pred, cur);
pred = cur;
}
}
public void DrawPoints(PointF[] pts, Brush br, float r = 5, bool clear = false)
{
if (clear) g.Clear(Color.White);
foreach (PointF p in pts)
g.FillEllipse(br, p.X - r, p.Y - r, 2 * r, 2 * r);
}
public static float[] CreateOpenKnotsArray(int pointsNum, int order)
{
float[] ans = new float[pointsNum + order];
for (int i = 0, c = 0; i < pointsNum + order; ++i)
{
if (i >= order && i <= pointsNum) ++c;
ans[i] = c;
}
return ans;
}
public static float[] CreatePeriodicKnotsArray(int pointsNum, int order)
{
float[] ans = new float[pointsNum + order];
for (int i = 0; i < pointsNum + order; ++i)
ans[i] = i;
return ans;
}
}
public static class Extender
{
public static PointF Add(this PointF obj, PointF val)
{
return new PointF(obj.X + val.X, obj.Y + val.Y);
}
public static PointF Multiply(this PointF obj, float val)
{
return new PointF(obj.X * val, obj.Y * val);
}
}
}