Doraemon

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 40

AMRITA VISHWA VIDYAPEETHAM

AMRITA SCHOOL OF COMPUTING, MYSORE

LAB RECORD – COMPUTER GRAPHICS


DEPARTMENT OF COMPUTER SCIENCE

PROGRAM: INTEGRATED MCA


SEMESTER: Ⅵ
COURSE TITLE: COMPUTER GRAPHICS
COURSE CODE: 18CSA383

SUBMITTED TO: SUBMITTED BY,


KAVYA T.G CHARISHMA.K. S
ASSISTANT PROFESSOR MY.SC.I5MCA20004
DEPARTMENT OF COMPUTER SCIENCE

SIGNATURE DATE:
INDEX

Sl. No. CONTENTS Pg. No.

1 Raster graphics display 2

2 2D geometric primitives 2,3

3 Concentric circles 3,4

4 Display filled square 4,5

5 Change background color 5,6

6 Draw emoji 6,7

7 Draw a line 7,8

8 Bresenham’s algorithm 8,9

9 Moving car 9,10

10 Circle using midpoint 11

11 Slope less than 1 12

12 Translation 13,14

13 Rotation 14,15,16

14 Scaling 16,17

15 Flood fill algorithm 17,18

16 Boundary fill algorithm 18,19

17 Shearing of a line 18,19

18 Reflection 19,20,21

19 Polygon clipping 21-24

20 Window to viewport 24,25

21 Cohen Sutherland line clipping 26,27,28


1. Drawing a 2D line showing as Raster Graphics display

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

int main()
{
int gd= DETECT,gm,x1,y1,x2,y2;
initgraph(&gd,&gm,"C:\\TC\\BGI");
clrscr();
printf("Enter the start point coordinate");
scanf("%d%d", &x1, &y1);
printf("Enter the end point coordinate");
scanf("%d%d", &x2, &y2);
setbkcolor(DARKGRAY);
setcolor(RED);
line(x1,y1,x2,y2);
outtextxy(20,450,"Press any key to continue");
getch();
closegraph();
return 0;
}

OUTPUT-

2. 2D geometric primitives
#include<stdio.h>
#include<conio.h>
#include<graphics.h>

void main()
{
int gd=DETECT, gm;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
setbkcolor(GREEN);
printf("\t\t\t\n\n LINE");
line (50,40,190,40);
printf("\t\t\n\n\n\n RECTANGLE ");
rectangle(125,115,215,165);
printf("\t\t\t\n\n\n\n\n\n\n ARC");
arc(120,200,180,0,30);
printf("\t\n\n\n\n CIRCLE");
circle (120,270,30);
printf("\t\n\n\n\n ECLIPSE");
ellipse(120,350,0,360,30,20);
getch();
}

OUTPUT-

3. Concentric circles

#include<stdio.h>
#include<graphics.h>
int main()
{
int gd=DETECT, gm;
int x,y;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
x=getmaxx()/2;
y=getmaxy()/2;
outtextxy(240,50, "concentric circles");
setcolor(RED);
circle(x,y,30);
setcolor(GREEN);
circle(x,y,50);
setcolor(YELLOW);
circle(x,y,70);
setcolor(BLUE);
circle(x,y,90);
getch();
closegraph();
return 0;
}

OUTPUT-

4. Display filled square

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

main()
{
int gd, gm, s;
gd=DETECT;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
printf("Enter the value of side of square: ");
scanf("%d",&s);
setbkcolor(LIGHT GRAY);
setcolor(RED);
moveto(50,50);
lineto (50+s,50),
lineto (50,50+s);
lineto (50+s,50+s);
lineto (50,50);
outtextxy(30,450,"Press any key to continue...");
getch();
closegraph();
}

OUTPUT-

5. Change background color

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

void main()
{
int gd=DETECT , gm;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
outtextxy("Press any key to change the background color to green");
getch();
setbkcolor(GREEN);
getch();
closegraph();
}

