Click here to Skip to main content
15,886,799 members
Articles / Programming Languages / C#

Bezier Curves Made Simple

Rate me:
Please Sign up or sign in to vote.
4.92/5 (43 votes)
14 Apr 2008CPOL4 min read 388.1K   15.1K   103  
A simple implementation of the famous Bezier curves in C#. Easy to understand.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Curves
{
    public partial class Form1 : Form
    {
        private List<double> ptList = new List<double>();
        private BezierCurve bc = new BezierCurve();

        public Form1()
        {
            InitializeComponent();
        }

        private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
        {
            ptList.Add(e.X);
            ptList.Add(e.Y);

            g.DrawRectangle(px, new Rectangle(e.X, e.Y, 1, 1));
            
        }

        Pen px = new Pen(Brushes.Red);
        Pen newpx = new Pen(Brushes.Magenta);
        Graphics g;
        private void button1_Click(object sender, EventArgs e)
        {
            // how many points do you need on the curve?
            const int POINTS_ON_CURVE = 1000;

            double[] ptind = new double[ptList.Count];
            double[] p = new double[POINTS_ON_CURVE];
            ptList.CopyTo(ptind, 0);

            bc.Bezier2D(ptind, (POINTS_ON_CURVE) / 2, p);

            //pictureBox1.Refresh();
            // draw points
            for (int i = 1; i != POINTS_ON_CURVE-1; i += 2)
            {
                g.DrawRectangle(newpx, new Rectangle((int)p[i + 1], (int)p[i], 1, 1));
                g.Flush();
                Application.DoEvents();
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            g = Graphics.FromHwnd(pictureBox1.Handle);
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
CEO Gravi Information Technologies and Consultancy Ltd
Turkey Turkey
Currently, also an MSc. student in Technical University of Munich, I develop practical application in computer vision for more than 5 years. I design real-time solutions to industrial and practical vision problems, both 3D and 2D. Very interested in developing algorithms in C relating math and vision.

Please visit Gravi's web page (http://www.gravi.com.tr) and my page (http://www.tbirdal.me) to learn more about what we develop.

I admire performance in algorithms.

"Great minds never die, they just tend to infinity..."

Comments and Discussions