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,如需转载请自行联系原作者


相关文章
|
1月前
|
自然语言处理 算法 搜索推荐
C语言中谈论算法
C语言中谈论算法
10 0
C语言中谈论算法
|
1月前
|
算法 安全 C语言
使用C语言实现DES算法代码
使用C语言实现DES算法代码
|
1月前
|
算法 搜索推荐 C语言
C语言的算法
C语言的算法
17 0
|
1月前
|
自然语言处理 算法 搜索推荐
C语言用伪代码表示算法
C语言用伪代码表示算法
28 0
|
21天前
|
算法 C语言
【算法与数据结构】 C语言实现单链表队列详解2
【算法与数据结构】 C语言实现单链表队列详解
|
21天前
|
存储 算法 C语言
【算法与数据结构】 C语言实现单链表队列详解1
【算法与数据结构】 C语言实现单链表队列详解
|
1月前
|
存储 机器学习/深度学习 算法
C语言代码实现数据结构与算法
以上代码中,哈希表使用链表解决哈希冲突,每个链表节点包含一个键值对。hash函数用于计算键值对应的哈希值,insert函数用于向哈希表中插入一个键值对,若当前位置为空,则直接插入;否则,将新节点插入到链表末尾。search函数用于在哈希表中查找指定键值的值,若存在则返回其值,否则返回-1。
32 1
|
1月前
|
搜索推荐 算法 C语言
C语言排序算法
C语言排序算法
13 0
|
1月前
|
算法 数据处理 C语言
C语言核心之一的算法结构
C语言核心之一的算法结构
14 1
|
1月前
|
算法 搜索推荐 C语言
C语言的算法
C语言的算法
14 1