函数返回指针类型、简单链表的应用

//本文中有一函数值得学习,不多讲看代码

#include ...................................//这里省略了.h文件

#define FD_MAX_NUMS 10

typedef struct FD_TYP
{
 int x,y;
 int xv,yv;
 FD_TYP *next;
}FD, *FD_PTR;

typedef struct FD_NUM_TYP
{
 FD fd_line[10];
 FD fd_curr_every[10];
}FD_NUM,  *FD_NUM_PTR;

FD *fd_link = NULL;
FD_NUM fd_num;

typedef struct TRACE_FD_TYP //跟踪飞弹
{
 int x,y;
 TRACE_FD_TYP *next;
} TRACE_FD, *TRACE_FD_PTR;

typedef struct TRACE_FD_NUM_TYP       
{
 TRACE_FD trace_fd_num[10];     //hold trace fd line
 TRACE_FD curr_fd_pos[10];      //everone current position point
}TRACE_FD_NUM, *TRACE_FD_NUM_PTR;

TRACE_FD_NUM gtrace_fd_manage;

int Shutdown(void)
{
 //free(&(fd_num.fd_line[0]));
 free(fd_link);

    return 1;
} // end shutdown
//
FD *Create_Fd_Pos(void )
{
 FD *temp_fd ,*curr_fd , *head_fd;
 temp_fd = curr_fd = head_fd = NULL;
 temp_fd = (FD *)malloc(sizeof(FD));
 head_fd = curr_fd = temp_fd;
 for (int i=0; i<10; i++)
 {
  
  temp_fd->x =  i;
  temp_fd->y =  i;
  temp_fd->xv = 1;
  temp_fd->yv = 2;
  temp_fd->next = NULL;
  if (i<9)
   temp_fd = (FD *)malloc(sizeof(FD));
  else
   temp_fd = NULL;
  curr_fd->next = temp_fd;
  curr_fd = curr_fd->next;
 } // end for i
 
 return head_fd;
} // end create_fd_pos

//
FD *Create_Fd_Pos_Array1(void )
{
 FD *temp_fd ,*curr_fd , *head_fd;
 temp_fd = curr_fd = head_fd = NULL;
 temp_fd = (FD *)malloc(sizeof(FD));
 head_fd = curr_fd = temp_fd;
 for (int i=0; i<10; i++)
 {
  
  temp_fd->x =  i*2;
  temp_fd->y =  i*2;
  temp_fd->xv = 31;
  temp_fd->yv = 32;
  temp_fd->next = NULL;
  if (i<9)
   temp_fd = (FD *)malloc(sizeof(FD));
  else
   temp_fd = NULL;
  curr_fd->next = temp_fd;
  curr_fd = curr_fd->next;
 } // end for i
 
 return head_fd;
} // end Create_Fd_Pos_Array1

//
FD *Create_Fd_Pos_Array2( int n )
{
 FD *temp_fd ,*curr_fd , *head_fd;
 temp_fd = curr_fd = head_fd = NULL;
 temp_fd = (FD *)malloc(sizeof(FD));
 head_fd = curr_fd = temp_fd;
 for (int i=0; i<10; i++)
 {
  
  temp_fd->x =  i*n;
  temp_fd->y =  i*n;
  temp_fd->xv = 31+n;
  temp_fd->yv = 32+n;
  temp_fd->next = NULL;
  if (i<9)
   temp_fd = (FD *)malloc(sizeof(FD));
  else
   temp_fd = NULL;
  curr_fd->next = temp_fd;
  curr_fd = curr_fd->next;
 } // end for i
 
 return head_fd;
} // end Create_Fd_Pos_Array2

int Main_Program(void)
{
 FD *curr_link =NULL;
 fd_link = Create_Fd_Pos();
/*
 fd_num.fd_line[0] = *Create_Fd_Pos_Array1();
 fd_num.fd_curr_every[0] = fd_num.fd_line[0];
 
 fd_num.fd_line[1] = *Create_Fd_Pos_Array2(4);
 fd_num.fd_curr_every[1] = fd_num.fd_line[1];

 for (int i=2;i<10;i++)
 {
  fd_num.fd_line[i] = *Create_Fd_Pos_Array2(2*i);
  fd_num.fd_curr_every[i] = fd_num.fd_line[i];
 } // end for i
*/ 

 

 curr_link = fd_link;
 for (int a=0;a<10;a++)
 {
  if (curr_link==NULL)
   break;
  printf("%d  x=%d  y=%d  xv=%d  yv=%d/n",a,curr_link->x,curr_link->y,curr_link->xv,curr_link->yv);
        curr_link = curr_link->next;
 } // end for

 

 return 1;
} // end Main_Program