OUTPUT-

6. Draw emoji

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

void main()
{
int gd=DETECT, gm;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
circle(300,180,80);
circle(270,150,10);
circle(320,150,10);
line(300,170,300,200);
arc(300,180,240,300,50);
getch();
closegraph();
}

OUTPUT-
7. Draw a line

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

void main()
{
int gd=DETECT, gm;
clrscr();
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
line(100,100,500,300);
getch();
closegraph();
}

OUTPUT-
8. Bresenham’s algorithm

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

void main(){
int gd=DETECT, gm;
int x1,y1,x2,y2,dx,dy,p,k,x,y;
clrscr();
printf("Enter the coordinates of starting point: ");
scanf("%d%d",&x1,&y1);
printf("Enter the cocarinates of ending point: ");
scanf("%d%d",&x2,&y2);
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
dx=x2-x1;
dy=y2-y1;
p=2*dy-dx;
x=x1;
y=y1;
putpixel(x,y,15);
for(k=1;k<=dx;k++)
{
if(p<0)
{
putpixel(x,y,15);
p=p+2*dy;
}
else
{
x=x+1;
y=y+1;
putpixel(x,y,15);
p=p+2*dy-2*dx;
}
}
outtextxy(200,10,"ILLUSTRATION OF BARSENHAM'S ALGORITHM");
outtextxy(x1+5,y1-5,"(x1,y1)");
outtextxy(x2+5,y2+5,"(x2,y2)");
getch();
closegraph();
restorecrtmode();
}

OUTPUT-

9. Moving car

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

int main()
{
int gd=DETECT,gm,i, maxx, cy;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
setbkcolor(WHITE);
setcolor(RED);
maxx=getmaxx()/2;
cy=getmaxy()/2;
for(i=0;i<maxx-140;i++)
{
cleardevice();
line(0+i,cy-20,0+i,cy+15);
line(0+i,cy-20,25+i,cy-20);
line(25+i,cy-20,40+i,cy-70);
line(40+i,cy-70,100+i,cy-70);
line(100+i,cy-70,115+i,cy-20);
line(115+i,cy-20,140+i,cy-20);
line(0+i,cy+15,18+i,cy+15);
circle(28+i,cy+15,10);
line (38+i,cy+15,102+i,cy+15);
circle(112+i,cy+15,10);
line(112+i,cy+15,140+i,cy+15);
line(140+i,cy+15, 140+i,cy-20);
rectangle(50+i,cy-62,90+i,cy-30);

setfillstyle(1, BLUE);
floodfill(5+i,cy-15,RED);
setfillstyle(1,LIGHTBLUE);
floodfill (52+i,cy-60,RED);
delay(10);
}
getch();
closegraph();
return 0;
}

OUTPUT-

10. Circle using midpoint

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void symmetry(int xc,int yc,int x,int y);
void main()
{
int gd=DETECT,gm;
int xc,yc,p,r,x,y;
clrscr();
printf("Enter the coordinates:");
scanf("%d%d",&xc,&yc);
printf("Enter the radius:");
scanf("%d",&r);
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
x=0;
y=r;
symmetry(xc,yc,x,y);
p=1-r;
while(x<=y)
{
if(p<0)
{
x=x+1;
p=p+2*x+1;
}
else{
x=x+1;
y=y-1;
p=p+2*x+1-2*y;
}
symmetry(xc,yc,x,y);
}
outtextxy(120,20,"Illustration of midpoint of circle");
outtextxy(xc-25,yc,"(xc,yc)");
getch();
closegraph();
restorecrtmode();
}
void symmetry(int xc,int yc,int x,int y)
{
putpixel(xc+x,yc+y,WHITE);
putpixel(xc-x,yc+y,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+y,yc-x,WHITE);
putpixel(xc-y,yc-x,WHITE);
}

OUTPUT-
11. Slope less than 1

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

