注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

简约男人

简约,不能简单

 
 
 

日志

 
 
关于我

一个过分渴望被理解的人其实就是一个软弱的人, 勇往直前的力量来自斩钉截铁的决心,不是来自别人的理解.

网易考拉推荐

基本图形绘制算法和动画效果  

2009-10-03 15:04:28|  分类: 我的软件作品 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

1、画直线、圆、椭圆函数


#include <stdio.h>
#include <Gmouse.h>
void drawline(int,int,int,int);

main()
{
   int x1,x0,y1,y0;

   initgraphic();

   sound(1400);
   delay(15000);
   nosound();
   printf("warnings! you must make sure the x is between 0 and ");
   x0=getmaxx();
   printf("%d ",x0);
  
   sound(1400);
   delay(15000);
   nosound();
   printf("\nwarnings! you must make sure the y is between 0 and ");
   y0=getmaxy();
   printf("%d ",y0);   
    
   printf("\n  input the two coordinates of the line,please! ");
   printf("\n  x0=");scanf("%d",&x0);
   printf("\n  y0=");scanf("%d",&y0);
   printf("\n  x1=");scanf("%d",&x1);
   printf("\n  y1=");scanf("%d",&y1);

         
   cleardevice();
   setcolor(4);

   drawline(x0,y0,x1,y1);
   getch();
   closegraph();

}

/*直线*/

void drawline(int x0,int y0,int x1,int y1)
{
    int x,y,dx,dy,e,i;
    /*y0=getmaxy()-y0;
    y1=getmaxy()-y1;*/
    if((x1>=x0&&y1>=y0)||(x0>=x1&&y0>=y1))
    {
       if(x0>=x1&&y0>=y1)
       {
        x=x0,x0=x1,x1=x;
        y=y0,y0=y1,y1=y;
       }
       dx=x1-x0;
       dy=y1-y0;
       if(dx>=dy)
       {
           e=-dx;x=x0;y=y0;
        for(i=0;i<=dx;i++)
        {
            putpixel(x,y,getcolor());
            x=x+1;
            e=e+2*dy;
            if(e>=0)
            {
                y=y+1;
                e=e-2*dx;
            }
        }
       }
       else
       {
        e=-dy;x=x0;y=y0;
        for(i=0;i<=dy;i++)
        {
            putpixel(x,y,getcolor());
            y=y+1;
            e=e+2*dx;
            if(e>=0)
            {
                x=x+1;
                e=e-2*dy;
            }
        }
       }
    }
    if((x1>=x0&&y1<=y0)||(x0>=x1&&y0<=y1))
    {
       if(x1>=x0&&y1<=y0)
       {
        x=x0,x0=x1,x1=x;
        y=y0,y0=y1,y1=y;
       }
       dx=x0-x1;
       dy=y1-y0;
       if(dx>=dy)
       {
           e=-dx;x=x0;y=y0;
        for(i=0;i<=dx;i++)
        {
            putpixel(x,y,getcolor());
            x=x-1;
            e=e+2*dy;
            if(e>=0)
            {
                y=y+1;
                e=e-2*dx;
            }
        }
       }
       else
       {
        e=-dy;x=x0;y=y0;
        for(i=0;i<=dy;i++)
        {
            putpixel(x,y,getcolor());
            y=y+1;
            e=e+2*dx;
            if(e>=0)
            {
                x=x-1;
                e=e-2*dy;
            }
           }
       }      
    }

}

/* 画圆函数  中点画圆法 */
void Circle(int  x1, int  y1, int  r, int color)
{
   int  x = 0, y =  r;
   int   dx = 3, dy = 2 -r -r, d = 1 - r;

    while (x <= y)
    {
        /* 绘制点(x,y)及其在八分圆中的另外7个对称点             */
        putpixel(x1+x, y1+y, color);
        putpixel(x1+y, y1+x, color);
        putpixel(x1-y, y1+x, color);
        putpixel(x1-x, y1+y, color);
        putpixel(x1-x, y1-y, color);
        putpixel(x1-y, y1-x, color);
        putpixel(x1+y, y1-x, color);
        putpixel(x1+x, y1-y, color);

        if (d < 0)
        {
            d += dx;
            dx += 2;
        }
       
        else
        {
            d += (dx + dy);
            dx += 2;
            dy += 2;
            y--;
        }

        x++;
    }
}

