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}$
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8.  
  9. namespace casteljau {
  10. public partial class Form1 : Form {
  11.  
  12. Bitmap image;
  13. Graphics g;
  14. List<point> points = new List<point>();
  15. Color color = Color.Yellow;
  16.  
  17. public Form1() {
  18. InitializeComponent();
  19.  
  20. image = new Bitmap(pictureBox1.Width, pictureBox1.Height);
  21. g = Graphics.FromImage(image);
  22. pictureBox1.Image = image;
  23. }
  24.  
  25. #region DRAW METHOD
  26.  
  27. private void drawCasteljau(List<point> list) {
  28. Point tmp;
  29. for (double t = 0; t &lt;= 1; t += 0.001) {
  30. tmp = getCasteljauPoint(points.Count-1, 0, t);
  31. image.SetPixel(tmp.X, tmp.Y, color);
  32. }
  33. }
  34.  
  35. private Point getCasteljauPoint(int r, int i, double t) {
  36. if(r == 0) return points[i];
  37.  
  38. Point p1 = getCasteljauPoint(r - 1, i, t);
  39. Point p2 = getCasteljauPoint(r - 1, i + 1, t);
  40.  
  41. return new Point((int) ((1 - t) * p1.X + t * p2.X), (int) ((1 - t) * p1.Y + t * p2.Y));
  42. }
  43.  
  44. #endregion
  45.  
  46. #region EVENT HANDLERS
  47.  
  48. private void pictureBox1_MouseClick(object sender, MouseEventArgs e) {
  49. points.Add(e.Location);
  50. //g.Clear(pictureBox1.BackColor);
  51. //g.DrawLines(new Pen(color), points.ToArray());
  52. //pictureBox1.Refresh();
  53. }
  54.  
  55. private void pictureBox1_MouseDoubleClick(object sender, MouseEventArgs e) {
  56. points.Add(e.Location);
  57.  
  58. //kirajzolas
  59. drawCasteljau(points);
  60. pictureBox1.Refresh();
  61.  
  62. points.Clear();
  63. }
  64. #endregion
  65. }
  66. }