# VBForums CodeBank > CodeBank - C# >  Density-Based Spatial Clustering of Applications with Noise (DBSCAN)

## ForumAccount

> Density-based spatial clustering of applications with noise (DBSCAN) is a data clustering algorithm. It is a density-based clustering algorithm because it finds a number of clusters starting from the estimated density distribution of corresponding nodes. DBSCAN is one of the most common clustering algorithms.
> 
> *http://en.wikipedia.org/wiki/DBSCAN*


How to use:

1. Add _Dbscan.cs_ to your project.
2. Add _Dbscan.txt_ to your project, set the _Copy to Output Directory_ property to _Copy always_ on it.
3. Add a chart (from the _System.Windows.Forms.DataVisualization_ namespace) to the form.
4. Add the code below to your form. (VB Version Here)



```
public partial class Form1 : Form
{

    #region fields

    private static Color[] ChartColors = new Color[] { Color.FromArgb(0x41, 140, 240),   Color.FromArgb(0xfc, 180, 0x41),  
                                                       Color.FromArgb(0xe0, 0x40, 10),   Color.FromArgb(5, 100, 0x92), 
                                                       Color.FromArgb(0xbf, 0xbf, 0xbf), Color.FromArgb(0x1a, 0x3b, 0x69), 
                                                       Color.FromArgb(0xff, 0xe3, 130),  Color.FromArgb(0x12, 0x9c, 0xdd), 
                                                       Color.FromArgb(0xca, 0x6b, 0x4b), Color.FromArgb(0, 0x5c, 0xdb),    
                                                       Color.FromArgb(0xf3, 210, 0x88),  Color.FromArgb(80, 0x63, 0x81), 
                                                       Color.FromArgb(0xf1, 0xb9, 0xa8), Color.FromArgb(0xe0, 0x83, 10),   
                                                       Color.FromArgb(120, 0x93, 190) };

    #endregion

    #region constructors

    public Form1()
    {
        this.InitializeComponent();
        this.InitializeChart();
    }

    #endregion

    #region methods

    private void InitializeChart()
    {
        Series series = this.chart1.Series[0];

        series.ChartType = SeriesChartType.Point;
        series.MarkerStyle = MarkerStyle.Circle;
        series.MarkerSize = 10;

        this.chart1.Legends[0].Enabled = false;

        this.chart1.ChartAreas[0].AxisX.LabelStyle.Format = "N3";
        this.chart1.ChartAreas[0].AxisX.MajorGrid.Enabled = false;

        this.chart1.ChartAreas[0].AxisY.LabelStyle.Format = "N3";
        this.chart1.ChartAreas[0].AxisY.MajorGrid.Enabled = false;
    }

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);

        // generate chart based on Dbscan.txt
        this.GenerateClusters();
    }

    private void GenerateClusters()
    {

        List<DbscanPoint> points = new List<DbscanPoint>();

        // change path as appropriate
        var lines = System.IO.File.ReadAllLines("Dbscan.txt");

        foreach (var line in lines)
        {
            var split = line.Split(' ');

            points.Add(new DbscanPoint(Convert.ToDouble(split[1]), 
                                       Convert.ToDouble(split[2])));
        }

        // customize the epsilion radius and minimum cluster points here
        // note: if you are using http://people.cs.nctu.edu.tw/~rsliang/dbscan/testdatagen.html
        //       to generate data, make sure these parameters match the parameters on the site
        //       otherwise you will get different results
        DbscanAlgorithm.Dbscan(points.ToArray(), 4d, 6);

        var series = this.chart1.Series[0];

        foreach (var item in points)
        {
            var p = series.Points[series.Points.AddXY(item.X, item.Y)];

            if (item.IsNoise)
                p.Color = SystemColors.Control;
            else
                p.Color = Form1.ChartColors[(item.ClusterId.Value - 1) % Form1.ChartColors.Length];
        }
    }

    #endregion

}
```

You should see this if everything was successful:



Here's how you can generate test data (this is what I verified the algorithm against): http://people.cs.nctu.edu.tw/~rslian...stdatagen.html.

----------