void main()
{
int gd=DETECT,gm;
int x1=100,x2=200,y1=200,y2=400,dx,dy,p,k,x,y;
clrscr();
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
dx=x2-x1;
dy=y2-y1;
p=p+2*dy-dx;
x=x1;
y=y1;
while(y<y2)
{
putpixel(x,y,50);
if(p<0)
{
y=y+1;
p=p+2*dy;
}
else{
x=x+1;
y=y+1;
p=p+2*dx-2*dy;
}
}
outtextxy(200,20,"Slope less than 1");
getch();
closegraph();
restorecrtmode();
}

OUTPUT-

12. Translation

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
{
int gd=DETECT,gm;
int x1,y1,x2,y2,a,b;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
printf("Enter the value of coordinates:");
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
printf("Enter the x translating factor:");
scanf("%d",&a);
printf("Enter the y translating factor:");
scanf("%d",&b);
cleardevice();
outtextxy(200,20,"Line before translation");
line(x1,y1,x2,y2);
x1=x1+a;
y1=y1+b;
x2=x2+a;
y2=y2+b;
getch();
cleardevice();
outtextxy(200,20,"Line after translation");
line(x1,y1,x2,y2);
getch();
closegraph();
restorecrtmode();
}

OUTPUT-

13. Rotation

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

void main()
{
int gd=DETECT,gm,ch;
float x1,y1,x2,y2,a,x,y;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
do{
printf("Main menu");
printf("\n1.Rotation about origin");
printf("\n2.Rotation about fixed point");
printf("\nEnter your choice:0 for exit");
scanf("%d",&ch);
if(ch==0){
getch();
exit(1);
}
printf("Enter the line coordtinates:");
scanf("%f%f%f%f",&x1,&y1,&x2,&y2);
printf("Enter the angle of rotation");
scanf("%f",&a);
switch(ch)
{
case 1:
cleardevice();
outtextxy(250,20,"Line before rotation");
line(x1,y1,x2,y2);
a=a*(3.14/180);
x1=x1*cos(a)-y1*sin(a);
y1=x1*sin(a)+y1*cos(a);
x2=x2*cos(a)-y2*sin(a);
y2=x2*sin(a)+y2*cos(a);
getch();
cleardevice();
outtextxy(250,20,"Line afteer rotation");
line(x1,y1,x2,y2);
getch();
cleardevice();
break;

case 2:
printf("Enter the fixed point");
scanf("%f",&x,&y);
cleardevice();
outtextxy(250,20," Line before rotation");
line(x1,y1,x2,y2);
a=a*(3.14/180);
x1=x1*cos(a)-y1*sin(a)-x*cos(a)+y*sin(a)+x;
y1=x1*sin(a)+y1*cos(a)-x*sin(a)-y*cos(a)+y;
x2=x2*cos(a)-y2*sin(a)-x*cos(a)+y*sin(a)+x;
y2=x2*sin(a)+y2*cos(a)-x*sin(a)-y*cos(a)+y;
getch();
cleardevice();
outtextxy(250,20,"Line afteer rotation");
line(x1,y1,x2,y2);
getch();
cleardevice();
break;

default:
printf("invalid");
cleardevice();
}
}
while(ch!=0);
getch();
closegraph();
restorecrtmode();
}

OUTPUT-
14. Scaling

