Showing posts with label Computer Graphics. Show all posts
Showing posts with label Computer Graphics. Show all posts

Friday, 27 September 2013

Animation

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

void main()
{

   int gdriver = DETECT, gmode;
   int midx, midy;
   int radius = 15;
   int i;

   initgraph(&gdriver, &gmode, "c:\\turboc3\\bgi");

   midy = getmaxy() / 2;
   setcolor(RED);

   for(i=3;i<49;i++)
   {

    delay(100);
    cleardevice();

   circle(20,midy-40,radius);
   line(2,midy-20,20,midy-10);
   line(40,midy-20,20,midy-10);
   line(20,midy-25,20,midy);
   line(20,midy,10,midy+30);

   line(20,midy,(float)(30+0.3*i),(float)((midy+30)-(0.3*i)));

   circle(i*10, midy+15, radius);

   circle(510,midy-40,radius);
   line(490,midy-20,510,midy-10);
   line(530,midy-20,510,midy-10);
   line(510,midy-25,510,midy);
   line(510,midy,500,midy+30);
   line(510,midy,520,midy+30);
   }


     for(i=49;i>3;i--)
   {

    delay(100);
     cleardevice();

   circle(20,midy-40,radius);
   line(2,midy-20,20,midy-10);
   line(40,midy-20,20,midy-10);
   line(20,midy-25,20,midy);
   line(20,midy,10,midy+30);
   line(20,midy,30,midy+30);
   circle(i*10, midy+15, radius);

   circle(510,midy-40,radius);
   line(490,midy-20,510,midy-10);
   line(530,midy-20,510,midy-10);
   line(510,midy-25,510,midy);
   line(510,midy,520,midy+30);


    line(510,midy,(float)(500-0.3*i),(float)((midy+30)-(0.3*i)));
   }






   circle(510,midy-40,radius);
   line(510,midy-25,510,midy);

   line(510,midy,520,midy+30);

   /* clean up */
   getch();
   closegraph();
}

2D transformations-Translation, Scaling, Rotation



#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>

void translate();
void scale();
void rotate();

void main()
{
int ch;
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\turboc3\\bgi");

setcolor(WHITE);
outtextxy (100,88,"Object.");
rectangle(100,150,150,100);
while(ch!=4)
{
printf("---MENU---");
printf("\n 1)Translate\n 2)Scale\n 3)Rotate\n4)exit");
printf("\nEnter your choice: ");
scanf("%d",&ch);
//cleardevice();

switch(ch)
{
case 1: translate();
break;
case 2: scale();
break;
case 3: rotate();
break;

}
}
getch();
closegraph();
}

void translate()
{
int tx,ty;
setcolor(2);
outtextxy(240,10,"TRANSLATION");
outtextxy(238,20,"------------");
printf("\nEnter tx: ");
scanf("%d",&tx);
printf("\nEnter ty: ");
scanf("%d",&ty);
//cleardevice();
rectangle(100,150,150,100);
printf("\nAfter Translation\n");
rectangle(100+tx,150+ty,150+tx,100+ty);
}

void scale()
{
int sx,sy;
setcolor(2);
outtextxy(240,10,"SCALING");
outtextxy(238,20,"--------");
printf("\nEnter sx: ");
scanf("%d",&sx);
printf("\nEnter sy: ");
scanf("%d",&sy);
//cleardevice();
rectangle(100,150,150,100);
printf("\nAfter Scaling\n");
rectangle(100*sx,150*sy,150*sx,100*sy);
}

void rotate()
{
float theta;
int x1,x2,x3,x4;
int y1,y2,y3,y4;
int ax1,ax2,ax3,ax4,ay1,ay2,ay3,ay4;
int refx,refy;
printf("\nEnter the angle for rotation: ");
scanf("%f",&theta);
theta=theta*(3.14/180);
//cleardevice();
setcolor(2);
outtextxy(240,10,"ROTATE");
outtextxy(238,20,"-------");
refx=100;
refy=100;

x1=100;
y1=100;
x2=150;
y2=100;
x3=150;
y3=150;
x4=100;
y4=150;

ax1=refy+(x1-refx)*cos(theta)-(y1-refy)*sin(theta);
ay1=refy+(x1-refx)*sin(theta)+(y1-refy)*cos(theta);

ax2=refy+(x2-refx)*cos(theta)-(y2-refy)*sin(theta);
ay2=refy+(x2-refx)*sin(theta)+(y2-refy)*cos(theta);

ax3=refy+(x3-refx)*cos(theta)-(y3-refy)*sin(theta);
ay3=refy+(x3-refx)*sin(theta)+(y3-refy)*cos(theta);

ax4=refy+(x4-refx)*cos(theta)-(y4-refy)*sin(theta);
ay4=refy+(x4-refx)*sin(theta)+(y4-refy)*cos(theta);
rectangle(100,150,150,100);
line(ax1,ay1,ax2,ay2);
line(ax2,ay2,ax3,ay3);
line(ax3,ay3,ax4,ay4);
line(ax4,ay4,ax1,ay1);


}

