sample code for line drawing Bresenham line drawing algorithm

There is no direct function available to draw line on the asp.net web form. Hence, we need to find the pixels of the line points and then drawing them on the bitmap objects. Here is the sample code that find the line points and sets them on the bitmap supplied in the argument.

 

public void lineSimple(int x0, int y0, int x1, int y1, System.Drawing.Color color, ref System.Drawing.Bitmap raster)

    {

 

        int dx = x1 - x0;

        int dy = y1 - y0;

 


        raster.SetPixel(x0, y0, color);

        if (dx != 0)

        {

            float m = (float)dy / (float)dx;

            float b = y0 - m * x0;

            dx = (x1 > x0) ? 1 : -1;

            while (x0 != x1)

            {

                x0 += dx;

                y0 = Convert.ToInt32(Math.Round(m * x0 + b));

                for (int i = 0; i <= 5; i++) // loops to change the thickness of the line

                {

                    for (int j = 0; j <= 6; j++)

                    {

                        raster.SetPixel(x0 + i, y0 + j, color);

                    }

                }


            }

        }

    }

 

I needed to develop this code because I needed to prepare my own line chart component. It is working without any problem.

 

Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList






Quality & Affordable Web Development | About the Author | Hosted By Windows Hosting | Discuss With Experts At Webmaster Forums