/*画圆,bresnham画圆法*/
template<typename imT>
void BsnhmCircle(int x0,int y0,int r,int color)
{
    int x = 0, y =  r;
    int d1, d2, d, dr;

    if (im == NULL)
        return;

    d = 2*(1 - r);
    while (y >= 0 )
    {
        putPixel(x0 + x, y0 + y, color);
        putPixel(x0 + y, y0 - x, color);
        putPixel(x0 - x, y0 - y, color);
        putPixel(x0 - y, y0 + x, color);
        if (d > 0)
        {
            d2 = 2*(d - x) - 1;
            if (d2 <= 0) dr = 2;
            else dr = 3;
        }
        else if (d < 0)
        {
            d1 = 2*(d + y) - 1;
            if (d1 <= 0) dr = 1;
            else dr = 2;
        }
        else dr = 2;

        switch (dr)
        {
        case 1 :
            x++;
            d += 2*x+1;
            break;
        case 2 :
            x++;
            y--;
            d += 2*(x - y + 1);
            break;
        case 3 :
            y--;
            d += (1 - 2*y);
            break;
        }
    }

}

/*椭圆*/

第一图形学作业 - tr0217 - 简约男人

void Ellipse(int xo, int yo, int a, int b)
{
    BOOL flag = FALSE;
    int  x;
    int  y;
    long aa;
    long bb;
    long d;
    int  t;

    if (a < b)
    {
        flag = TRUE;
        t = a;
        a = b;
        b = t;
    }
    aa = (long)a * a;
    bb = (long)b * b;

    x = 0;
    y = b;
    d = 4 * bb + aa * (-b * 4 + 1);
    while (4 * bb * (x + 1) <= aa * (4 * y - 2))
    {
        if (!flag) putpixel(xo + x, yo + y, getcolor());
        else putpixel(xo + y, yo + x, getcolor());
        if (!flag) putpixel(xo + x, yo - y, getcolor());
        else putpixel(xo + y, yo - x, getcolor());
        if (!flag) putpixel(xo - x, yo + y, getcolor());
        else putpixel(xo - y, yo + x, getcolor());
        if (!flag) putpixel(xo - x, yo - y, getcolor());
        else putpixel(xo - y, yo - x, getcolor());
        if (d < 0)
        {
            d += 4 * bb * (2 * x + 3);
            x++;
        }
        else
        {
            d += 4 * bb * (2 * x + 3) + 4 * aa * (-2 * y + 2);
            x++;
            y--;
        }
    }

    y = 0;
    x = a;
    d = 4 * aa + bb * (-a * 4 + 1);
    while (4 * bb * (x + 1) >= aa * (4 * y - 2))
    {
        if (!flag) putpixel(xo + x, yo + y, getcolor());
        else putpixel(xo + y, yo + x, getcolor());
        if (!flag) putpixel(xo + x, yo - y, getcolor());
        else putpixel(xo + y, yo - x, getcolor());
        if (!flag) putpixel(xo - x, yo + y, getcolor());
        else putpixel(xo - y, yo + x, getcolor());
        if (!flag) putpixel(xo - x, yo - y, getcolor());
        else putpixel(xo - y, yo - x, getcolor());
        if (d < 0)
        {
            d += 4 * aa * (2 * y + 3);
            y++;
        }
        else
        {
            d += 4 * aa * (2 * y + 3) + 4 * bb * (-2 * x + 2);
            y++;
            x--;
        }
    }
}

 二、旋转五角星

/*这是一个数据结构与算法的练习,在本程序中,画线 画圆 填充 都没有调用库函数;

旋转采用了链表的数据结构;由于五角星高度对称,我在程序中每转过8度,计算一次五角星中各坐标点,并把这些坐标数组存入旋转链表,直道五角星又回到原样,也就是说也就是说我只让五角星旋转了72度。这样同样有旋转效果。如果在程序中动态计算坐标,即没转过一个角度又重新计算坐标并画出,不仅效率不高而且变形大*/ 

第一图形学作业 - tr0217 - 简约男人

#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <dos.h>
#include <dir.h>
 

typedef struct chain
{ float pentacle[10][2];
  struct chain *next;
}rotate_chain;

void drawpentacle(float pentacle[10][2],int,int);


