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
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 BezierSurface
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public float[,] mult(float[,] a, float[,] b)
{
float[,] c = new float[a.GetLength(0), b.GetLength(1)];
for (int i = 0; i < a.GetLength(0); ++i)
{
for (int j = 0; j < b.GetLength(1); ++j)
{
c[i, j] = 0;
for (int k = 0; k < a.GetLength(1); ++k)
{
c[i, j] = c[i, j] + a[i, k] * b[k, j];
}
}
}
return c;
}
float[,] BX = new float[4, 4] {{50, 100, 150, 200},
{50, 100, 150, 200},
{50, 100, 150, 200},
{50, 100, 150, 200}};
float[,] BY = new float[4, 4] {{50, 50, 50, 50},
{100, 100, 100, 100},
{150, 150, 150, 150},
{200, 200, 200, 200}};
private void Form1_Load(object sender, EventArgs e)
{
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
Pen pen = new Pen(Color.Black);
PointF[,] output = new PointF[16, 16];
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 4; ++j) {
g.DrawEllipse(pen, BX[i, j] - 5, BY[i, j] - 5, 10, 10);
}
}
for (int u = 0; u <= 15; ++u)
{
float cur_u = (float)u / 15f;
float[,] U = new float[1, 4] { { (1 - cur_u) * (1 - cur_u) * (1 - cur_u),
3 * (1 - cur_u) * (1 - cur_u) * cur_u,
3 * (1 - cur_u) * cur_u * cur_u,
cur_u * cur_u * cur_u } };
for (int w = 0; w <= 15; ++w)
{
float cur_w = (float)w / 15f;
float[,] W = new float[4, 1] { { (1 - cur_w) * (1 - cur_w) * (1 - cur_w) },
{3 * (1 - cur_w) * (1 - cur_w) * cur_w},
{3 * (1 - cur_w) * cur_w * cur_w},
{cur_w * cur_w * cur_w }};
float [,] X = mult(mult(U, BX), W);
float [,] Y = mult(mult(U, BY), W);
output[u, w] = new PointF(X[0, 0], Y[0, 0]);
}
}
for (int u = 1; u <= 15; ++u)
{
for (int w = 0; w <= 15; ++w)
{
g.DrawLine(new Pen(Color.Red), output[u, w], output[u - 1, w]);
}
}
for (int u = 0; u <= 15; ++u)
{
for (int w = 1; w <= 15; ++w)
{
g.DrawLine(new Pen(Color.Red), output[u, w], output[u, w - 1]);
}
}
}
private bool isDragging = false;
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
if (Math.Abs(BX[0,0] - e.X) < 10 && Math.Abs(BY[0,0] - e.Y) < 10)
{
this.isDragging = true;
}
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
this.isDragging = false;
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (this.isDragging)
{
BX[0, 0] = e.X;
BY[0, 0] = e.Y;
this.Invalidate();
}
}
}
}