Mid Point Circle Algo

#include<stdio.h>
#include<graphics.h>
#include<conio.h>
#include<iostream.h>
void main()
{
 clrscr();
 int gd=DETECT,gm;
 initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
 int r,pk,xc,yc,x,y,pk1;
 cout<<"enter the coordinates of center:";
 cin>>xc>>yc;
 cout<<"enter the radius:";
 cin>>r;
 pk=1-r;
 x=0;
 y=r;
 while((x<=y))
 if(pk<0)
 {
  x++;
  putpixel(x+xc,y+yc,RED);
  putpixel(-y+xc,x+yc,RED);
  putpixel(x+xc,-y+yc,RED);
  putpixel(-y+xc,-x+yc,RED);
  putpixel(-x+xc,-y+yc,RED);
  putpixel(-x+xc,y+yc,RED);
  putpixel(y+xc,-x+yc,RED);
  putpixel(y+xc,x+yc,RED);
  pk=pk+(2*x)+1;
  }
  else
  {
   x++;
   y--;
   putpixel(x+xc,y+yc,RED);
   putpixel(-y+xc,x+yc,RED);
   putpixel(x+xc,-y+yc,RED);
   putpixel(-y+xc,-x+yc,RED);
   putpixel(-x+xc,-y+yc,RED);
   putpixel(-x+xc,y+yc,RED);
   putpixel(y+xc,-x+yc,RED);
   putpixel(y+xc,x+yc,RED);
   pk=pk+(2*x)-(2*y)+1;
   }
   getch();
   closegraph();
   }

Bresenham's Line Drawing Algo

#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
{
clrscr();
int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"C:\\TURBOC3\\BGI");

int x1,x2,dx,dy,y1,y2,i=1,p;
cout<<"Enter starting points :";
cin>>x1>>y1;
cout<<"Enter starting points :";
cin>>x2>>y2;

dx=abs(x2-x1);
dy=abs(y2-y1);
p=2*dy-dx;
while(i<=dx)
{
  x1++;
  if (p<0)
   p=p+2*dy;
  else
   {
   p=p+2*dy-2*dx;
   y1++;
   }
   putpixel(x1,y1,WHITE);
   i++;
 }
  getch();

}

DDA Line Drawing Algo


#include<iostream.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
#include<stdio.h>
void main()
{
 int gdriver=DETECT,gmode;
 initgraph(&gdriver,&gmode,"C:\\TURBOC3\\BGI");

 int x1=50,y1=100,x2=100,y2=300,dx,dy,length;
 int i=1;
 float x,y,xi,yi;
/* cout<<"Enter Starting Point:";
 cin>>x1>>y1;
 cout<<"Enter Ending Point :";
 cin>>x2>>y2;*/
 dx=x2-x1;
 dy=y2-y1;
 if(abs(dx)>abs(dy))
  length=abs(dx);
 else
  length=abs(dy);
xi=(float)dx/length;
yi=(float)dy/length;

x=x1;
y=y1;
 putpixel(x,y,WHITE);
while(i<=length)
 {
 x=x+xi;
 y=y+yi;
 i++;
  putpixel(int(x),int(y),WHITE);
 }
 getch();
 closegraph();
}

Boundary Fill

#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include<iostream.h>
#include<dos.h>
void bfill(int x,int y,int a);
int main()
{
   /* request auto detection */
   int gdriver = DETECT, gmode;

   /* initialize graphics and local variables */
   initgraph(&gdriver, &gmode, "C:\\TURBOC3\\BGI");

   /*Initialize the variables and accept data*/
   int r,xc,yc,x,y,p;
   cout<<"Enter the radius:";
   cin>>r;
   cout<<"Enter the center co-ordinates:";
   cin>>xc>>yc;
   x=0;

   y=r;
   p=1-r;

   /*Calculate and plot the points*/
   do
   {
    putpixel(xc+x,yc+y,WHITE);
    putpixel(xc+y,yc+x,WHITE);
    putpixel(xc-y,yc+x,WHITE);
    putpixel(xc+x,yc-y,WHITE);
    putpixel(xc-x,yc-y,WHITE);
    putpixel(xc-y,yc-x,WHITE);
    putpixel(xc+y,yc-x,WHITE);
    putpixel(xc-x,yc+y,WHITE);
    if(p<0)
    {
     x=x+1;
     y=y;
     p=p+(2*x)+1;
    }
   else
   {
    x=x+1;
    y=y-1;
    p=p+
    (2*x)+1-(2*y);
   }
  }
  while(x<=y);
  //start boundary filling
  bfill(xc,yc,4);
   /* clean up */
   getch();
   closegraph();
   return 0;
   }
