Wednesday, October 13, 2010

Draw the following figure(1)

Share Orkut

#include<stdio.h>
#include<graphics.h>
struct point
{
 int x,y;
}pol[3],polnew[3],circ,insid[12];
int n,r;
float trans[3][3],result[3][1],xandy[3][1];
void plot(int,int,int,int);
void boundaryfill4(int,int,int,int);
void midpoint(int xc,int yc,int r)
{
 int x=0;
 int y=r;
 int p=1-r;
 plot(xc,yc,x,y);
 while(x<y)
 {
  x++;
  if(p<0)
   p+=2*x+1;
  else
  {
   y--;   
   p+=2*(x-y)+1;
  }
  plot(xc,yc,x,y);
 }
}
void plot(int xc,int yc,int x,int y)
{
 putpixel(xc+x,yc+y,10);
 putpixel(xc-x,yc+y,10);
 putpixel(xc+x,yc-y,10);
 putpixel(xc-x,yc-y,10);
 putpixel(xc+y,yc+x,10);
 putpixel(xc-y,yc+x,10);
 putpixel(xc+y,yc-x,10);
 putpixel(xc-y,yc-x,10);
}
void drawpol()
{
 int i; 
 for(i=0;i<2;i++)
  line(pol[i].x,pol[i].y,pol[(i+1)].x,pol[(i+1)].y);
}
void drawnewpol()
{
 int i; 
 for(i=0;i<2;i++)
  line(polnew[i].x,polnew[i].y,polnew[(i+1)].x,polnew[(i+1)].y);
}
void mulmat()
{
 int i,j,k;
 for(i=0;i<3;i++)
 {
  for(j=0;j<1;j++)
  {
   result[i][j]=0.0;
   for(k=0;k<3;k++)
    result[i][j]+=trans[i][k]*xandy[k][j];
  }
 }
}
void transformpoint(int x,int y)
{
 xandy[0][0]=x;
 xandy[1][0]=y;
 xandy[2][0]=1;
 mulmat();
}
void transformpoly()
{
 int i; 
 for(i=0;i<3;i++)
 {
  xandy[0][0]=pol[i].x;
  xandy[1][0]=pol[i].y;
  xandy[2][0]=1;
  mulmat();
  polnew[i].x=result[0][0];
  polnew[i].y=result[1][0];
 }
}
void makeIdentity()
{
 int i,j;
 for(i=0;i<3;i++)
 {
  for(j=0;j<3;j++)
   trans[i][j]=0.0;
  trans[i][i]=1.0;
 }
}
void rotate(int rx,int ry,int an)
{
 float ang;
 ang=(float)(an*(3.14/180));
 trans[0][0]=cos(ang);
 trans[0][1]=-sin(ang);
 trans[0][2]=rx*(1-cos(ang))+ry*sin(ang);
 trans[1][0]=sin(ang);
 trans[1][1]=cos(ang);
 trans[1][2]=ry*(1-cos(ang))-rx*sin(ang);
 
}
void boundaryfill4(int x,int y,int new,int bdry)
{
 int current=getpixel(x,y);
 if((current!=bdry)&&(current!=new))
 {
  putpixel(x,y,new);
  boundaryfill4(x+1,y,new,bdry);
  boundaryfill4(x-1,y,new,bdry);
  boundaryfill4(x,y+1,new,bdry);
  boundaryfill4(x,y-1,new,bdry);
 }
}
int main()
{
 int i,j,angle=0,gd=DETECT,gm=VGAMAX,ch;
 int color[]={1,14,3,4,5,6,7,8,9,11,12,13};
 printf("Enter the orgin ");
 scanf("%d%d",&circ.x,&circ.y);
 printf("Enter the radius ");
 scanf("%d",&r);
 do{
  printf("\nMENU\n-------\n1.Enter inside points to fill\n
  2.Take the inside points automatically");
  printf("\nEnter ur choice  ");
  scanf("%d",&ch);
 }while(ch>2&&ch<1);
 if(ch==1)
 {
  printf("\nEnter the inside point of circle ");
  scanf("%d%d",&insid[0].x,&insid[0].y);
  for(i=1;i<12;i++)
  {
   printf("\nEnter the inside point %d ",i);
   scanf("%d%d",&insid[i].x,&insid[i].y);
  }
 }
 initgraph(&gd,&gm,"NULL");
 setcolor(10);
 midpoint(circ.x,circ.y,r);
 makeIdentity();
 j=360/11;
 pol[0].x=circ.x;
 pol[0].y=circ.y+r;
 pol[1].x=pol[0].x;
 pol[1].y=pol[0].y+50;
 rotate(circ.x,circ.y,j);
 transformpoint(pol[0].x,pol[0].y);
 pol[2].x=result[0][0];
 pol[2].y=result[1][0];
 rotate(circ.x,circ.y,360/22);
 transformpoint(pol[1].x,pol[1].y);
 pol[1].x=result[0][0];
 pol[1].y=result[1][0];
 if(ch==2)
 {
  insid[0].x=circ.x;
  insid[0].y=circ.y;
  insid[1].x=(pol[0].x+pol[1].x+pol[2].x)/3;
  insid[1].y=(pol[0].y+pol[1].y+pol[2].y)/3;
 }
 boundaryfill4(insid[0].x,insid[0].y,color[0],10);
 drawpol();
 boundaryfill4(insid[1].x,insid[1].y,color[1],10);
 for(i=0;i<10;i++)
 {
  angle+=j;
  rotate(circ.x,circ.y,angle);
  transformpoly();
  drawnewpol();
  if(ch==2)
  {
   insid[i+2].x=(polnew[0].x+polnew[1].x+polnew[2].x)/3;
   insid[i+2].y=(polnew[0].y+polnew[1].y+polnew[2].y)/3;
  }
  boundaryfill4(insid[i+2].x,insid[i+2].y,color[i+2],10);
 }
 while(!kbhit());
 closegraph();
 return 0;
}

1 comment:

  1. hei, wonderful work da. but it would be really helpful if you would put the explanation along with the program.

    ReplyDelete