#include<stdio.h>
#include<graphics.h>
#include<conio.h>
#include<math.h>
void main(){
int gd=DETECT,gm;
int x1,y1,x2,y2,a,b,h,k,ch;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
do{
printf("MAin menu");
printf("\n1. scaling about origin");
printf("\n2. scaling about fixed point");
printf("\nEnter ur choice:0 for exit");
scanf("%d",&ch);
if(ch==0){
getch();
exit(1);
}
printf("Enter the cooridnates:");
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
printf("x factor");
scanf("%d",&a);
printf("y factor");
scanf("%d",&b);

switch(ch)
{
case 1:
cleardevice();
outtextxy(200,20,"before scaling");
line(x1,y1,x2,y2);
x1=x1*a;
y1=y1*b;
x2=x2*a;
y2=y2*b;
getch();
cleardevice();
outtextxy(200,20,"after scaling");
line(x1,y1,x2,y2);
getch();
cleardevice();
break;

case 2:
printf("Enter fixed point");
scanf("%d%d",&h,&k);
cleardevice();
outtextxy(200,20,"before scaling");
line(x1,y1,x2,y2);
x1=x1*a-h*a+h;
y1=y1*b-k*b+k;
x2=x2*a-h*a+h;
y2=y2*b-k*b+k;
getch();
cleardevice();
outtextxy(200,20,"after scaling");
line(x1,y1,x2,y2);
getch();
cleardevice();
break;

default:
printf("Invalid option");
cleardevice();

}
}
while(ch!=0);
getch();
closegraph();
restorecrtmode();
}

OUTPUT-
15. Flood fill algorithm

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

void floodFill(int x,int y, int fill, int newcol)


{
if(getpixel(x,y)!=newcol&&getpixel(x,y)!=fill)
{
putpixel(x,y,fill);
floodFill(x+1,y,fill, newcol);
floodFill(x,y+1,fill, newcol);
floodFill(x-1,y,fill, newcol);
floodFill(x,y-1,fill, newcol);
floodFill(x-1,y-1,fill, newcol);
floodFill(x-1,y+1,fill, newcol);
floodFill(x+1,y-1,fill, newcol);
floodFill(x+1,y+1,fill, newcol);
}
}
int main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
rectangle(50,50,100,100);
floodFill(55,55,4,15);
delay(1000);
getch();
closegraph();
return 0;
}

OUTPUT-

16. Boundary fill algorithm

#include<stdio.h>
#include<graphics.h>
void boundaryFill(int x, int y,int newcol,int oldcol)
{
if(getpixel(x,y)==oldcol)
{
putpixel(x,y,newcol);
boundaryFill(x+1,y,newcol,oldcol);
boundaryFill(x-1,y,newcol,oldcol);
boundaryFill(x,y+1,newcol,oldcol);
boundaryFill(x,y-1,newcol,oldcol);
}
}
int main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
rectangle(50,50,300,300);
boundaryFill(51,51,12,0);
getch();
closegraph();
return 0;
}

OUTPUT-

17. Shearing of a line

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

void main(){
int gd=DETECT,gm;
int x1,y1,x2,y2,a,b;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
printf("Enter the coordinates:");
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
printf("Enter x factor");
scanf("%d",&a);
printf("Enter y factor");
scanf("%d",&b);
cleardevice();
outtextxy(200,20,"before shearing");
line(x1,y1,x2,y2);
x1=x1+a*y1;
y1=y1+b*x1;
x2=x2+a*y2;
y2=y2+b*x2;
getch();
cleardevice();
outtextxy(200,20,"after shearing");
line(x1,y1,x2,y2);
getch();
closegraph();
restorecrtmode();
}

OUTPUT-

18. Reflection

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