void bfill(int x,int y,int a)
{int current;
 current=getpixel(x,y);
 if((current!=a)&&(current!=WHITE))
 {
 putpixel(x,y,a);
 delay(5);
 bfill(x+1,y,a);
 bfill(x-1,y,a);
 bfill(x,y+1,a);
 bfill(x,y-1,a);
 }
}

Flood Fill

#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include<iostream.h>
#include<dos.h>
void bfill(int x,int y,int a);
void ffill(int x,int y,int a);
void main()
{
   /* request auto detection */
   int gdriver = DETECT, gmode;

   /* initialize graphics and local variables */
   initgraph(&gdriver, &gmode, "C:\\TURBOC3\\BGI");

   /*Initialize the variables and accept data*/
   int r,xc,yc,x,y,p;
   cout<<"Enter the radius:";
   cin>>r;
   cout<<"Enter the center co-ordinates:";
   cin>>xc>>yc;
   x=0;

   y=r;
   p=1-r;

   /*Calculate and plot the points*/
   do
   {
    putpixel(xc+x,yc+y,WHITE);
    putpixel(xc+y,yc+x,WHITE);
    putpixel(xc-y,yc+x,WHITE);
    putpixel(xc+x,yc-y,WHITE);
    putpixel(xc-x,yc-y,WHITE);
    putpixel(xc-y,yc-x,WHITE);
    putpixel(xc+y,yc-x,WHITE);
    putpixel(xc-x,yc+y,WHITE);
    if(p<0)
    {
     x=x+1;
     y=y;
     p=p+(2*x)+1;
    }
   else
   {
    x=x+1;
    y=y-1;
    p=p+(2*x)+1-(2*y);
   }
  }
  while(x<=y);
  //start boundary filling
  bfill(xc,yc,4);
  ffill(xc,yc,6);

   /* clean up */
   getch();
   }
void bfill(int x,int y,int a)
{int current;
 current=getpixel(x,y);
 if((current!=a)&&(current!=WHITE))
 {
 putpixel(x,y,a);
 delay(5);
 bfill(x+1,y,a);
 bfill(x-1,y,a);
 bfill(x,y+1,a);
 bfill(x,y-1,a);
 }
}
 void ffill(int x,int y,int a)
{int current;
 current=getpixel(x,y);
 if((current!=a)&&(current==4))
 {
 putpixel(x,y,a);
 delay(5);
 ffill(x+1,y,a);
 ffill(x-1,y,a);
 ffill(x,y+1,a);
 ffill(x,y-1,a);
 }
}

Mid point ellipse


#include <graphics.h>
#include <iostream.h>
#include <math.h>
#include <conio.h>

 void main()
{
   clrscr();
    float rx,ry,xc,yc,x0,y0,x,y;
    float p1,p2;

    int gdriver = DETECT, gmode;
    initgraph(&gdriver, &gmode, "C:\\TURBOC3\\BGI");

    cout<<"enter rx and ry=";
    cin>>rx>>ry;

    cout<<"enter xc and yc=";
    cin>>xc>>yc;

    x0=0;
    y0=ry;
    x=x0;
    y=y0;

    putpixel(xc+x0,yc+y0,RED);
    putpixel(xc+x0,yc-y0,RED);

    p1=(ry*ry)-(rx*rx*ry)+((rx*rx)/4);
    do
    {
    x++;
    if(p1<0)
    {
     p1=p1+(2*ry*ry*x)+(ry*ry);
     }
     else
     {
     y--;
     p1=p1+(2*ry*ry*x)-(2*rx*rx*y)+(ry*ry);
     }

      putpixel(x+xc,y+yc,RED);
      putpixel((-x+xc),(-y+yc),RED);
      putpixel((-x+xc),y+yc,RED);
      putpixel(x+xc,(-y+yc),RED);
     }while(ry*ry*x>=radd titlex*rx*y);


    p2=(ry*ry*(x+0.5)*(x+0.5))+(rx*rx)*(y-1)*(y-1)-(rx*rx)*(ry*ry);

    do
    {
    y--;
    if(p2>0)
    {
    p2=p2-(2*rx*rx*y)+(rx*rx);
    }
    else
    {
    x++;
    p2=p2-(2*rx*rx*y)+(2*ry*ry*x)+(rx*rx);
    }
    putpixel(x+xc,y+yc,RED);
    putpixel((-x+xc),(-y+yc),RED);
    putpixel((-x+xc),y+yc,RED);
    putpixel(x+xc,(-y+yc),RED);
    }while(y!=0||x!=rx);

   getch();
   closegraph();
}

Total Pageviews

DjKiRu Initative. Powered by Blogger.