C语言实现的DDA和Bresenham直线算法

#include <graphics.h>
#include <stdio.h>
#include <math.h>
#include <process.h>
/* she ru */
int Round(float a)
{
 return (int)(a + 0.5);
}
/* DDA */
void LineWithDDA(int xStart,int yStart,int xEnd,int yEnd)
{
 int dx =xEnd - xStart,dy =yEnd - yStart,steps,k;
 float xIn, yIn, x = xStart, y = yStart;
 if(fabs(dx) > fabs(dy))
 {
  steps = fabs(dx);
 }
 else
 {
  steps = fabs(dy);
 }
 xIn = (float)dx / (float)steps;
 yIn = (float)dy / (float)steps;
 putpixel(Round(x),Round(y),2);
 for(k=0;k<steps;k++)
 {
  x+=xIn;
  y+=yIn;
  putpixel(Round(x),Round(y),2);
 }
 getch();
}
/* |k|<1 */
void LineWithBresenham_One(int xStart,int yStart,int xEnd,int yEnd)
{
 int dx = fabs(xEnd -xStart), dy = fabs(yEnd - yStart);
 int currentP = 2 * dy - dx;
 int twoDy = 2 * dy, twoDySubTwoDx = 2 * (dy - dx);
 int x, y;
 if(xStart == xEnd)
 {
  line(xStart,yStart,xEnd,yEnd);
  return ;
 }
 if(yStart == yEnd)
 {
  line(xStart,yStart,xEnd,yEnd);
  return;
 }
 if(xStart > xEnd)
 {
  x = xEnd;
  y = yEnd;
  xEnd = xStart;
 }
 else
 {
  x = xStart;
  y = yStart;
 }
 putpixel(x,y,2);
 while(x < xEnd)
 {
  x++;
  if(currentP < 0)
  {
   currentP += twoDy;
  }
  else
  {
   y++;
   currentP += twoDySubTwoDx;
  }
  putpixel(x,y,2);
 }
 getch();
}
/* |k|>1 */
void LineWithBresenham_Two(int xStart,int yStart,int xEnd,int yEnd)
{
 int dx = fabs(xEnd -xStart), dy = fabs(yEnd - yStart);
 int currentP = 2 * dy - dx;
 int twoDx = 2 * dx, twoDxSubTwoDy = 2 * (dx - dy);
 int x, y;
 if(xStart == xEnd)
 {
  line(xStart,yStart,xEnd,yEnd);
  return ;
 }
 if(yStart == yEnd)
 {
  line(xStart,yStart,xEnd,yEnd);
  return;
 }
 if(yStart > yEnd)
 {
  x = xEnd;
  y = yEnd;
         yEnd = yStart;
 }
 else
 {
  x = xStart;
  y = yStart;
 }
 putpixel(x,y,2);
 while(y < yEnd)
 {
  y++;
  if(currentP < 0)
  {
   currentP += twoDx;
  }
  else
  {
   x++;
   currentP += twoDxSubTwoDy;
  }
  putpixel(x,y,2);
 }
 getch();
}
void InitScreen()
{
   clrscr();
   printf("\n\n\n\n\n             ***********************************************************\n");
   printf("             *                                                         *\n");
   printf("             *  This is a graphics system,you can use it to graphics.  *\n");
   printf("             *                                                         *\n");
   printf("             ***********************************************************\n");
   printf("\n\n\n\n                1  Line a line with Bresenham while |k|<1. \n\n");
   printf("                2  Line a line with Bresenham while |k|>1. \n\n");
   printf("                3  Line a Line with DDA including x>y and x<y in xOy\n\n");
   printf("                0  Exit system.\n\n\n");
   printf("                Please select a number:");
}
int main()
{
   /* request autodetection */
   int gdriver = DETECT, gmode, errorcode;
   int xStart, yStart, xEnd, yEnd;
   int number;
   InitScreen();
   flag:scanf("%d",&number);
   if(number == 1)
   {
 printf("                Please input four numbers:");
 scanf("%d%d%d%d",&xStart,&yStart,&xEnd,&yEnd);
  /* initialize graphics and local  variables */
    initgraph(&gdriver, &gmode, "");
    /* read result of initialization */
    errorcode = graphresult();
    if (errorcode != grOk)
    {
        printf("Graphics error: %s\n", grapherrormsg(errorcode));
         printf("Press any key to halt:");
         getch();
         exit(1); /* terminate with an error code */
   }
 LineWithBresenham_One(fabs(xStart),fabs(yStart),fabs(xEnd),fabs(yEnd)) ;
 closegraph();
 InitScreen();
 goto flag;
   }
   else if(number == 2)
   {
 printf("                Please input four numbers:");
 scanf("%d%d%d%d",&xStart,&yStart,&xEnd,&yEnd);
 initgraph(&gdriver, &gmode, "");
    /* read result of initialization */
    errorcode = graphresult();
    if (errorcode != grOk)
    {
        printf("Graphics error: %s\n", grapherrormsg(errorcode));
         printf("Press any key to halt:");
         getch();
         exit(1); /* terminate with an error code */
   }
 LineWithBresenham_Two(fabs(xStart),fabs(yStart),fabs(xEnd),fabs(yEnd)) ;
 closegraph();
 InitScreen();
 goto flag;
   }
   else if(number == 3)
   {
 printf("                Please input four numbers:");
 scanf("%d%d%d%d",&xStart,&yStart,&xEnd,&yEnd);
 initgraph(&gdriver, &gmode, "");
    /* read result of initialization */
    errorcode = graphresult();
    if (errorcode != grOk)
    {
        printf("Graphics error: %s\n", grapherrormsg(errorcode));
         printf("Press any key to halt:");
         getch();
         exit(1); /* terminate with an error code */
   }
 LineWithDDA(fabs(xStart),fabs(yStart),fabs(xEnd),fabs(yEnd)) ;
 closegraph();
 InitScreen();
 goto flag;
   }
   else if(number == 0)
   {
/* abort(); */
 exit(0);
   }
   /* clean up */
/*   getch();
   closegraph();
   return 0;     */

}

















本文转自terryli51CTO博客,原文链接: http://blog.51cto.com/terryli/520654,如需转载请自行联系原作者


上一篇:JavaScript 编程精解 中文第三版 四、数据结构:对象和数组


下一篇:用C语言实现窗口抖动