Tuesday, 10 June 2014

C-Program for Bresenham’s Line algorithm

                 (for lines with |slope|<1).                                                 

This program performs scan conversion of a straight line between two points supplied as inputs. Although the main program calls line drawing function with an example point pairs, one may change it for arbitrary inputs. This  is a text  mode program for the Bresenham’s line drawing algorithm (scan conversion algorithm). Here output co-ordinate locations will be printed only.

                                                               
                                                                                                                                                          
#include<stdio.h>
                                                                                            
#define wsize 50
int buffer[wsize][wsize];
                                                                               
main()
{
 int i, j;
                                                                                            
 for(i = 0; i < wsize; i++)
   for(j = 0; j < wsize; j++)
      buffer[i][j] = 1;
                                                                                            
 BresLine(39, 25, 4, 5);
 printf("\n");
 for(i = 0; i < wsize; i++)
   {
    for(j = 0; j < wsize; j++)
      printf("%d ",buffer[i][j]);
    printf("\n");
   }

}
                                                                               
BresLine(int x1, int y1, int x2, int y2)
{
 int x, y, dx, dy, p, temp, k;

 dx = abs(x2-x1); dy = abs(y2-y1);
 x = x1; y = y1;
 p = 2 * dy - dx;
 printf("x = %d,  y = %d\n", x, y);
 buffer[x][y]=0;
 for(k=0; k < dx; k++)
   {
    if(p < 0)
      {
       if(x < x2) x++; else x--;
       p = p + 2 * dy;
      }
    else
      {
       if(y < y2) y++; else y--;
       if(x < x2) x++; else x--;
       p = p + 2 * (dy - dx);
      }
    printf("x = %d,  y = %d\n", x, y);
    buffer[x][y]=0;
   }
}

    
     

No comments:

Post a Comment