void main(){
int gd=DETECT,gm;
float x1,y1,x2,y2,x3,y3,x4,y4,m,n,b;
int ch,x,y;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
do{
printf("1.ref about x");
printf("\n2.ref about y");
printf("\n3.ref about arbitary");
printf("enter ur choice:0 for exit");
scanf("%d",&ch);
if(ch==0){
getch();
exit(1);
}
printf("Enter coordinates:");
scanf("%f%f%f%f",&x1,&y1,&x2,&y2);
switch(ch){
case 1:
cleardevice();
outtextxy(200,20,"before");
x=getmaxx();
y=getmaxy();
line(0,y/2,x,y/2);
line(x/2,0,x/2,y);
line(x1+x/2,y1+y/2,x2+x/2,y2+y/2);
x1=x1+x/2;
y1=y1+y/2-2*y1;
x2=x2+x/2;
y2=y2+y/2-2*y2;
getch();
cleardevice();
outtextxy(200,20,"after");
line(x1,y1,x2,y2);
line(0,y/2,x,y/2);
line(x/2,0,x/2,y);
getch();
cleardevice();
break;

case 2:
cleardevice();
outtextxy(200,20,"before");
x=getmaxx();
y=getmaxy();
line(0,y/2,x,y/2);
line(x/2,0,x/2,y);
line(x1+x/2,y1+y/2,x2+x/2,y2+y/2);
x1=x1+x/2-2*x1;
y1=y1+y/2;
x2=x2+x/2-2*x2;
y2=y2+y/2;
getch();
cleardevice();
outtextxy(200,20,"after");
line(x1,y1,x2,y2);
line(0,y/2,x,y/2);
line(x/2,0,x/2,y);
getch();
cleardevice();
break;

case 3:
printf("enter arbitary");
scanf("%f%f%f%f",&x3,&y3,&x4,&y4);
cleardevice();
outtextxy(200,20,"before");
line(x1,y1,x2,y2);
line(x3,y3,x4,y4);
m=(y4-y3)/(x4-x3);
b=y3-m*x3;
n=m*m+1;
x1=(1-m*m)*x1/n+2*m*y1/n-2*b*m/n;
y1=(m*m-1)*y1/n+2*m*x1/n+2*b/n;
x2=(1-m*m)*x2/n+2*m*y2/n-2*b*m/n;
y2=(m*m-1)*y2/n+2*m*x2/n+2*b/n;
getch();
cleardevice();
outtextxy(200,20,"after");
line(x1,y1,x2,y2);
line(x3,y3,x4,y4);
getch();
cleardevice();
break;

default:
printf("incalid");
cleardevice();

}
}
while(ch!=0);
getch();
closegraph();
restorecrtmode();
}

OUTPUT-
19. Polygon clipping

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

#define TRUE 1
#define FALSE 0
typedef unsigned int outcode;
outcode CompOutCode(float x,float y);

enum{
TOP= 0x1,
BOTTOM= 0x2,
RIGHT=0x4,
LEFT=0x8
};

float xmin,xmax,ymin,ymax;
void clip(float x0,float y0,float x1,float y1)
{
outcode outcode0,outcodel,outcodeOut;
int accept = FALSE,done = FALSE;
outcode0 =CompOutCode(x0,y0);
outcodel= CompOutCode(x1,y1);

do
{
if(!(outcode0|outcodel))
{
accept = TRUE;
done = TRUE;
}
else
if(outcode0&outcodel)
done = TRUE;
else
{
float x,y;
outcodeOut = outcode0?outcode0:outcodel;
if(outcodeOut&TOP)
{
x=x0+(x1-x0)*(ymax-y0)/(y1-y0);
y=ymax;
}
else
if(outcodeOut&BOTTOM)
{
x = x0+(x1-x0)*(ymin-y0)/(y1-y0);
y=ymin;
}
else
if(outcodeOut&RIGHT)
{
y=y0+(y1-y0)*(xmax-x0)/(x1-x0);
x = xmax;
}
else
{
y=y0+(y1-y0)*(xmin-x0)/(x1-x0);
x = xmin;
}

if(outcodeOut==outcode0)
{
x0 = x;
y0 = y;
outcode0 =CompOutCode(x0,y0);
}
else
{
x1 = x;
y1=y;
outcodel =CompOutCode(x1,y1);
}}
}
while(done==FALSE);
if(accept)
line(x0,y0,x1,y1);
outtextxy(150,20,"POLYGON AFTER CLIPPING");
rectangle(xmin,ymin, xmax,ymax);
}
outcode CompOutCode(float x,float y){
outcode code = 0;
if(y>ymax)
code|=TOP;
else if(y<ymin)
code|=BOTTOM;
if(x>xmax)
code|=RIGHT;
else if(x<xmin)
code|=LEFT;
return code;
}
void main(){
float x1,y1,x2,y2;
int gdriver = DETECT, gmode, n,poly[14],i;
clrscr();
printf("Enter the no of sides of polygon:");
scanf("%d",&n);
printf("\nEnter the coordinates of polygon\n");
for(i=0;i<2*n;i++)
{
scanf("%d",&poly[i]);
}
poly[2*n]=poly[0];
poly[2*n+1]=poly[1];
printf("Enter the rectangular coordinates of clipping window\n");
scanf("%f%f%f%f",&xmin,&ymin, &xmax,&ymax);
initgraph(&gdriver, &gmode, "C:\\TURBOC3\\BGI");
outtextxy(150,20,"POLYGON BEFORE CLIPPING");
drawpoly(n+1,poly);
rectangle(xmin,ymin,xmax,ymax);
getch( );
cleardevice();
for(i=0;i<n;i++)
clip(poly[2*i],poly[(2*i)+1],poly[(2*i)+2],poly[(2*i)+3]);
getch( );
restorecrtmode();
}