main()
{
  float roll[2][2]={{0.99027,0.13917},{-0.13917,0.99027}},ptl[10][2]={{0,1},{0.23,0.31},{0.95,0.31},{0.36,-0.11},{0.59,0.-0.81},{0,-0.38},{-0.59,-0.81},{-0.36,-0.11},{-0.95,0.31},{-0.23,0.31}};
  int i,j,k,l,X,Y,R;
  int min[4];
  int gdriver = DETECT, gmode, errorcode;
  rotate_chain  *head,*p,*q;
 
  /*初始化图形界面*/
   /* register a driver that was added into graphics.lib */
   errorcode = registerbgidriver(EGAVGA_driver);

   /* report any registration errors */
   if (errorcode < 0)
   {
      printf("Graphics error: %s\n", grapherrormsg(errorcode));
      printf("Press any key to halt:");
      getch();
      exit(1); /* terminate with an error code */
   }

   /* initialize graphics and local variables */
   initgraph(&gdriver, &gmode, "");

   /* read result of initialization */
   errorcode = graphresult();
   if (errorcode != grOk)  /* an error occurred */
   {
      printf("Graphics error: %s\n", grapherrormsg(errorcode));
      printf("Press any key to halt:");
      getch();
      exit(1); /* terminate with an error code */
   }
  
  /*获得图形参数*/
   X=getmaxx();
   Y=getmaxy();
  
   printf("---------------------");printf("warnings!");printf("------------------------\n");
   printf("1_Do make sure the ");printf("X ");printf("coordinate is between");printf(" 0 ");printf("and");printf(" %d\n",X);
   sound(1400);
   delay(200000);
   nosound();
   printf("X");printf("=");scanf("%d",&X);
  
   printf("---------------------");printf("warnings!");printf("------------------------\n");
   printf("2_Do make sure the ");printf("Y ");printf("coordinate is between");printf(" 0 ");printf("and");printf(" %d\n",Y);
   sound(1400);
   delay(200000);
   nosound();
   printf("\nY");printf("=");scanf("%d",&Y);
  
   printf("---------------------");printf("warnings!");printf("------------------------\n");
   min[0]=getmaxx()-X;min[1]=getmaxy()-Y;min[2]=X;min[3]=Y;R=getmaxx();
   for(i=0;i<4;i++)
     if(min[i]<R)  R=min[i];
   printf("3__Do make sure the ");printf(" R ");printf("is between");printf(" 0 ");printf("and");printf(" %d\n",R); 
   sound(1400);
   delay(200000);
   nosound();
   printf("R");printf("=");scanf("%d",&R);
  
  /*创建图形旋转链表*/
  p=malloc(sizeof(rotate_chain));head=p;p->next=head;q=p;
  for(i=0;i<10;i++)
   { printf("\n");for(j=0;j<2;j++)
   { p->pentacle[i][j]=(ptl[i][j]*R);
        printf("%16.5f",p->pentacle[i][j]);}}
   getch();

  for(l=0;l<8;l++)
    {p=malloc(sizeof(rotate_chain));q->next=p;p->next=head;
     for(i=0;i<10;i++)
      for(j=0;j<2;j++)
       {
        p->pentacle[i][j]=0;
        for(k=0;k<2;k++)
          p->pentacle[i][j]=p->pentacle[i][j]+(q->pentacle[i][k]*roll[k][j]);
       }
     q=p;
     getch();
     printf("\n");
    
       for(i=0;i<10;i++)
   { printf("\n");for(j=0;j<2;j++)
   { printf("%16.5f",p->pentacle[i][j]);}}

    }
  getch();
 
 
  /*画出图形和旋转*/
  setbkcolor(8);
  setcolor(12);
  cleardevice();
  p=head; i=1;
  do{k=kbhit();
  setcolor(2);
  line(X,0,X,479);
  line(0,Y,639,Y);
  circle(X,Y,R);
  setcolor(i);
  drawpentacle(p->pentacle,X,Y);
  setfillstyle(SOLID_FILL,i);
  floodfill(X,Y,i);
  delay(35000);
  cleardevice();
  p=p->next;
  i++;if(i>13) i=1;
  }while(k==0);
  getch();closegraph();
}

 

void drawpentacle(float pentacle[10][2],int X,int Y)
{
 line(pentacle[0][0]+X,pentacle[0][1]+Y,pentacle[1][0]+X,pentacle[1][1]+Y);
 line(pentacle[1][0]+X,pentacle[1][1]+Y,pentacle[2][0]+X,pentacle[2][1]+Y);
 line(pentacle[2][0]+X,pentacle[2][1]+Y,pentacle[3][0]+X,pentacle[3][1]+Y);
 line(pentacle[3][0]+X,pentacle[3][1]+Y,pentacle[4][0]+X,pentacle[4][1]+Y);
 line(pentacle[4][0]+X,pentacle[4][1]+Y,pentacle[5][0]+X,pentacle[5][1]+Y);
 line(pentacle[5][0]+X,pentacle[5][1]+Y,pentacle[6][0]+X,pentacle[6][1]+Y);
 line(pentacle[6][0]+X,pentacle[6][1]+Y,pentacle[7][0]+X,pentacle[7][1]+Y);
 line(pentacle[7][0]+X,pentacle[7][1]+Y,pentacle[8][0]+X,pentacle[8][1]+Y);
 line(pentacle[8][0]+X,pentacle[8][1]+Y,pentacle[9][0]+X,pentacle[9][1]+Y);
 line(pentacle[9][0]+X,pentacle[9][1]+Y,pentacle[0][0]+X,pentacle[0][1]+Y);
}

 

 

 


 

  评论这张
 
阅读(522)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017