de Casteljau algoritmus (C#)
$\bf P_{i,j}=(1-t)P_{i-1,j}+tP_{i-1,j+1} \cases{i=1,2,...,n \cr j=0,1,...,n-i}$
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace casteljau {
public partial class Form1 : Form {
Bitmap image;
Graphics g;
List<point> points =
new List<point>
();
Color color = Color.Yellow;
public Form1() {
InitializeComponent();
image =
new Bitmap
(pictureBox1.
Width, pictureBox1.
Height);
g = Graphics.FromImage(image);
pictureBox1.Image = image;
}
#region DRAW METHOD
private void drawCasteljau(List<point> list) {
Point tmp;
for (double t = 0; t <= 1; t += 0.001) {
tmp = getCasteljauPoint(points.Count-1, 0, t);
image.SetPixel(tmp.X, tmp.Y, color);
}
}
private Point getCasteljauPoint(int r, int i, double t) {
if(r == 0) return points[i];
Point p1 = getCasteljauPoint(r - 1, i, t);
Point p2 = getCasteljauPoint(r - 1, i + 1, t);
return new Point
((int) ((1 - t
) * p1.
X + t * p2.
X),
(int) ((1 - t
) * p1.
Y + t * p2.
Y));
}
#endregion
#region EVENT HANDLERS
private void pictureBox1_MouseClick(object sender, MouseEventArgs e) {
points.Add(e.Location);
//g.Clear(pictureBox1.BackColor);
//g.DrawLines(new Pen(color), points.ToArray());
//pictureBox1.Refresh();
}
private void pictureBox1_MouseDoubleClick(object sender, MouseEventArgs e) {
points.Add(e.Location);
//kirajzolas
drawCasteljau(points);
pictureBox1.Refresh();
points.Clear();
}
#endregion
}
}