OUTPUT-

20. Window to viewport

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main(){
int gd=DETECT,gm,i,n,poly[14],poly1[14];
int wxmin,wymin,wxmax,wymax,vxmin,vymin,vxmax,vymax;
float sx,sy;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
printf("Enter coordinates:\nwxmin=");
scanf("%d",&wxmin);
printf("\nwymin=");
scanf("%d",&wymin);
printf("\nwxmax=");
scanf("%d",&wxmax);
printf("\nwymax=");
scanf("%d",&wymax);
printf("Enter coordinates viewport:\nvxmin=");
scanf("%d",&vxmin);
printf("\nvymin=");
scanf("%d",&vymin);
printf("\nvxmax=");
scanf("%d",&vxmax);
printf("\nvymax=");
scanf("%d",&vymax);
printf("Enter sides:");
scanf("%d",&n);
printf("Enter coordinates:");
for(i=0;i<2*n;i++){
scanf("%d",&poly[i]);
}
cleardevice();
poly[2*n]=poly[0];
poly[2*n+1]=poly[1];
drawpoly(n+1,poly);
rectangle(wxmin,wymin,wxmax,wymax);
sx=(vxmax-vxmin)/(wxmax-wxmin);
sy=(vymax-vymin)/(wymax-wymin);
for(i=0;i<n;i++){
poly1[2*i]=sx*(poly[2*i]-wxmin)+vxmin;
poly1[2*i+1]=sy*(poly[2*i+1]-wymin)+vymin;
}
poly1[2*n]=poly1[0];
poly1[2*n+1]=poly1[1];
rectangle(vxmin,vymin,vxmax,vymax);
outtextxy(200,20,"window to vport");
drawpoly(n+1,poly1);
getch();
closegraph();
}

OUTPUT-
21. Cohen Sutherland line clipping

#include<stdio.h>
#include<graphics.h>
#include<conio.h>
#include<math.h>
#define TRUE 1
#define FALSE 0
typedef unsigned int outcode;
outcode CompOutCode(float x,float y);

enum{TOP=0x1,
BOTTOM=0x2,
RIGHT=0x4,
LEFT=0x8
};