// TRACE FD
TRACE_FD *Trace_FD( int x1, int y1, int x0=rand()%640, int y0=0)
{
 int dx,         // difference in x's
    dy,             // difference in y's
    dx2,            // dx,dy * 2
    dy2,
    x_inc,          // amount in pixel space to move during drawing
    y_inc,          // amount in pixel space to move during drawing
    error,          // the discriminant i.e. error i.e. decision variable
    index;          // used for looping
 int tmpx,tmpy;

// init trace_fd struce
 TRACE_FD *temp_trace_fd,*head_fd,*curr_pos_fd;
 head_fd = curr_pos_fd=temp_trace_fd=NULL;
 temp_trace_fd = (TRACE_FD *)malloc(sizeof(TRACE_FD));
 head_fd = curr_pos_fd=temp_trace_fd;
 
    tmpx = x0;
 tmpy = y0;
// compute horizontal and vertical deltas
dx = x1-x0;
dy = y1-y0;

//compute x1,y1 end


// test which direction the line is going in i.e. slope angle
if (dx>=0)
   {
   x_inc = 1;

   } // end if line is moving right
else
   {
   x_inc = -1;
   dx    = -dx;  // need absolute value

   } // end else moving left

// test y component of slope

if (dy>=0)
   {
   y_inc = 1;
   } // end if line is moving down
else
   {
   y_inc = -1;
   dy    = -dy;  // need absolute value

   } // end else moving up

// compute (dx,dy) * 2
dx2 = dx << 1;
dy2 = dy << 1;

// now based on which delta is greater we can draw the line
if (dx > dy)
   {
   // initialize error term
   error = dy2 - dx;

   // draw the line
   for (index=0; index <= dx; index++)
       {

       // test if error has overflowed
       if (error >= 0)
          {
          error-=dx2;

          // move to next line
          //temp_trace_fd->y+=y_inc;
    tmpy+=y_inc;
    temp_trace_fd->x=tmpx;
          temp_trace_fd->y=tmpy;
    } // end if error overflowed

       // adjust the error term
       error+=dy2;
   
       // move to the next pixel
    tmpx+=x_inc;
    temp_trace_fd->y = tmpy;
    temp_trace_fd->x = tmpx;
    temp_trace_fd->next = NULL;
    if (index<dx)
   temp_trace_fd = (TRACE_FD *)malloc(sizeof(TRACE_FD));
    else
   temp_trace_fd = NULL;

    curr_pos_fd->next = temp_trace_fd;
    curr_pos_fd = curr_pos_fd->next;
       } // end for

   } // end if |slope| <= 1
else
   {
   // initialize error term
   error = dx2 - dy;

   // draw the line
   for (index=0; index <= dy; index++)
       {
      
       // test if error overflowed
       if (error >= 0)
          {
          error-=dy2;

          // move to next line
          //temp_trace_fd->x+=x_inc;
          tmpx+=x_inc;
    temp_trace_fd->x = tmpx;
    temp_trace_fd->y = tmpy;
          } // end if error overflowed

       // adjust the error term
       error+=dx2;

       // move to the next pixel
       //temp_trace_fd->y+=y_inc;
    tmpy+=y_inc;
    temp_trace_fd->x=tmpx;
    temp_trace_fd->y=tmpy;

    temp_trace_fd->next = NULL;
    if (index<dx)
   temp_trace_fd = (TRACE_FD *)malloc(sizeof(TRACE_FD));
    else
   temp_trace_fd = NULL;

    curr_pos_fd->next = temp_trace_fd;
    curr_pos_fd = curr_pos_fd->next;
       } // end for

   } // end else |slope| > 1

// return success
return head_fd;

} // END Trace_FD

int main(int argc, char* argv[])
{
 Init_Program();

 Main_Program();

 Shutdown();
   
 return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值