float xmin,xmax,ymin,ymax;
void clip(float x0,float y0,float x1,float y1)
{
outcode outcode0, outcode1, outcodeOut;
int accept=FALSE, done=FALSE;
outcode0=CompOutCode(x0,y0);
outcode1=CompOutCode(x1,y1);
do
{
if(!(outcode0|outcode1))
{
accept=TRUE;
done=TRUE;
}
else
if(outcode0&outcode1)
done=TRUE;
else{
float x,y;
outcodeOut=outcode0?outcode0:outcode1;
if(outcodeOut&TOP){
x=x0+(x1-x0)*(ymax-y0)/(y1-y0);
y=ymax;
}
else if(outcodeOut&BOTTOM){
x=x0+(x1-x0)*(ymin-y0)/(y1-y0);
y=ymin;
}
else if(outcodeOut&RIGHT){
y=y0+(y1-y0)*(xmax-x0)/(x1-x0);
x=xmax;
}
else{
y=y0+(y1-y0)*(xmin-x0)/(x1-x0);
x=xmin;
}
if(outcodeOut==outcode0)
{
x0=x;
y0=y;
outcode0=CompOutCode(x0,y0);
}
else{
x1=x;
y1=y;
outcode1=CompOutCode(x1,y1);

}
}
}
while(done==FALSE);
if(accept)
line(x0,y0,x1,y1);
outtextxy(200,20,"Lone after clipping");
rectangle(xmin,ymin,xmax,ymax);
}
outcode CompOutCode(float x,float y)
{
outcode code=0;
if(y>ymax)
code|=TOP;
else if(y<ymin)
code|=BOTTOM;
else if(x>xmax)
code|=RIGHT;
else if(x<xmin)
code|=LEFT;
return code;
}
void main()
{
float x1,y1,x2,y2;
int gd=DETECT,gm;
printf("Enter the ending point:");
scanf("%f%f%f%f",&x1,&y1,&x2,&y2);
printf("Enter the recantangular coordinates:");
scanf("%f%f%f%f",&xmin,&ymin,&xmax,&ymax);
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
outtextxy(200,20,"Before clipping");
line(x1,y1,x2,y2);
rectangle(xmin,ymin,xmax,ymax);
getch();
cleardevice();
clip(x1,y1,x2,y2);
getch();
restorecrtmode();
}

OUTPUT-
ANIMATED DORAEMON DRAWING

Problem Statement:
The problem is to create an animated drawing of the popular character Doraemon using the
graphics library in C. The objective is to develop a program that can generate a visually
appealing and recognizable representation of Doraemon on the screen through a series of
animated iterations. The program should utilize various graphics functions to draw the different
body parts of Doraemon, apply color settings to create visual variations, and introduce a gradual
animation effect to bring the character to life. The final output should resemble Doraemon and
capture its characteristic features, such as the round face, blue body, bell, and other recognizable
elements.

Introduction:
The given code is a C program that uses the graphics.h library to draw a character named
Doraemon on the screen. Doraemon is a popular Japanese manga and anime character. The code
utilizes various graphics functions to create the shape and features of the character. It uses shapes
such as ellipses, circles, and lines to depict different body parts of Doraemon, including the head,
body, hands, legs, tail, and facial features. The program begins by including the necessary header
files, such as `stdio.h`, `conio.h`, `dos.h`, `stdlib.h`, and `graphics.h`. These header files provide
the required functions and libraries for performing input/output operations, console operations,
DOS-related functions, standard library functions, and graphics functions respectively.
The `main` function is the entry point of the program. It initializes the graphics mode using the
`initgraph` function. The function takes the graphics driver, graphics mode, and the path to the
BGI (Borland Graphics Interface) directory as parameters. This function sets up the graphics
environment for drawing on the screen. Inside the main function, there is a loop that iterates 50
times. Within each iteration of the loop, various graphics functions are used to draw different
body parts of the Doraemon character.
The code utilizes functions such as `setcolor` to set the color for drawing, `ellipse` to draw
ellipses, `line` to draw lines, and `circle` to draw circles. Each body part is positioned, shaped,
and sized using specific coordinates and parameters. The program uses different colors to render
the character. It sets the color using `setcolor` function with numeric codes. By changing the
color within each iteration of the loop, the character's appearance changes over time.
At the end of each iteration, the program introduces a delay of 350 milliseconds using the `delay`
function. This creates an animation effect as the character gradually appears on the screen.
Finally, the program waits for a key press using the `getch` function, and then closes the graphics
mode using the `closegraph` function. Overall, the code combines the use of graphics functions,
loops, and color manipulation to create an animated drawing of the Doraemon character.
Hardware Requirements:
1. Computer or laptop
2. Sufficient RAM to run the graphics functions smoothly
3. Display monitor or screen to view the output

Software Requirements:
1. C compiler (such as Turbo C++, Code::Blocks, or Visual Studio)
2. Graphics.h library
3. Operating system that supports the chosen C compiler and graphics library (e.g., Windows,
Linux)

Code:
#include<stdio.h>
#include<conio.h>
#include<dos.h>
#include<stdlib.h>
#include<graphics.h>
void main()
{
int i;
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\turboc3\\bgi");

for(i=0;i<50;i++)
{
//________LEGS______________//
setcolor(15);
ellipse(200,450,150,390,50,16);
line(157,441,243,441);
ellipse(252,448,246,389,50,18);
line(243,441,297,441);
setcolor(i);
ellipse(249,441,90,180,10,15);

//_______TAIL
setcolor(12);
ellipse(155,410,80,280,8,8);

setcolor(i);
line(157,441,157,370);
line(297,441,297,350);

//_______left hand
setcolor(15);
circle(147,357,16);
setcolor(i);
ellipse(137,342,300,330,58,19);
ellipse(174,348,110,170,29,49);

//______right hand
setcolor(15);
circle(327,275,16);
setcolor(i);
ellipse(265,260,299,340,74,90);
ellipse(323,285,110,170,25,19);

//___NECK
setcolor(12);
line(163,302,285,302);
line(163,298,285,298);
ellipse(163,301,90,270,3,3);
ellipse(285,300,280,80,4,3);

//_____tummy bag
setcolor(15);
ellipse(243,334,143,40,56,48);
ellipse(244,346,163,15,30,24);
line(214,340,273,340);

//________bell
setcolor(14);
circle(244,310,8);
line(238,306,251,306);
line(236,309,251,309);

//______head
setcolor(i);
ellipse(220,237,0,180,87,60);
ellipse(180,237,180,250,47,65);
ellipse(268,237,290,360,40,65);

//_______face
setcolor(15);
ellipse(263,277,15,90,45,60);
ellipse(245,210,0,360,19,23);
ellipse(207,210,0,360,19,23);
ellipse(202,237,190,243,47,69);
ellipse(187,254,90,170,32,35);
circle(216,217,5);
circle(237,218,5);

//_____nose
setcolor(12);
circle(227,236,8);
ellipse(230,262,180,360,45,30);
ellipse(232,263,10,190,47,6);

setcolor(15);
ellipse(275,253,290,110,10,8);

line(227,244,227,257);
line(245,238,269,234);
line(245,244,269,242);
line(245,250,269,252);

line(185,236,210,238);
line(185,244,210,244);
line(185,252,210,250);

//__________text
settextstyle(3,0,2);
setcolor(i);
outtextxy(100,50,"DORAEMON");
delay(350);
}
getch();
closegraph();
}
Output:

Future Enhancements:

1) Port the code to a modern compiler: Turbo C is an outdated compiler, and it may not
work on modern systems. Consider using a modern compiler like GCC or Clang, and
update the code to be compatible with the new compiler.

2) Use a graphics library compatible with the chosen compiler: Since the graphics.h library
is specific to the Turbo C compiler, it may not be available in other compilers. Explore
alternative graphics libraries such as SDL, OpenGL, or SFML that can be used with
modern compilers.

3) Implement modularization and object-oriented programming (OOP) concepts: The


current code is written in a procedural style. To enhance the code's maintainability and
readability, you can break it down into smaller functions or implement an object-oriented
approach by creating classes for different parts of the Doraemon animation.
4) Add interactivity: You can introduce user interaction to the animation by responding to
keyboard or mouse events. For example, you could add functionality to move Doraemon
or trigger different animations based on user input.

5) Enhance the visual effects: Experiment with different graphical effects, such as shading,
transparency, or particle effects, to make the animation more visually appealing.

6) Implement additional animations: Expand the program by adding more animations or


interactions. For example, you could create additional movements for Doraemon, animate
other characters, or create a complete scene for Doraemon to interact with.

You might also like