0% found this document useful (0 votes)
192 views93 pages

Content

The document describes a C graphics program to create a basic paint application. It includes functions to draw different shapes like lines, circles, rectangles, ellipses using mouse input. The functions change colors, clear the screen, and include options like New, Open, Save. Structures and unions are used to initialize functions. Mouse functions like show/hide mouse pointer, get mouse position are used. On execution, the program displays the paint interface and allows drawing and editing graphics.

Uploaded by

Titu Biswas
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
192 views93 pages

Content

The document describes a C graphics program to create a basic paint application. It includes functions to draw different shapes like lines, circles, rectangles, ellipses using mouse input. The functions change colors, clear the screen, and include options like New, Open, Save. Structures and unions are used to initialize functions. Mouse functions like show/hide mouse pointer, get mouse position are used. On execution, the program displays the paint interface and allows drawing and editing graphics.

Uploaded by

Titu Biswas
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 93

BASIC SHAPES AIM: To write a program to draw the basic shapes such as circle, line, rectangle, ellipse and

display text on screen using c graphics. PROCEDURE: Start the program by importing the header files such as graphics.h and conio.h. Initialize the graph. Draw the basic shapes by using the different functions such as rectangle(), circle(), arc(), line(), ellipse(), fillellipse(), pieslice(). Use the setcolor() to differentiate the different shapes. Fill the shapes by using different styles such as HATCH_FILL, SOLID_FILL and so on. Close the graph and stop the program. SOURCE CODE:
#include<graphics.h> #include<conio.h> main() { int gd=DETECT,gm,left=100,top=100,right=200,bottom=200,x=300,y=150,radius=50; initgraph(&gd,&gm,"C:/TC/BGI"); rectangle(left,top,right,bottom); setcolor(9); circle(x,y,radius); setcolor(5); arc(100,350,0,120,radius); setcolor(10); setbkcolor(3); setfillstyle(HATCH_FILL,6); setlinestyle(SOLID_LINE,1,1); line(left,top+150,left+100,top+200); ellipse(500,y,0,360,60,40); fillellipse(500,300,60,40); lineto(300,500); pieslice(210,300,0,30,radius); outtextxy(left+100,top+250,"Sample Text"); sector(300,300,0,70,60,50); getch(); closegraph(); return 0; }

OUTPUT:

RESULT:

Thus the above code was implemented and executed successfully and the output was shown.

BARCHART

AIM: To write a C graphicsprogram to draw a bar chart filled with different styles and in different colors. PROCEDURE: Start the program by importing the header files such as graphics.h and conio.h Initialize the graph. Draw the basic shapes by using the different functions such as rectangle(), line(), bar(). Use the setcolor() to differentiate the different shapes. Fill the shapes by using different styles such as HATCH_FILL, SOLID_FILL and so on. Close the graph and stop the program. SOURCE CODE: #include<graphics.h> #include<conio.h> main() { int gd=DETECT,gm; initgraph(&gd,&gm,"C:\\TC\\BGI"); setcolor(YELLOW); rectangle(0,30,639,450); settextstyle(SANS_SERIF_FONT,HORIZ_DIR,2); setcolor(WHITE); outtextxy(275,0,"BAR CHART"); setlinestyle(SOLID_LINE,0,2); line(100,420,100,60); line(100,420,600,420); line(90,70,100,60); line(110,70,100,60); line(590,410,600,420); line(590,430,600,420); outtextxy(95,35,"y"); outtextxy(610,405,"x"); outtextxy(85,415,"0"); setfillstyle(LINE_FILL,BLUE); bar(150,100,200,419); setfillstyle(XHATCH_FILL,RED); bar(225,150,275,419); setfillstyle(WIDE_DOT_FILL,GREEN); bar(300,200,350,419); setfillstyle(INTERLEAVE_FILL,MAGENTA); bar(375,125,425,419); setfillstyle(HATCH_FILL,BROWN); bar(450,175,500,419); getch(); return 0; }

OUTPUT:

RESULT: Thus the above code was implemented and executed successfully and the output was shown.

PIE CHART

AIM: To write a C graphics program to draw a pie chart showing percentage of various components drawn with different filling styles and colors.
PROCEDURE:

Start the program by importing the header files such as graphics.h and conio.h. Initialize the graph. Draw the basic shapes by using the different functions such as rectangle(),pieslice(). Use the setcolor() to differentiate the different shapes. Fill the shapes by using different styles such as HATCH_FILL, SOLID_FILL and so on. Print the text by using outtext(). Calculate the midx and midy by dividing the maximum value with two. Close the graph and stop the program. SOURCE CODE:
#include<graphics.h> #include<conio.h> main() { int gd=DETECT,gm,midx,midy; initgraph(&gd,&gm,"C:\\TC\\BGI"); setcolor(MAGENTA); rectangle(0,40,639,450); settextstyle(SANS_SERIF_FONT,HORIZ_DIR,2); setcolor(WHITE); outtextxy(275,10,"PIE CHART"); midx=getmaxx()/2; midy=getmaxy()/2; setfillstyle(LINE_FILL,BLUE); pieslice(midx,midy,0,75,100); outtextxy(midx+100,midy-75,"20.83%"); setfillstyle(XHATCH_FILL,RED); pieslice(midx,midy,75,225,100); outtextxy(midx-175,midy-75,"41.62%"); setfillstyle(WIDE_DOT_FILL,GREEN); pieslice(midx,midy,225,360,100); outtextxy(midx+75,midy+75,"37.50%"); getch(); return 0; }

OUTPUT:

RESULT: Thus the above code was implemented and executed successfully and the output was shown.

CIRCLES IN CIRCLES

AIM: To write a C graphics program to draw the circles in circles using two different colors. PROCEDURE: Start the program by importing the header files such as graphics.h and conio.h. Initialize the graph. Draw the basic shapes by using the different functions such as circle(), arc(). Use the setcolor() to differentiate the different shapes. Get the coordinates of arc by dividing the maxx and maxy to get the midx and midy value. Draw the circle by incresing the angle value with the delay. Close the graph and stop the program. SOURCE CODE:
#include<graphics.h> #include<conio.h> #include<dos.h> main() { int gd=DETECT,gm,x,y,color,angle=0; struct arccoordstype a; initgraph(&gd,&gm,"C:\\tc\\BGI"); delay(2000); while(angle<=360) { setcolor(BLACK); arc(getmaxx()/2,getmaxy()/2,angle,angle+2,100); setcolor(WHITE); getarccoords(&a); circle(a.xstart,a.ystart,25); setcolor(BLACK); arc(getmaxx()/2,getmaxy()/2,angle,angle+2,150); getarccoords(&a); setcolor(MAGENTA); circle(a.xstart,a.ystart,25); angle=angle+5; delay(50); } getch(); closegraph(); return 0; }

OUTPUT:

RESULT:

Thus the above code was implemented and executed successfully and the output was shown.

COUNT DOWN AIM: To write a C graphics program to perform countdown for 30 seconds. PROCEDURE: Start the program by importing the header files such as graphics.h and conio.h. Initialize the graph. Use the setcolor() to differentiate the different shapes and settextjustify() to justify the text. Fill the shapes by using different styles such as HATCH_FILL, SOLID_FILL and so on. Display the outtext by dividing the maximum value by two to get the mid value. Close the graph and stop the program. SOURCE CODE:
#include<graphics.h> #include<dos.h> #include<conio.h> main() { int gd=DETECT,gm,i; char a[5]; initgraph(&gd,&gm,"C:\\TC\\BGI"); settextjustify(CENTER_TEXT,CENTER_TEXT); settextstyle(DEFAULT_FONT,HORIZ_DIR,3); setcolor(RED); for(i=30;i>=0;i--) { sprintf(a,"%d",i); outtextxy(getmaxx()/2,getmaxy()/2,a); delay(1000); if(i==0) break; cleardevice(); } getch(); closegraph(); return 0; }

OUTPUT:

RESULT:

Thus the above code was implemented and executed successfully and the output was shown.

10

PAINT APPLICATION AIM: To write a C graphics program to create a Paint application that draws different shapes using mouse such as line, circle, pixel and many other shapes. It can also changes the color, clears the screen. PROCEDURE: Start the program by importing the header files such as graphics.h and conio.h. Initialize the graph. Use the setcolor() to differentiate the different shapes. Fill the shapes by using different styles such as HATCH_FILL, SOLID_FILL and so on. Use the struct and union to initialize the functions. Use the different functions such as initialize(); structure(); getresponse(); check(); mouse(); showmouseptr(); restrictmouseptr(); getmousepos(); hidemouseptr(); freehand(); rect(); circ(); lin(); mypoly(); paint(); eraser(); changemouseptr(); printmousepos(); New(); Open(); restore(); Save();About();bressline();bressline1();drawline (); drawline1();Ellipse();Ellipse1();save(); clearline() to crate the paint application. Write the necessary code for each functions and call those code using the switch case inorder to execute the code. Close the graph and stop the program. SOURCE CODE:
#include<stdio.h> #include<conio.h> #include<graphics.h> #include<dos.h> #include<stdlib.h> #include<math.h> #define INCR 1 #define DECR -1 #define PREDX 1 #define PREDY 0 int gm,gd=DETECT,button,x,y,ch,fg,bg,dx, dy, e, e_inc, e_noinc,j,w,h; union REGS i,o; struct SREGS s ; char str[10]; char buffer[3000]; int hourglass[32]={ /*hourglass screen mask*/ 0x0000,0x0000,0x0000,0x0000, 0x8001,0xc003,0xf00f,0xfc3f, 0xfc3f,0xf00f,0xc003,0x8001, 0x0000,0x0000,0x0000,0x0000, /*the mouse ptr bitmap*/ 0xffff,0x8001,0xffff,0x8001, 0x4002,0x2004,0x1008,0x0240, 0x0240,0x0810,0x2004,0x4002, 11

0x8001,0xffff,0x8001,0xffff}; int plus[32]={ /*+ screen mask*/ 0xfe7f,0xfe7f,0xfe7f,0xfe7f, 0xfe7f,0xfe7f,0xfe7f,0x0000, 0x0000,0xfe7f,0xfe7f,0xfe7f, 0xfe7f,0xfe7f,0xfe7f,0xfe7f, /*+mouseptr bitmap*/ 0x0180,0x0180,0x0180,0x0180, 0x0180,0x0180,0x0180,0xfe7f, 0xfe7f,0x0180,0x0180,0x0180, 0x0180,0x0180,0x0180,0x0180}; int rubber[32]={ /*Eraser screen mask*/ 0x0000,0x0000,0x0000,0x0000, 0x0000,0x0000,0x0000,0x0000, 0x0000,0x0000,0x0000,0x0000, 0x0000,0x0000,0x0000,0x0000, /*eraser bitmap*/ 0xffff,0x8001,0x8001,0x8001, 0x8001,0x8001,0x8001,0x8001, 0x8001,0x8001,0x8001,0x8001, 0x8001,0x8001,0x8001,0xffff}; int hand[32]={/*hand-screenmask+pointer bitmap*/ 0xe1ff,0xe1ff,0xe1ff,0xe1ff, 0xe1ff,0x0000,0x0000,0x0000, 0x0000,0x0000,0x0000,0x0000, 0x0000,0x0000,0x0000,0x0000, 0x1e00,0x1200,0x1200,0x2100, 0x13ff,0x1249,0x1249,0xf249, 0x9001,0x9001,0x9001,0x8001, 0x8001,0x8001,0xffff,0x0000}; int pencil[32] = { 0x3ff ,0x5ff ,0x6ff ,0x1b7f, 0x1dbf,0x6edf,0xb76f,0xdbb7, 0xeddb,0xf6ed,0xfb76,0xfdb8, 0xfed9,0xff63,0xffa7,0xffcf, 0xfc00,0xfa00,0xf900,0xe480, 0xe240,0x9120,0x4890,0x2448, 0x1224,0x912 ,0x489 ,0x247 , 0x126 ,0x9c ,0x58 ,0x30 }; int bottle[32]={ 0x0000,0xbffe,0xdffe,0xeffe, 0xf7fe,0xf7fe,0xf7fe,0xf7fe, 0xf000,0xf000,0xf000,0xf7fe, 0xf7fe,0xf7fe,0xf7fe,0xf000, 0xffff,0x4001,0x2001,0x1001, 0x801 ,0x801 ,0x801 ,0x801 , 0xfff ,0xfff ,0xfff ,0x801 , 0x801 ,0x801 ,0x801 ,0xfff }; char NEW[]={"Creates A New File"}; char OPEN[]={"Opens An Existing File"}; char SAVE[]={"Saves The Active File"}; char ABOUT[]={"Displays Information About Program"}; char PENCIL[]={"Draws A Free Form Line One Pixel Width"}; 12

char PAINT[]={"Fills An Area With Current Drawing Colour select draw color by right click"}; char RECTANGLE[]={"Draws A Rectangle"}; char ELLIPSE[]={"Draws An Ellipse"}; char LINE[]={"Draws A Straight Line"}; char POLYLINE[]={"Draws A Polygon"}; char COLOUR[]={"Left Click -> Foreground Color Right Click -> Background Color"}; char ERASER[]={"Erases A Portion Of Figure"}; char EXIT[]={"Quits Program"}; void initialize(); void structure(); int getresponse(); int check(); int initmouse(); void showmouseptr(); void restrictmouseptr(int,int,int,int); void getmousepos(); void hidemouseptr(); void freehand(); void rect(); void circ(); void lin(); void mypoly(); void paint(); void eraser(); void changemouseptr(int *); void printmousepos(); void New(); void Open(); int restore(int,int,int,int,char*); void Save(); void About(); void bressline ( int, int, int, int ); void bressline1( int, int, int, int); void drawline ( int , int , int , int , int , int ); void drawline1 ( int , int , int, int , int, int ); void Ellipse(int ,int ,int ,int ); void Ellipse1(int ,int ,int ,int ); void save(int,int ,int,int,char*); void clearline(); void main() { int pch=1; clrscr(); initialize(); structure(); showmouseptr(); while(1) { setcolor(fg); rectangle(35,40,600,450); ch=getresponse(); if(ch==0||ch==35) ch=pch; switch(ch) { 13

case 1: rect(); break; case 2: freehand(); break; case 3: circ(); break; case 4: eraser(); break; case 5: lin(); break; case 6: paint(); break; case 7: mypoly(); break; case 30: if(ch!=pch) New(); break; case 31: if(ch!=pch) Open(); break; case 32: if(ch!=pch) Save(); break; case 33: if(ch!=pch) About(); break; case 34: if(ch!=pch) closegraph(); remove("c:/temp.tmp"); exit(0); default: if(button==1) { fg=ch-10; setfillstyle(SOLID_FILL,fg); setcolor(15); bar(2,445,17,465); rectangle(2,445,17,465); setcolor(fg); rectangle(35,40,600,450); } else if(button==2) { 14

bg=ch-10; setcolor(15); setfillstyle(SOLID_FILL,bg); bar(10,455,25,475); rectangle(10,455,25,475); setfillstyle(SOLID_FILL,fg); bar(2,445,17,465); rectangle(2,445,17,465); setcolor(fg); } else setcolor(4); outtextxy(35,457,COLOUR); setcolor(fg); break; } if(ch>30&&ch<=33||ch>=10&&ch<=25) pch=pch; else pch=ch; } } void initialize() { initgraph(&gd,&gm,"c:\\tc\\bgi"); if(initmouse()==0) { closegraph(); printf("\nMouse Driver is not present"); getch(); exit(0); } } initmouse() { i.x.ax=0; int86(0x33,&i,&o); return(o.x.ax); } void showmouseptr() { i.x.ax=1; int86(0x33,&i,&o); } void restrictmouseptr(int x1,int y1,int x2,int y2) { i.x.ax=7; i.x.cx=x1; i.x.dx=x2; int86(0x33,&i,&o); i.x.ax=8; i.x.cx=y1; i.x.dx=y2; int86(0x33,&i,&o); } 15

void getmousepos() { i.x.ax=3; int86(0x33,&i,&o); button=o.x.bx; x=o.x.cx; y=o.x.dx; } void hidemouseptr() { i.x.ax=2; int86(0x33,&i,&o); } void printmousepos() { int x1,y1,x2,y2,i,j,col; col=getcolor(); setcolor(11); x1=515+textwidth("x:"); y1=455+1; x2=x1+textwidth(str); y2=y1+textheight(str); for(i=x1;i<=x2;i++) for(j=y1;j<=y2;j++) putpixel(i,j,7); outtextxy(515,455,"x: y: "); sprintf(str,"%3d %3d",x-35,y-40); outtextxy(x1,y1,str); setcolor(col); } void changemouseptr ( int *shape ) { i.x.ax = 9 ; /* service number */ i.x.bx = 0 ; /* actual cursor position from left */ i.x.cx = 0 ; /* actual cursor position from top */ i.x.dx = ( unsigned ) shape ; /* offset address of pointer image */ segread ( &s ) ; s.es = s.ds ; /* segment address of pointer */ int86x ( 0x33, &i, &i, &s ) ; } int check() { int row1,row2,i,flag=0,col1,col2; for(row1=100,i=1;i<=7;i++,row1=row1+45) { row2=row1+20; if(x>7&&x<27&&y>row1&&y<row2) { flag=1; goto down; } } for(row1=50,i=10;i<=25;i++,row1=row1+25) { row2=row1+20; 16

if(x>610&&x<630&&y>row1&&y<row2) { flag=1; goto down; } } for(i=30;i<=34;i++) { if(i==30) { col2=260;col1=220; } else if(i==31) { col1=270;col2=310; } else if(i==32) { col1=320;col2=360; } else if(i==33) { col1=370;col2=420; } else { col1=427;col2=465; } if(x>col1&&x<col2&&y>20&&y<35) { if(button==1) { flag=1; break; } } } down: if(flag==1) return i; else return 0; } int getresponse() { int col,X; static int bn; static int pbn; static int flag; col=getcolor(); getmousepos(); bn=check(); if(bn>=0&&bn<=7||bn>=10) { if(pbn!=bn) { switch(pbn) { case 1: setcolor(15); hidemouseptr(); 17

rectangle(7,100,27,120); showmouseptr(); break; case 2: setcolor(15); hidemouseptr(); rectangle(7,145,27,165); showmouseptr(); break; case 3: setcolor(15); hidemouseptr(); rectangle(7,190,27,210); showmouseptr(); break; case 4: setcolor(15); hidemouseptr(); rectangle(7,235,27,255); showmouseptr(); break; case 5: setcolor(15); hidemouseptr(); rectangle(7,280,27,300); showmouseptr(); break; case 6: setcolor(15); hidemouseptr(); rectangle(7,325,27,345); showmouseptr(); break; case 7: setcolor(15); hidemouseptr(); rectangle(7,370,27,390); showmouseptr(); break; case 0: clearline(); } } switch(bn) { case 1: if(pbn!=bn) { setcolor(RED); hidemouseptr(); rectangle(7,100,27,120); showmouseptr(); flag=0; outtextxy(35,457,RECTANGLE); } 18

else if(button==1) { setcolor(13); hidemouseptr(); rectangle(7,100,27,120); showmouseptr(); flag=1; } break; case 2: if(pbn!=bn) { setcolor(RED); hidemouseptr(); rectangle(7,145,27,165); showmouseptr(); flag=0; outtextxy(35,457,PENCIL); } else if(button==1) { setcolor(13); hidemouseptr(); rectangle(7,145,27,165); showmouseptr(); flag=1; } break; case 3: if(pbn!=bn) { setcolor(RED); hidemouseptr(); rectangle(7,190,27,210); showmouseptr(); flag=0; outtextxy(35,457,ELLIPSE); } else if(button==1) { setcolor(13); hidemouseptr(); rectangle(7,190,27,210); showmouseptr(); flag=1; } break; case 4: if(pbn!=bn) { setcolor(RED); hidemouseptr(); rectangle(7,235,27,255); showmouseptr(); flag=0; 19

outtextxy(35,457,ERASER); } else if(button==1) { setcolor(13); hidemouseptr(); rectangle(7,235,27,255); showmouseptr(); flag=1; } break; case 5: if(pbn!=bn) { setcolor(RED); hidemouseptr(); rectangle(7,280,27,300); showmouseptr(); flag=0; outtextxy(35,457,LINE); } else if(button==1) { setcolor(13); hidemouseptr(); rectangle(7,280,27,300); showmouseptr(); flag=1; } break; case 6: if(pbn!=bn) { setcolor(RED); hidemouseptr(); rectangle(7,325,27,345); showmouseptr(); flag=0; outtextxy(35,457,PAINT); } else if(button==1) { setcolor(13); hidemouseptr(); rectangle(7,325,27,345); showmouseptr(); flag=1; } break; case 7: if(pbn!=bn) { setcolor(RED); hidemouseptr(); rectangle(7,370,27,390); 20

showmouseptr(); flag=0; outtextxy(35,457,POLYLINE); } else if(button==1) { setcolor(13); hidemouseptr(); rectangle(7,370,27,390); showmouseptr(); flag=1; } break; case 30: if(button==1) { flag=1; } else if(pbn!=bn) { clearline(); setcolor(4); outtextxy(35,457,NEW); flag=0; } break; case 31: if(button==1) { flag=1; } else if(pbn!=bn) { clearline(); setcolor(4); outtextxy(35,457,OPEN); flag=0; } break; case 32: if(button==1) { flag=1; } else if(pbn!=bn) { clearline(); setcolor(4); outtextxy(35,457,SAVE); flag=0; } break; case 33: if(button==1) { 21

flag=1; } else if(pbn!=bn) { clearline(); setcolor(4); outtextxy(35,457,ABOUT); flag=0; } break; case 34: if(button==1) { flag=1; } else if(pbn!=bn) { clearline(); setcolor(4); outtextxy(35,457,EXIT); flag=0; } break; case 0: clearline(); default: flag=1; } } setcolor(col); pbn=bn; if(flag==1) { flag=0; return (bn); } else return (0); } void freehand() { int px=1,py=1,flag=0,but; while(1) { getmousepos(); if(x>=35&&x<=600&&y>=40&&y<=450) { changemouseptr(pencil); if(px!=x||py!=y) printmousepos(); if(button==1) { if(flag==0) { hidemouseptr(); 22

flag=1; } else line(px,py,x,y); } else if(flag!=0) { showmouseptr(); flag=0; } px=x; py=y; } else { changemouseptr(hand); showmouseptr(); break; } } } void rect() { int ix,iy,fx,fy,px=1,py=1,but,flag=0,tx,ty,t=0; while(1) { getmousepos(); if(x>=35&&x<=600&&y>=40&&y<=450) { if(button==1 && t==0) { tx=x;ty=y; t=1; } changemouseptr(plus); if(px!=x||py!=y) printmousepos(); if(button==1&&flag==0) { ix=x;iy=y; flag=1; } else if(button==1 && flag==1 ) { fx=x;fy=y; hidemouseptr(); setcolor(fg); rectangle(tx,ty,fx,fy); setcolor(bg); rectangle(tx,ty,fx,fy); showmouseptr(); flag=0; } if(button!=1) { 23

setcolor(fg); rectangle(tx,ty,fx,fy); t=0; } } else { if(t==1) { setcolor(fg); rectangle(tx,ty,fx,fy); } t==0; changemouseptr(hand); break; } px=x; py=y; } } void circ() { int ix,iy,but,flag=0,p,q,a,b,p1,q1,a1,b1,px,py; while(1) { getmousepos(); if(x>=35&&x<=600&&y>=40&&y<=450) { changemouseptr(pencil); if(px!=x||py!=y) { printmousepos(); } if(button==1) { if(flag==0) { hidemouseptr(); ix=x;iy=y; p1=q1=a1=b1=0; flag=1; } else { p=(ix+x)/2; q=(iy+y)/2; a=abs(ix-p); b=abs(iy-q); if(p1!=p||q1!=q) { Ellipse1(p1,q1,a1,b1); Ellipse(p,q,a,b); p1=p;q1=q;a1=a;b1=b; } } 24

} else if(flag!=0) { showmouseptr(); flag=0; } px=x; py=y; } else { changemouseptr(hand); showmouseptr(); break; } } } void lin() { int ix,iy,px=1,py=1,but,flag=0; while(1) { getmousepos(); if(x>=35&&x<=600&&y>=40&&y<=450) { changemouseptr(pencil); if(px!=x||py!=y) { printmousepos(); } if(button==1) { if(flag==0) { hidemouseptr(); ix=px=x;iy=py=y; flag=1; } else { if(px!=x||py!=y) { bressline1(ix,iy,px,py); bressline(ix,iy,x,y); } } } else if(flag!=0) { showmouseptr(); flag=0; } px=x; py=y; } 25

else { changemouseptr(hand); break; } } } void mypoly() { int ix,iy,fx,fy,px=1,py=1,but,flag=0; while(1) { getmousepos(); if(x>=35&&x<=600&&y>=40&&y<=450) { changemouseptr(pencil); if(px!=x||py!=y) printmousepos(); if(button==1) { hidemouseptr(); lin(); showmouseptr(); ix=x; iy=y; } } else { changemouseptr(hand); break; } px=x; py=y; } } void paint() { int px=1,py=1,but; setfillstyle(SOLID_FILL,bg); while(1) { getmousepos(); if(x>=35&&x<=600&&y>=40&&y<=450) { changemouseptr(bottle); if(px!=x||py!=y) printmousepos(); if(button==1) { hidemouseptr(); floodfill(x,y,fg); showmouseptr(); } } 26

else { changemouseptr(hand); break; } px=x; py=y; } } void eraser() { int but,px,py,i,j; while(1) { getmousepos(); if(x>=36&&x<=583&&y>=41&&y<=433) { changemouseptr(rubber); if(px!=x||py!=y) printmousepos(); if(button==1) { hidemouseptr(); for(i=y;i<=y+16;i++) for(j=x;j<=x+16;j++) { putpixel(j,i,bg); } showmouseptr(); } } else { changemouseptr(hand); break; } px=x; py=y; } } void structure() { int i,j,row; setfillstyle(SOLID_FILL,8); setcolor(15); bar(0,0,640,480); // screen back ground settextstyle(SMALL_FONT,HORIZ_DIR,4); for(i=0,row=50;i<=15;i++,row+=25) { setfillstyle(SOLID_FILL,i); // color tool bars bar(610,row,630,row+20); rectangle(610,row,630,row+20); } setfillstyle(SOLID_FILL,8); // draw tool bars setcolor(15); 27

for(i=0,row=100;i<=6;i++,row+=45) { bar(7,row,27,row+20); rectangle(7,row,27,row+20); } setcolor(11); rectangle(9,104,25,116); // rectangle tool bar line(9,147,15,147); // pencil tool line(9,148,14,148); line(9,149,13,149); line(9,150,12,150); line(9,151,11,151); line(9,152,10,152); line(9,147,9,153); line(15,147,24,156); line(9,153,18,162); line(24,156,18,162); line(15,147,9,153); line(12,150,21,159); circle(17,200,8); // circle tool setcolor(11); // eraser tool setfillstyle(SOLID_FILL,11); bar3d(9,246,17,252,8,1); setcolor(11); line(10,283,24,297); // line tool rectangle( 9,333,25,343); // fill area tool arc(17,333,0,180,7); line(12,388,22,388); // polygon tool line(12,388,9,380); line(22,388,25,380); line(17,372,25,380); line(17,372,9,380); fg=15; // assign colors bg=0; setfillstyle(SOLID_FILL,bg); // bottom corner symbol bar(10,455,25,475); setcolor(15); rectangle(10,455,25,475); setfillstyle(SOLID_FILL,fg); bar(2,445,17,465); rectangle(2,445,17,465); setcolor(fg); rectangle(35,40,600,450); setfillstyle(SOLID_FILL,bg); floodfill(320,240,fg); // outtextxy(230,20,"NEW OPEN SAVE ABOUT EXIT"); setcolor(0); bar(220,20,260,35); bar(270,20,310,35); bar(320,20,360,35); bar(370,20,420,35); bar(427,20,465,35); setcolor(15); rectangle(220,20,260,35); rectangle(270,20,310,35); 28

rectangle(320,20,360,35); rectangle(370,20,420,35); rectangle(427,20,465,35); outtextxy(230,20,"NEW OPEN SAVE

ABOUT EXIT");

} void bressline ( int x1, int y1, int x2, int y2 ) { int incdec, t, i ; if ( x1 > x2 ) { t = x1 ; x1 = x2 ; x2 = t ; t = y1 ; y1 = y2 ; y2 = t ; } dx = x2 - x1 ; dy = y2 - y1 ; if ( dx == 0 ) /* vertical line */ { if ( y1 > y2 ) { t = y1 ; y1 = y2 ; y2 = t ; } for ( i = y1,j=0 ; i <= y2 ; i++,j++ ) { buffer[j]=getpixel (x1,i); putpixel ( x1, i,fg) ; } return ; } if ( dy == 0 ) /* horizontal line */ { for ( i = x1,j=0 ; i < x2 ; i++ ,j++) { buffer[j]=getpixel (i,y1); putpixel ( i, y1, fg ) ; } return ; } /* 0 < m < 1 */ if ( dy < dx && dy > 0 ) { e_noinc = 2 * dy ; e = 2 * dy - dx ; e_inc = 2 * ( dy - dx ) ; drawline ( x1, y1, x2, y2, PREDX, INCR ) ; } /* m = 1 */ if ( dy == dx && dy > 0 ) { e_noinc = 2 * dy ; e = 2 * dy - dx ; e_inc = 2 * ( dy - dx ) ; drawline ( x1, y1, x2, y2, PREDX, INCR ) ; } /* 1 < m < infinity */ if ( dy > dx && dy > 0 ) { 29

e_noinc = 2 * dx ; e = 2 * dx - dy ; e_inc = 2 * ( dx - dy ) ; drawline ( x1, y1, x2, y2, PREDY, INCR ) ; } /* 0 > m > -1 */ if ( -dy < dx && dy < 0 ) { dy = -dy ; e_noinc = 2 * dy ; e = 2 * dy - dx ; e_inc = 2 * ( dy - dx ) ; drawline ( x1, y1, x2, y2, PREDX, DECR ) ; } /* m = -1 */ if ( dy == -dx && dy < 0 ) { dy = -dy ; e_noinc = ( 2 * dy ) ; e = 2 * dy - dx ; e_inc = 2 * ( dy - dx ) ; drawline ( x1, y1, x2, y2, PREDX, DECR ) ; } /* -1 > m > 0 */ if ( -dy > dx && dy < 0 ) { dx = -dx ; e_noinc = - ( 2*dx ) ; e = 2 * dx - dy ; e_inc = - 2 * ( dx - dy ) ; drawline ( x2, y2, x1, y1, PREDY, DECR ) ; } } void bressline1( int x1, int y1, int x2, int y2 ) { int incdec, t, i ; if ( x1 > x2 ) { t = x1 ; x1 = x2 ; x2 = t ; t = y1 ; y1 = y2 ; y2 = t ; } dx = x2 - x1 ; dy = y2 - y1 ; if ( dx == 0 ) /* vertical line */ { if ( y1 > y2 ) { t = y1 ; y1 = y2 ; y2 = t ; } for ( i = y1,j=0 ; i <= y2 ; i++,j++ ) { putpixel ( x1, i, buffer[j] ) ; } return ; } if ( dy == 0 ) /* horizontal line */ 30

{ for ( i = x1,j=0 ; i < x2 ; i++ ,j++) { putpixel ( i, y1,buffer[j] ) ; } return ; } /* 0 < m < 1 */ if ( dy < dx && dy > 0 ) { e_noinc = 2 * dy ; e = 2 * dy - dx ; e_inc = 2 * ( dy - dx ) ; drawline1( x1, y1, x2, y2, PREDX, INCR ) ; } /* m = 1 */ if ( dy == dx && dy > 0 ) { e_noinc = 2 * dy ; e = 2 * dy - dx ; e_inc = 2 * ( dy - dx ) ; drawline1( x1, y1, x2, y2, PREDX, INCR ) ; } /* 1 < m < infinity */ if ( dy > dx && dy > 0 ) { e_noinc = 2 * dx ; e = 2 * dx - dy ; e_inc = 2 * ( dx - dy ) ; drawline1 ( x1, y1, x2, y2, PREDY, INCR ) ; } /* 0 > m > -1 */ if ( -dy < dx && dy < 0 ) { dy = -dy ; e_noinc = 2 * dy ; e = 2 * dy - dx ; e_inc = 2 * ( dy - dx ) ; drawline1 ( x1, y1, x2, y2, PREDX, DECR ) ; } /* m = -1 */ if ( dy == -dx && dy < 0 ) { dy = -dy ; e_noinc = ( 2 * dy ) ; e = 2 * dy - dx ; e_inc = 2 * ( dy - dx ) ; drawline1( x1, y1, x2, y2, PREDX, DECR ) ; } /* -1 > m > 0 */ if ( -dy > dx && dy < 0 ) { dx = -dx ; e_noinc = - ( 2*dx ) ; e = 2 * dx - dy ; e_inc = - 2 * ( dx - dy ) ; 31

drawline1( x2, y2, x1, y1, PREDY, DECR ) ; } } void drawline ( int x1, int y1, int x2, int y2, int pred, int incdec ) { int i, start, end, var ; if ( pred == PREDX ) { start = x1 ; end = x2 ; var = y1 ;} else { start = y1 ; end = y2 ; var = x1 ;} for ( i = start,j=0 ; i <= end ; i++,j++ ) { if ( pred == PREDY ) { buffer[j]=getpixel (var,i); putpixel ( var, i, fg ) ; } else { buffer[j]=getpixel (i,var); putpixel ( i, var, fg ) ; } if ( e < 0 ) e += e_noinc ; else { var += incdec ; e += e_inc ; } } } void drawline1 ( int x1, int y1, int x2, int y2, int pred, int incdec ) { int i, start, end, var ; if ( pred == PREDX ) { start = x1 ; end = x2 ; var = y1 ; } else { start = y1 ; end = y2 ; var = x1 ; } for ( i = start,j=0 ; i <= end ; i++,j++ ) { if ( pred == PREDY ) { putpixel ( var, i,buffer[j]) ; } else { putpixel ( i, var, buffer[j] ) ; } if ( e < 0 ) e += e_noinc ; else 32

{ var += incdec ; e += e_inc ; } } } void Ellipse(int xc,int yc,int a0,int b0) { int x = 0; int y = b0; long a= a0; long b = b0; long Asquared = a*a; long TwoAsquared = 2 * Asquared; long Bsquared = b*b; long TwoBsquared = 2 * Bsquared; long d; long dx,dy; d = Bsquared - Asquared*b + Asquared/4; dx = 0; dy = TwoAsquared *b; j=0; while (dx < dy) { buffer[j]=getpixel(xc + x, yc + y);j++; buffer[j]=getpixel(xc - x, yc + y);j++; buffer[j]=getpixel(xc + x, yc - y);j++; buffer[j]=getpixel(xc - x, yc - y);j++; putpixel(xc + x, yc + y, fg); putpixel(xc - x, yc + y, fg); putpixel(xc + x, yc - y, fg); putpixel(xc - x, yc - y, fg); if (d > 0) { --y; dy -= TwoAsquared; d-=dy; } ++x; dx+=TwoBsquared; d += Bsquared + dx; } d += (3 *(Asquared-Bsquared)/2 -(dx+dy)) / 2; while (y >=0) { buffer[j]=getpixel(xc + x, yc + y);j++; buffer[j]=getpixel(xc - x, yc + y);j++; buffer[j]=getpixel(xc + x, yc - y);j++; buffer[j]=getpixel(xc - x, yc - y);j++; putpixel(xc + x, yc + y, fg); putpixel(xc - x, yc + y, fg); putpixel(xc + x, yc - y, fg); putpixel(xc - x, yc - y, fg); if (d < 0) { ++x; 33

dx+= TwoBsquared; d+= dx; } --y; dy -= TwoAsquared; d+= Asquared - dy; } } void Ellipse1(int xc,int yc,int a0,int b0) { int x = 0; int y = b0; long a= a0; long b = b0; long Asquared = a*a; long TwoAsquared = 2 * Asquared; long Bsquared = b*b; long TwoBsquared = 2 * Bsquared; long d; long dx,dy; d = Bsquared - Asquared*b + Asquared/4; dx = 0; dy = TwoAsquared *b; j=0; while (dx < dy) { putpixel(xc + x, yc + y,buffer[j]);j++; putpixel(xc - x, yc + y,buffer[j]);j++; putpixel(xc + x, yc - y,buffer[j]);j++; putpixel(xc - x, yc - y,buffer[j]);j++; if (d > 0) { --y; dy -= TwoAsquared; d-=dy; } ++x; dx+=TwoBsquared; d += Bsquared + dx; } d += (3 *(Asquared-Bsquared)/2 -(dx+dy)) / 2; while (y >=0) { putpixel(xc + x, yc + y,buffer[j]);j++; putpixel(xc - x, yc + y,buffer[j]);j++; putpixel(xc + x, yc - y,buffer[j]);j++; putpixel(xc - x, yc - y,buffer[j]);j++; if (d < 0) { ++x; dx+= TwoBsquared; d+= dx; } --y; dy -= TwoAsquared; d+= Asquared - dy; 34

} } void New() { hidemouseptr(); structure(); showmouseptr(); } void Open() { int i; char st[13]; setcolor(15); hidemouseptr(); save(220,200,420,280,"c:/temp.tmp"); setfillstyle(SOLID_FILL,15); bar(220,200,420,280); setfillstyle(SOLID_FILL,8); bar(221,201,419,216); settextstyle(SMALL_FONT,HORIZ_DIR,5); outtextxy(222,201," OPEN"); settextstyle(SMALL_FONT,HORIZ_DIR,4); setcolor(8); outtextxy(222,230," Enter the File Name"); w=textwidth("a"); h=textheight("a"); bar(221,263,419,279); setcolor(15); outtextxy(320-(3*w),265,"< OK >"); setcolor(8); for(i=0;st[i-1]!='\n';i++) { st[i]=getch(); if(st[i]==13||i>11) { st[i]='\0'; break; } else if(st[i]==27) { goto Down; } st[i+1]='\0'; outtextxy(320-(6*w),245,st); } if(restore(36,41,599,449,st)==0) { setfillstyle(SOLID_FILL,15); bar(220,200,420,280); setfillstyle(SOLID_FILL,8); bar(221,201,419,216); settextstyle(SMALL_FONT,HORIZ_DIR,5); setcolor(15); outtextxy(222,201," ERROR!"); settextstyle(SMALL_FONT,HORIZ_DIR,4); 35

setcolor(8); outtextxy(265,239,"Incorrect File Name!"); bar(221,263,419,279); setcolor(15); getch(); Down: restore(220,200,420,280,"c:/temp.tmp"); } remove("c:/temp.tmp"); showmouseptr(); setcolor(fg); } void About() { int i,w,col; char st[13]; setcolor(15); hidemouseptr(); save(200,195,435,285,"c:/temp.tmp"); setcolor(8); for(i=0;i<180;i++) { circle(320,240,i); delay(5); } setcolor(15); outtextxy(200,300,"PAINT APPLICATION"); flushall(); getch(); setfillstyle(1,16); // restore(100,100,500,400,"c:/temp.tmp"); bar(50,50,500,430); remove("c:/temp.tmp"); showmouseptr(); setcolor(fg); } void Save() { int i,flag=0; char st[13]; setcolor(15); hidemouseptr(); save(220,200,420,280,"c:/temp.tmp"); setfillstyle(SOLID_FILL,15); bar(220,200,420,280); setfillstyle(SOLID_FILL,8); bar(221,201,419,216); settextstyle(SMALL_FONT,HORIZ_DIR,5); outtextxy(222,201," SAVE"); settextstyle(SMALL_FONT,HORIZ_DIR,4); setcolor(8); outtextxy(222,230," Enter the File Name"); w=textwidth("a"); h=textheight("a"); bar(221,263,419,279); 36

setcolor(15); outtextxy(320-(3*w),265,"< OK >"); setcolor(8); for(i=0;st[i-1]!='\n';i++) { st[i]=getch(); if(st[i]==13||i>11) { st[i]='\0'; flag=1; break; } void save(int x1,int y1,int x2,int y2,char name[13]) { int i,j; char ch; FILE *fp; x=x2; y=y2; fp=fopen(name,"wb"); if(fp!=NULL) { for(i=x1;i<=x2;i++) for(j=y1;j<=y2;j++) { ch=getpixel(i,j); fputc(ch,fp); } fclose(fp); } } int restore(int x1,int y1,int x2,int y2, char name[13]) { char ch; int i,j; FILE *fp; fp=fopen(name,"rb"); if(fp!='\0') { for(i=x1;i<=x2;i++) for(j=y1;j<=y2;j++) { ch=fgetc(fp); putpixel(i,j,ch); } fclose(fp); return 1; } else return 0; } void clearline() { int i,j; setcolor(8); 37

// for(i=457;i<=470;i++) // line(35,i,450,i); for(i=35;i<=450;i++) for(j=457;j<=470;j++) putpixel(i,j,8); }

OUTPUT:

RESULT:

Thus the above code was implemented and executed successfully and the output was shown.

38

PRESS ME GAME AIM: To write a C graphics program to create a Press me game. PROCEDURE: Start the program by importing the header files such as graphics.h and conio.h. Initialize the graph. Use the union to initialize the functions. Draw the basic shapes by using the different functions such as rectangle(), bar(). Use the functions such as showmouseptr(), hidemouseptr(), getmousepos() to initalize and execute the code. Use the setcolor() to differentiate the different shapes. Fill the shapes by using different styles such as HATCH_FILL, SOLID_FILL and so on. In the main() inialize all the functions and the make the respective codes to execute. Close the graph and stop the program. SOURCE CODE:
#include <stdio.h> #include <conio.h> #include <dos.h> #include <graphics.h> #include <stdlib.h> union REGS i, o; int left = 265, top = 250; void initialize_graphics_mode() { int gd = DETECT, gm, error; initgraph(&gd,&gm,"C:\\TC\\BGI"); error = graphresult(); if (error != grOk) { perror("Error "); printf("Press any key to exit...\n"); getch(); exit(EXIT_FAILURE); } } void showmouseptr() { i.x.ax = 1; int86(0x33,&i,&o); } void hidemouseptr() { i.x.ax = 2; int86(0x33,&i,&o); } void getmousepos(int *x,int *y) 39

{ i.x.ax = 3; int86(0x33,&i,&o); *x = o.x.cx; *y = o.x.dx; } void draw_bar() { hidemouseptr(); setfillstyle(SOLID_FILL,DARKGRAY); bar(190,180,450,350); showmouseptr(); } void draw_button(int x, int y) { hidemouseptr(); setfillstyle(SOLID_FILL,LIGHTMAGENTA); bar(x,y,x+100,y+30); moveto(x+5,y); setcolor(YELLOW); outtext("Press me"); showmouseptr(); } void draw() { settextstyle(SANS_SERIF_FONT,HORIZ_DIR,2); outtextxy(155,451,"press me"); setcolor(BLUE); rectangle(0,0,639,450); setcolor(RED); outtextxy(160,25,"Try to press the \"Press me\" button"); outtextxy(210,50,"Press escape key to exit"); setfillstyle(XHATCH_FILL,CYAN); setcolor(BLUE); bar(1,1,75,449); bar(565,1,638,449); showmouseptr(); draw_bar(); draw_button(left,top); } void initialize() { initialize_graphics_mode(); if( !initmouse() ) { closegraph(); printf("Unable to initialize the mouse"); printf("Press any key to exit...\n"); getch(); exit(EXIT_SUCCESS); } draw(); } int initmouse() { 40

i.x.ax = 0; int86(0x33,&i,&o); return ( o.x.ax ); } void get_input() { int x, y; while(1) { getmousepos(&x,&y); /* mouse pointer in left of button */ if( x >= (left-3) && y >= (top-3) && y <= (top+30+3) && x < left ) { draw_bar(); left = left + 4; if (left > 350) left = 190; draw_button(left,top); } /* mouse pointer in right of button */ else if (x<=(left+100+3)&&y>=(top-3)&&y<=(top+30+3)&&x>(left+100)) { draw_bar(); left = left - 4; if (left < 190) left = 350; draw_button(left,top); } /* mouse pointer above button */ else if(x>(left-3) && y>=(top-3) && y<(top) && x<= (left+100+3)) { draw_bar(); top = top + 4; if (top > 320) top = 180; draw_button(left,top); } /* mouse pointer below button */ else if (x>(left-3)&&y>(top+30)&&y<=(top+30+3)&&x<=(left+100+3)) { draw_bar(); top = top - 4; if (top < 180) top = 320; draw_button(left,top); } if (kbhit()) { if (getkey() == 1) exit(EXIT_SUCCESS); } } } int getkey() { 41

i.h.ah = 0; int86(22,&i,&o); return( o.h.ah ); } main() { initialize(); get_input(); return 0; }

OUTPUT:

RESULT:

Thus the above code was implemented and executed successfully and the output was shown.

42

DDA LINE ALGORITHM

AIM: To write a C graphics program to draw a Line using DDA Line Algorithm. ALGORITHM: The DDA is a scan conversion line algorithm based on calculating either dy or dx. A line is sampled at unit intervals in one coordinate and corresponding integer values nearest the line path are determined for other coordinates. Considering a line with positive slope, if the slope is less than or equal to 1, we sample at unit x intervals (dx=1) and compute successive y values as yk+1 = yk+m. Subscript k takes integer values starting from 0, for the 1st point and increases by until endpoint is reached. Y value is rounded off to nearest integer to correspond to a screen pixel. For lines with slope greater than 1, we reverse the role of x and y i.e. we sample at dy=1 and calculate consecutive x values as xk+1=xk+1/m. Similar calculations are carried out to determine pixel positions along a line with negative slope. Thus, if the absolute value of the slope is less than 1, we set dx=1 if xstart<xend i.e. the starting extreme point is at the left. SOURCE CODE: #include <stdio.h> #include <dos.h> #include <graphics.h> void lineDDA(int, int, int, int); void main() { int x1, y1, xn, yn; int gd = DETECT, gm; initgraph(&gd, &gm, "C:\\TC\\BGI"); printf("Enter the starting coordinates of line: "); scanf("%d %d", &x1, &y1); printf("Enter the ending coordinates of line: "); scanf("%d %d", &xn, &yn); lineDDA(x1, y1, xn, yn); getch(); } void lineDDA(int x1, int y1, int xn, int yn) { int dx, dy, m, i; m = (yn-y1)/(xn-x1); for (i=x1; i<=xn; i++) { if (m <= 1)
43

{ dx = 1; dy = m * dx; } else { dy = 1; dx = dy / m; } x1 = x1 + dx; y1 = y1 + dy; putpixel(x1, y1, RED); delay(20); }}

OUTPUT:

RESULT: Thus the above code was implemented and executed successfully and the output was shown.

44

BRESENHAMS LINE ALGORITHM AIM: To write a C graphics program to draw a Line using Bresenham's Line Algorithm. ALGORITHM: An accurate, efficient raster line drawing algorithm converts lines using only incremental integer calculations that can be adapted to display circles and other curves. Input the two line endpoints and store left endpoint as (x0, y0). Pre-calculate the values dx, dy, 2dy and 2dy - 2dx. Color pixel (x0, y0). Let p0 = 2dy dx. At each xk along the line, starting with k=0: If pk<0, then the next point to plot is (xk+1, yk), and pk+1 = pk + 2dy. Otherwise, the next point to plot is (xk + 1, yk + 1), and pk+1 = pk + 2dy 2dx. Repeat Step-4 dx times. SOURCE CODE: #include <stdio.h> #include <dos.h> #include <graphics.h> void lineBres(int, int, int, int); void main() { int x1, y1, xn, yn; int gd = DETECT, gm; initgraph(&gd, &gm, "C:\\TC\\BGI"); printf("Enter starting coordinates of line: "); scanf("%d %d", &x1, &y1); printf("Enter ending coordinates of line: "); scanf("%d %d", &xn, &yn); lineBres(x1, y1, xn, yn); getch(); } void lineBres(int x1, int y1, int xn, int yn) { int dx = xn - x1, dy = yn - y1; int di = 2 * dy - dx; int ds = 2 * dy, dt = 2 * (dy - dx); putpixel(x1, y1, RED); while (x1 < xn) { x1++; if (di < 0) di = di + ds; else {
45

y1++; di = di + dt; } putpixel(x1, y1, RED); delay(20); } }

OUTPUT:

RESULT: Thus the above code was implemented and executed successfully and the output was shown.

46

MID-POINT CIRCLE ALGORITHM


AIM: To write a C graphics program to draw a Circle using mid - Point Circle Algorithm. ALGORITHM:

Input radius r and circle centre (xc, yc), then set the coordinates for the first point on the circumference of a circle centred on the origin as:
( x0 , y0 ) (0, r )
Calculate the initial value of the decision parameter as:
p0 5 4 r

1. Starting with k = 0 at each position xk, perform the following test. If pk< 0, the next point along the circle centred on (0, 0) is (xk+1, yk) and:
p k 1 p k 2 x k 1 1
Otherwise the next point along the circle is (xk+1, yk-1) and:

pk 1 pk 2 xk 1 1 2 yk 1

2. Determine symmetry points in the other seven octants 3. Move each calculated pixel position (x, y) onto the circular path centred at (xc, yc) to plot the coordinate values:
x x xc
y y yc

4. Repeat steps 3 to 5 until x >= y

SOURCE CODE:
#include <stdio.h> #include <dos.h> #include <graphics.h> void circleMidpoint(int, int, int); void drawCircle(int, int, int, int); void main() { int xc, yc, r; int gd = DETECT, gm; initgraph(&gd, &gm, "c:\\tc\\bgi"); printf("Enter center coordinates of circle: "); scanf("%d %d", &xc, &yc); printf("Enter radius of circle: "); scanf("%d", &r); 47

circleMidpoint(xc, yc, r); getch(); } void circleMidpoint(int xc, int yc, int r) { int x = 0, y = r; int p = 1 - r; while (x < y) { drawCircle(xc, yc, x, y); x++; if (p < 0) p = p + 2 * x + 1; else { y--; p = p + 2 * (x - y) + 1; } drawCircle(xc, yc, x, y); delay(50); } } void drawCircle(int xc, int yc, int x, int y) { putpixel(xc+x, yc+y, RED ); putpixel(xc-x, yc+y, BLUE); putpixel(xc+x, yc-y, GREEN); putpixel(xc-x, yc-y, CYAN); putpixel(xc+y, yc+x, LIGHTMAGENTA); putpixel(xc-y, yc+x, YELLOW); putpixel(xc+y, yc-x, WHITE); putpixel(xc-y, yc-x, DARKGRAY); }

OUTPUT:

RESULT: Thus the above code was implemented and executed successfully and the output was shown.
48

MID-POINT ECLIPSE ALGORITHM AIM: To write a C graphics program to draw an Ellipse using mid - Point Ellipse Algorithm. ALGORITHM: Input rx, ry, and ellipse center (xc, yc), and obtain the first point on an ellipse centered on the origin as(x0, y0) = (0, ry) 1. Calculate the initial parameter in region 1 as

p10 ry2 rx2ry 1 rx2 4


2. At each xi position, starting at i = 0, if p1i< 0, the next point along the ellipse centered on (0, 0) is (xi + 1, yi) and

p1i 1 p1i 2ry2 xi 1 ry2


otherwise, the next point is (xi + 1, yi 1) and

p1i 1 p1i 2ry2 xi 1 2rx2 yi 1 ry2


and continue until

2ry2 x 2rx2 y
3. (x0, y0) is the last position calculated in region 1. Calculate the initial parameter in region 2 as

p20 ry2 ( x0 1 )2 rx2 ( y0 1)2 rx2ry2 2


At each yi position, starting at i = 0, if p2i> 0, the next point along the ellipse centered on (0, 0) is (xi, yi 1) and

p 2i 1 p 2i 2rx2 yi 1 rx2

otherwise, the next point is (xi + 1, yi 1) and

p2i 1 p2i 2ry2 xi 1 2rx2 yi 1 rx2


Use the same incremental calculations as in region 1. Continue until y = 0. 4. For both regions determine symmetry points in the other three quadrants. 5. Move each calculated pixel position (x, y) onto the elliptical path centered on (xc, yc) and plot the coordinate values. x = x + xc ,y = y + yc

49

SOURCE CODE:
#include <stdio.h> #include <dos.h> #include <graphics.h> void ellipseMidpoint(float, float, float, float); void drawEllipse(float, float, float, float); void main() { float xc, yc, rx, ry; int gd = DETECT, gm; initgraph(&gd, &gm, "c:\\tc\\bgi"); printf("\nEnter the center coordinates of ellipse: "); scanf("%f %f", &xc, &yc); printf("\nEnter x-radius coordinate: "); scanf("%f", &rx); printf("\nEnter y-radius coordiante: "); scanf("%f", &ry); ellipseMidpoint(xc, yc, rx, ry); getch(); } void ellipseMidpoint(float xc, float yc, float rx, float ry) { float rxSq = rx * rx; float rySq = ry * ry; float x = 0, y = ry, p; float px = 0, py = 2 * rxSq * y; drawEllipse(xc, yc, x, y); //Region 1 p = rySq - (rxSq * ry) + (0.25 * rxSq); while (px < py) { x++; px = px + 2 * rySq; if (p < 0) p = p + rySq + px; else { y--; py = py - 2 * rxSq; p = p + rySq + px - py; } drawEllipse(xc, yc, x, y); delay(30); } //Region 2 p = rySq*(x+0.5)*(x+0.5) + rxSq*(y-1)*(y-1) - rxSq*rySq; while (y > 0) { y--; py = py - 2 * rxSq; 50

if (p > 0) p = p + rxSq - py; else { x++; px = px + 2 * rySq; p = p + rxSq - py + px; } drawEllipse(xc, yc, x, y); delay(30); } } void drawEllipse(float xc, float yc, float x, float y) { putpixel(xc+x, yc+y, CYAN); putpixel(xc-x, yc+y, BLUE); putpixel(xc+x, yc-y, DARKGRAY); putpixel(xc-x, yc-y, RED); }

OUTPUT:

RESULT: Thus the above code was implemented and executed successfully and the output was shown.

51

LINE CLIPPING AIM: To write a C graphics program for Line Clipping. ALGORITHM: Start the program by importing the header files such as graphics.h and conio.h. Initialize the graph. Given a set of 2D lines or polygons and a window, clip the lines or polygons to their regions that are inside the window. For clipping a point(x,y)If(xmin<x<xmax)&&( (ymin<y<ymax) then(x,y) is visible. For lines -if both endpoints are in,do trivial acceptance -if one endpoint in, one endpoint out, must clip -if both endpoints out Could be out Could be clipped Close the graph and stop the program. SOURCE CODE: #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;
52

} 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,"LINE AFTER CLIPPING"); rectangle(xmin,ymin,xmax,ymax); } outcode CompOutCode(float x,float y)
53

{ 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 ; printf("\nEnter the endpoints of line\n"); scanf("%f%f%f%f",&x1,&y1,&x2,&y2); printf("Enter the rectangular coordinates of clipping window\n"); scanf("%f%f%f%f",&xmin,&ymin,&xmax,&ymax); initgraph(&gdriver, &gmode, "c:\\tc\\bgi"); outtextxy(200,20,"LINE BEFORE CLIPPING"); line(x1,y1,x2,y2); rectangle(xmin,ymin,xmax,ymax); getch( ); cleardevice( ); clip(x1,y1,x2,y2); getch( ); restorecrtmode( );} OUTPUT:

54

RESULT: Thus the above code was implemented and executed successfully and the output was shown.

55

TEXT ANIMATION AIM: To write a C graphics program for Text Animation. PROCEDURE: Start the program by importing the header files such as graphics.h and conio.h. Initialize the graph. Enter the text to be animated. Enter the co ordinates to display the text Display the outtext. Close the graph and stop the program. SOURCE CODE: #include<stdio.h> #include<math.h> #include<conio.h> #include<graphics.h> #define round(val) (int)(val+0.5) void main() { int gd=DETECT,gm,sx,sy,tx,ty; char text[50]; void move(int,int,int,int,char[]); printf("Enter the text:"); scanf("%s",text); printf("Enter the initial points:"); scanf("%d%d",&sx,&sy); printf("Enter the TARGET points:"); scanf("%d%d",&tx,&ty); initgraph(&gd,&gm,"c:\\tc\\bgi"); outtextxy(sx,sy,text); move(sx,sy,tx,ty,text); getch(); closegraph(); } void move(int sx,int sy,int tx,int ty,char text[50]) { int dx=tx-sx,dy=ty-sy,steps,k; float xin,yin,x=sx,y=sy; getch(); if(abs(dx)>abs(dy)) steps=abs(dy); else steps=abs(dy); xin=dx/(float)steps; yin=dy/(float)steps;
56

for(k=0;k<steps;k++) { cleardevice(); x+=xin; y+=yin; setcolor(15); outtextxy(round(x),round(y),text); delay(50); } } OUTPUT:

RESULT: Thus the above code was implemented and executed successfully and the output was shown.

57

2D SCALING AIM: To write a C graphics program for 2D Scaling. ALGORITHM: Start the program by importing the header files such as graphics.h and conio.h. Initialize the graph. Scaling is the concept of increasing (or decreasing) the size of a picture. (In one or in either directions. When it is done in both directions, the increase or decrease in both directions need not be same) To change the size of the picture, we increase or decreases the distance between the end points of the picture and also change the intermediate points are per requirements. Suppose we want the point (x1 y1) to be scaled by a factor sx and by a factor sy along y direction.Then the new coordinates become : x2 = x1 * sx and y2 = y1 * sy. Enter the co-ordinates as well as the scaling co-ordinates. The result is displayed. SOURCE CODE:
#include<stdio.h> #include<conio.h> #include<graphics.h> #include<process.h> #include<math.h> int x1,y1,x2,y2,x3,y3,mx,my; void draw(); void scale(); void main() { int gd=DETECT,gm; int c; initgraph(&gd,&gm,"c:\\tc\\bgi "); printf("Enter the 1st point for the triangle:"); scanf("%d%d",&x1,&y1); printf("Enter the 2nd point for the triangle:"); scanf("%d%d",&x2,&y2); printf("Enter the 3rd point for the triangle:"); scanf("%d%d",&x3,&y3); draw(); scale(); } void draw() { line(x1,y1,x2,y2); line(x2,y2,x3,y3); line(x3,y3,x1,y1); } void scale() { int x,y,a1,a2,a3,b1,b2,b3; 58

int mx,my; printf("Enter the scalling coordinates"); scanf("%d%d",&x,&y); mx=(x1+x2+x3)/3; my=(y1+y2+y3)/3; cleardevice(); a1=mx+(x1-mx)*x; b1=my+(y1-my)*y; a2=mx+(x2-mx)*x; b2=my+(y2-my)*y; a3=mx+(x3-mx)*x; b3=my+(y3-my)*y; line(a1,b1,a2,b2); line(a2,b2,a3,b3); line(a3,b3,a1,b1); draw(); getch(); }

OUTPUT:

59

RESULT: Thus the above code was implemented and executed successfully and the output was shown.

60

2D ROTATION AIM: To write a C graphics program for 2D Rotation. ALGORITHM: Start the program by importing the header files such as graphics.h and conio.h. Initialize the graph. It is to rotate a point about an axis. The axis can be any of the coordinates or simply any other specified line also. Suppose we want to rotate a point (x1 y1) clockwise through an angle about the origin of the coordinate system. Then mathematically we can show that x2 = x1cos + y1sinand y2 = x1sin - y1cos Enter the co-ordinates and the angle to be rotated. The result is displayed. SOURCE CODE:
#include<stdio.h> #include<conio.h> #include<graphics.h> #include<process.h> #include<math.h> void TriAngle(int x1,int y1,int x2,int y2,int x3,int y3); void Rotate(int x1,int y1,int x2,int y2,int x3,int y3); void main() { int gd=DETECT,gm; int x1,y1,x2,y2,x3,y3; initgraph(&gd,&gm,"c:\\tc\\bgi "); printf("Enter the 1st point for the triangle:"); scanf("%d%d",&x1,&y1); printf("Enter the 2nd point for the triangle:"); scanf("%d%d",&x2,&y2); printf("Enter the 3rd point for the triangle:"); scanf("%d%d",&x3,&y3); TriAngle(x1,y1,x2,y2,x3,y3); getch(); cleardevice(); Rotate(x1,y1,x2,y2,x3,y3); setcolor(1); TriAngle(x1,y1,x2,y2,x3,y3); getch(); } void TriAngle(int x1,int y1,int x2,int y2,int x3,int y3) { line(x1,y1,x2,y2); line(x2,y2,x3,y3); line(x3,y3,x1,y1); 61

} void Rotate(int x1,int y1,int x2,int y2,int x3,int y3) { int x,y,a1,b1,a2,b2,a3,b3,p=x2,q=y2; float Angle; printf("Enter the angle for rotation:"); scanf("%f",&Angle); cleardevice(); Angle=(Angle*3.14)/180; a1=p+(x1-p)*cos(Angle)-(y1-q)*sin(Angle); b1=q+(x1-p)*sin(Angle)+(y1-q)*cos(Angle); a2=p+(x2-p)*cos(Angle)-(y2-q)*sin(Angle); b2=q+(x2-p)*sin(Angle)+(y2-q)*cos(Angle); a3=p+(x3-p)*cos(Angle)-(y3-q)*sin(Angle); b3=q+(x3-p)*sin(Angle)+(y3-q)*cos(Angle); printf("Rotate"); TriAngle(a1,b1,a2,b2,a3,b3); }

OUTPUT:

62

RESULT:

Thus the above code was implemented and executed successfully and the output was shown.

63

2D TRANSLATION TRIANGLE AIM: To write a C graphics program for 2D Translation Triangle. ALGORITHM: Start the program by importing the header files such as graphics.h and conio.h. Initialize the graph. Translation refers to the shifting of a point to some other place, whose distance with regard to the present point is known. Consider a point P(x1, y1) to be translated to another point Q(x2, y2). If we know the point value (x2, y2) we can directly shift to Q by displaying the pixel (x2, y2). On the other hand, suppose we only know that we want to shift by a distance of Tx along xaxis and Ty along Y axis. Then obviously the coordinates can be derived by x2 =x1 +Tx and Y2 = y1+ Ty. Close the graph and stop the program. SOURCE CODE:
#include<stdio.h> #include<conio.h> #include<graphics.h> #include<process.h> #include<math.h> int x1,y1,x2,y2,x3,y3,mx,my; void draw(); void tri(); void main() { int gd=DETECT,gm; int c; initgraph(&gd,&gm,"c:\\tc\\bgi "); printf("Enter the 1st point for the triangle:"); scanf("%d%d",&x1,&y1); printf("Enter the 2nd point for the triangle:"); scanf("%d%d",&x2,&y2); printf("Enter the 3rd point for the triangle:"); scanf("%d%d",&x3,&y3); cleardevice(); draw(); getch(); tri(); getch(); } void draw() { line(x1,y1,x2,y2); line(x2,y2,x3,y3); line(x3,y3,x1,y1); } 64

void tri() { int x,y,a1,a2,a3,b1,b2,b3; printf("Enter the Transaction coordinates"); scanf("%d%d",&x,&y); cleardevice(); a1=x1+x; b1=y1+y; a2=x2+x; b2=y2+y; a3=x3+x; b3=y3+y; line(a1,b1,a2,b2); line(a2,b2,a3,b3); line(a3,b3,a1,b1); }

OUTPUT:

65

RESULT: Thus the above code was implemented and executed successfully and the output was shown.

66

2D TRANSLATION RECTANGLE AIM: To write a C graphics program for 2D Translation Rectangle. ALGORITHM: Start the program by importing the header files such as graphics.h and conio.h. Initialize the graph. Translation refers to the shifting of a point to some other place, whose distance with regard to the present point is known. Consider a point P(x1, y1) to be translated to another point Q(x2, y2). If we know the point value (x2, y2) we can directly shift to Q by displaying the pixel (x2, y2). On the other hand, suppose we only know that we want to shift by a distance of Tx along x axis and Ty along Y axis. Then obviously the coordinates can be derived by x2 =x1 +Tx and Y2 = y1+ Ty. Close the graph and stop the program. SOURCE CODE:
#include<stdio.h> #include<conio.h> #include<graphics.h> #include<process.h> #include<math.h> void RectAngle(int x,int y,int Height,int Width); void Translate(int x,int y,int Height,int Width); void main() { int gd=DETECT,gm; int x,y,Height,Width; initgraph(&gd,&gm,"c:\\tc\\bgi "); printf("Enter the First point for the Rectangle:"); scanf("%d%d",&x,&y); printf("Enter the Height&Width for the Rectangle:"); scanf("%d%d",&Height,&Width); RectAngle(x,y,Height,Width); getch(); cleardevice(); Translate(x,y,Height,Width); RectAngle(x,y,Height,Width); getch(); } void RectAngle(int x,int y,int Height,int Width) { line(x,y,x+Width,y); line(x,y,x,y+Height); line(x+Width,y,x+Width,y+Height); line(x,y+Height,x+Width,y+Height); } void Translate(int x,int y,int Height,int Width) 67

{ int Newx,Newy,a,b; printf("Enter the Transaction coordinates"); scanf("%d%d",&Newx,&Newy); cleardevice(); a=x+Newx; b=y+Newy; RectAngle(a,b,Height,Width); }

OUTPUT:

68

RESULT:

Thus the above code was implemented and executed successfully and the output was shown.

69

3D SCALING AIM: To write a C graphics program for 3D Scaling. ALGORITHM: Start the program by importing the header files such as graphics.h and conio.h. Initialize the graph. Scaling is the concept of increasing (or decreasing) the size of a picture. (In one or in either directions. When it is done in both directions, the increase or decrease in both directions need not be same) To change the size of the picture, we increase or decreases the distance between the end points of the picture and also change the intermediate points are per requirements. Suppose we want the point (x1 y1) to be scaled by a factor sx and by a factor sy along y direction. Then the new coordinates become: x2 = x1 * sx and y2 = y1 * sy. Enter the co-ordinates as well as the scaling co-ordinates. The result is displayed. SOURCE CODE:
#include<stdio.h> #include<conio.h> #include<math.h> #include<process.h> #include<graphics.h> int x1,x2,y1,y2,mx,my,depth; void draw(); void scale(); void main() { int gd=DETECT,gm,c; initgraph(&gd,&gm,"c:\\tc\\bgi"); printf("\n\t\t3D Transformation Scalling\n\n"); printf("\nEnter 1st top value(x1,y1):"); scanf("%d%d",&x1,&y1); printf("Enter right bottom value(x2,y2):"); scanf("%d%d",&x2,&y2); depth=(x2-x1)/4; mx=(x1+x2)/2; my=(y1+y2)/2; draw(); getch(); cleardevice(); scale(); getch(); } void draw() { bar3d(x1,y1,x2,y2,depth,1); } 70

void scale() { int x,y,a1,a2,b1,b2,dep; printf("\n\n Enter scalling co-ordinates:"); scanf("%d%d",&x,&y); a1=mx+(x1-mx)*x; a2=mx+(x2-mx)*x; b1=my+(y1-my)*y; b2=my+(y2-my)*y; dep=(a2-a1)/4; bar3d(a1,b1,a2,b2,dep,1); setcolor(5); draw(); }

OUTPUT:

71

RESULT: Thus the above code was implemented and executed successfully and the output was shown.

72

3D ROTATION AIM: To write a C graphics program for 3D Rotation. ALGORITHM: Start the program by importing the header files such as graphics.h and conio.h. Initialize the graph. Translation refers to the shifting of a point to some other place, whose distance with regard to the present point is known. Consider a point P(x1, y1) to be translated to another point Q(x2, y2). If we know the point value (x2, y2) we can directly shift to Q by displaying the pixel (x2, y2). On the other hand, suppose we only know that we want to shift by a distance of Tx along x axis and Ty along Y axis. Then obviously the coordinates can be derived by x2 =x1 +Tx and Y2 = y1+ Ty. Close the graph and stop the program. SOURCE CODE:
#include<stdio.h> #include<conio.h> #include<math.h> #include<process.h> #include<graphics.h> int x1,x2,y1,y2,mx,my,depth; void draw(); void rotate(); void main() { int gd=DETECT,gm,c; initgraph(&gd,&gm,"c:\\tc\\bgi"); printf("\n3D Transformation Rotating\n\n"); printf("\nEnter 1st top value(x1,y1):"); scanf("%d%d",&x1,&y1); printf("Enter right bottom value(x2,y2):"); scanf("%d%d",&x2,&y2); depth=(x2-x1)/4; mx=(x1+x2)/2; my=(y1+y2)/2; draw(); getch(); cleardevice(); rotate(); getch(); } void draw() { bar3d(x1,y1,x2,y2,depth,1); } void rotate() 73

{ float t; int a1,b1,a2,b2,dep; printf("Enter the angle to rotate="); scanf("%f",&t); t=t*(3.14/180); a1=mx+(x1-mx)*cos(t)-(y1-my)*sin(t); a2=mx+(x2-mx)*cos(t)-(y2-my)*sin(t); b1=my+(x1-mx)*sin(t)-(y1-my)*cos(t); b2=my+(x2-mx)*sin(t)-(y2-my)*cos(t); if(a2>a1) dep=(a2-a1)/4; else dep=(a1-a2)/4; bar3d(a1,b1,a2,b2,dep,1); setcolor(5); }

OUTPUT:

74

RESULT: Thus the above code was implemented and executed successfully and the output was shown.

75

3D TRANSLATION

AIM: To write a C graphics program for 3D Translation. ALGORITHM: Start the program by importing the header files such as graphics.h and conio.h. Initialize the graph. Translation refers to the shifting of a point to some other place, whose distance with regard to the present point is known. Consider a point P(x1, y1) to be translated to another point Q(x2, y2). If we know the point value (x2, y2) we can directly shift to Q by displaying the pixel (x2, y2). On the other hand, suppose we only know that we want to shift by a distance of Tx along x axis and Ty along Y axis. Then obviously the coordinates can be derived by x2 =x1 +Tx and Y2 = y1+ Ty. Close the graph and stop the program. SOURCE CODE:
#include<stdio.h> #include<conio.h> #include<math.h> #include<process.h> #include<graphics.h> int x1,x2,y1,y2,mx,my,depth; void draw(); void trans(); void main() { int gd=DETECT,gm,c; initgraph(&gd,&gm,"c:\\tc\\bgi"); printf("\n\t\t3D Transmission\n\n"); printf("\nEnter 1st top value(x1,y1):"); scanf("%d%d",&x1,&y1); printf("Enter right bottom value(x2,y2):"); scanf("%d%d",&x2,&y2); depth=(x2-x1)/4; mx=(x1+x2)/2; my=(y1+y2)/2; draw(); getch(); cleardevice(); trans(); getch(); } void draw() { bar3d(x1,y1,x2,y2,depth,1); } 76

void trans() { int a1,a2,b1,b2,dep,x,y; printf("\n Enter the Ttransition Co ordinates:"); scanf("%d%d",&x,&y); a1=x1+x; a2=x2+x; b1=y1+y; b2=y2+y; dep=(a2-a1)/4; bar3d(a1,b1,a2,b2,dep,1); setcolor(5); draw(); }

OUTPUT:

77

RESULT: Thus the above code was implemented and executed successfully and the output was shown.

78

BOUNCING BALL AIM: To write a C graphics program to implement a Bouncing Ball. PROCEDURE: Start the program by importing the header files such as graphics.h and conio.h, math.h, stdlib.h, stdio.h. Calculate the midx and midy and draw the image size using imagesize(). Use getimagesize() and putimage size() to get and put the size of the images. Use the setcolor() to differentiate the different shapes. Check the midy value which does not exceeds the getmaxy value and display the image using the putimage(). Stop the program. SOURCE CODE:
#include <stdio.h> #include <conio.h> #include <math.h> #include <graphics.h> #include<stdlib.h> void main() { int dr,md,midx,i, midy,dx,dy,mx, my,k,x,y,xdir,ydir,oldx,oldy; double pi,tpi,a,t; void *ball; unsigned imgsize; dr = DETECT; initgraph(&dr, &md, "c:\\tc\\bgi"); midx = getmaxx()/2; midy = getmaxy()/2; pi = 3.14159; tpi = pi * 2.0; setcolor(13); circle(midx,midy,10); imgsize = imagesize(midx-10, midy-10, midx+10, midy+10); ball = malloc(imgsize); getimage(midx-10, midy-10, midx+10, midy+10,ball); putimage(midx-10, midy-10, ball,XOR_PUT); xdir = -5; ydir=5; do { oldx = midx; oldy = midy; midx = midx + random(xdir); midy = midy + random(ydir) ; if (midx < 0) { midx = midx + 5; xdir =5; } if (midx > getmaxx()) 79

{ midx = midx - 5; xdir = -5; } if (midy < 0) { midy = midy + 5; ydir = 5; } if (midy > getmaxy()) { midy = midy - 5; ydir = -5; } putimage(midx-10, midy-10,ball,XOR_PUT); for (a=1.0; a<30000.0; a=a+0.1); { } putimage(midx-10, midy-10,ball,XOR_PUT); } while (!kbhit()); getch(); }

OUTPUT:

RESULT: Thus the above code was implemented and executed successfully and the output was shown.

80

MOVING WHEEL

AIM: To write a C graphics program to implement a Moving Wheel. PROCEDURE: Start the program by importing the header files such as graphics.h and alloc.h. Initialize the graph. Draw the basic shapes by using the different functions such as circle(),line(), ellipse(), fillellipse(). Use the setcolor() to differentiate the different shapes. Defient the sixe of the image using imagesize(). Use getimage() and putimage() to get and put the images. Display the image using putimage(). Using the delay() make the image to rotate in an regular inerval. Close the graph and stop the program. SOURCE CODE:
#include<graphics.h> #include<alloc.h> void wheel(); void main() { int gd,gm; detectgraph(&gd,&gm); initgraph(&gd,&gm,"c:\\tc\\bgi"); wheel(); getch(); closegraph(); } void wheel() { int x,y,area1,area2,area3,area4; void *buff1,*buff2,*buff3,*buff4; circle(320,240,40); circle(320,240,30); line(290,240,350,240); setfillstyle(SOLID_FILL,YELLOW); fillellipse(285,240,5,5); setfillstyle(SOLID_FILL,GREEN); fillellipse(355,240,5,5); line(320,210,320,270); setfillstyle(SOLID_FILL,RED); fillellipse(320,205,5,5); setfillstyle(SOLID_FILL,BLUE); fillellipse(320,275,5,5); area1=imagesize(280,200,360,280); buff1=malloc(area1); 81

getimage(280,200,360,280,buff1); putimage(480,120,buff1,COPY_PUT); clearviewport(); circle(320,240,40); circle(320,240,30); line(300,220,340,260); setfillstyle(SOLID_FILL,DARKGRAY); fillellipse(295,215,5,5); setfillstyle(SOLID_FILL,MAGENTA); fillellipse(345,265,5,5); line(300,260,340,220); setfillstyle(SOLID_FILL,CYAN); fillellipse(295,265,5,5); setfillstyle(SOLID_FILL,BROWN); fillellipse(345,215,5,5); area2=imagesize(280,200,360,280); buff2=malloc(area2); getimage(280,200,360,280,buff2); putimage(480,120,buff2,COPY_PUT); clearviewport(); circle(320,240,40); circle(320,240,30); line(290,240,350,240); setfillstyle(SOLID_FILL,DARKGRAY); fillellipse(285,240,5,5); setfillstyle(SOLID_FILL,CYAN); fillellipse(355,240,5,5); line(320,210,320,270); setfillstyle(SOLID_FILL,MAGENTA); fillellipse(320,205,5,5); setfillstyle(SOLID_FILL,BROWN); fillellipse(320,275,5,5); area3=imagesize(280,200,360,280); buff3=malloc(area3); getimage(280,200,360,280,buff3); putimage(480,120,buff3,COPY_PUT); clearviewport(); circle(320,240,40); line(300,220,340,260); setfillstyle(SOLID_FILL,YELLOW); fillellipse(295,215,5,5); setfillstyle(SOLID_FILL,GREEN); fillellipse(345,265,5,5); line(300,260,340,220); setfillstyle(SOLID_FILL,RED); fillellipse(295,265,5,5); setfillstyle(SOLID_FILL,BLUE); fillellipse(345,215,5,5); area4=imagesize(280,200,360,280); buff4=malloc(area4); getimage(280,200,360,280,buff4); putimage(480,120,buff4,COPY_PUT); clearviewport(); clearviewport(); x=0;y=240; 82

setcolor(GREEN); while(!kbhit()) { if(x==640) x=0; putimage(x,y,buff1,COPY_PUT); x=x+5; delay(100); clearviewport(); putimage(x,y,buff2,COPY_PUT); x=x+5; delay(100); clearviewport(); putimage(x,y,buff3,COPY_PUT); x=x+5; delay(100); clearviewport(); putimage(x,y,buff4,COPY_PUT); x=x+5; delay(100); clearviewport(); } }

OUTPUT:

RESULT: Thus the above code was implemented and executed successfully and the output was shown.

83

CAR MOVEMENT AIM: To write a C graphics program to implement a Car Movement. PROCEDURE: Start the program by importing the header files such as graphics.h and conio.h and dos.h. Initialize the graph. Display the text using outtextxy() and set the port usingsetviewport(). Draw the basic shapes by using the different functions such as rectangle(), circle(). Use the setcolor() to differentiate the different shapes. Fill the shapes by using different styles such as HATCH_FILL, SOLID_FILL and so on. Close the graph and stop the program. SOURCE CODE:
#include <graphics.h> #include <dos.h> #include <conio.h> main() { int i, j = 0, gd = DETECT, gm; initgraph(&gd,&gm,"C:\\TC\\BGI"); settextstyle(DEFAULT_FONT,HORIZ_DIR,2); outtextxy(25,240,"Press any key to view the moving car"); getch(); setviewport(0,0,639,440,1); for( i = 0 ; i <= 420 ; i = i + 10, j++ ) { rectangle(50+i,275,150+i,400); rectangle(150+i,350,200+i,400); circle(75+i,410,10); circle(175+i,410,10); setcolor(j); delay(100); if( i == 420 ) break; clearviewport(); } getch(); closegraph(); return 0; }

84

OUTPUT:

RESULT: Thus the above code was implemented and executed successfully and the output was shown.

85

FILLING BUCKET WITH WATER AIM: To write a C graphics program to fill a bucket with water from a pipe. PROCEDURE: Start the program by importing the header files such as graphics.h and conio.h, dos.h. Initialize the graph. Draw the basic shapes by using the different functions such as arc(), line(), ellipse(), fillellipse(), pieslice(). Use the setcolor() to differentiate the different shapes. Fill the shapes by using different styles such as HATCH_FILL, SOLID_FILL and so on. Displat the pxel values by using putpixel(). Using the for() and delay() fill the water in the bucket by cheking the conditions. Close the graph and stop the program. SOURCE CODE:
#include<conio.h> #include<dos.h> #include<graphics.h> void main() { int gd = DETECT, gm = DETECT, i, j; initgraph(&gd, &gm, "c:\\tc\\bgi"); ellipse(300, 200, 0, 360, 50, 25); ellipse(300, 300, 0, 360, 50, 25); line(250, 200, 250, 300); line(350, 200, 350, 300); ellipse(300, 100, 180, 360, 5, 2); line(295, 100, 295, 80); line(305, 100, 305, 86); arc(300, 80, 90, 180, 5); putpixel(306, 85, 15); putpixel(307, 84, 15); line(308, 84, 630, 84); line(300, 75, 303, 75); line(314, 75, 630, 75); putpixel(304, 74, 15); putpixel(305, 73, 15); line(306, 72, 306, 65); line(311, 72, 311, 65); putpixel(312, 73, 15); putpixel(313, 74, 15); pieslice(309, 62, 0, 360, 5); setfillstyle(SOLID_FILL, BLUE); setcolor(BLUE); for (i = 0; i < 7; i++) { line(297 + i, 103, 297 + i, 300); } 86

for (i = 1; i < 100; i++) { setcolor(LIGHTBLUE); ellipse(300, 300 - i, 180, 360, 4, 2); delay(30); fillellipse(300, 300 - i, 49, 25); setcolor(1); line(297, 275 - i, 303, 275 - i); setcolor(15); ellipse(300, 200, 180, 360, 50, 25); delay(50); } ellipse(300, 200, 0, 360, 50, 25); setcolor(0); for (i = 0; i < 7; i++) line(297 + i, 103, 297 + i, 174); getch(); }

OUTPUT:

RESULT: Thus the above code was implemented and executed successfully and the output was shown.

87

MAN WALKING IN RAIN AIM: To write a C graphics program to implement Man Walking in Rain. PROCEDURE: Start the program by importing the header files such as stdio.h, dos.h, stdlib.h, graphics.h and conio.h. Initialize the function rain() and find the dx and dy value by subtracting x2 from x1 and from y2 from y1. Draw the basic shapes by using the different functions such as circle(),line(), ellipse(). Use the setcolor() to differentiate the different shapes. Check the abs value of dx > dy. Display the pixel by using putpixel() Initialize the graph. By checking the condition in for loop display the line. Fill the shapes by using different styles such as HATCH_FILL, SOLID_FILL and so on. Close the graph and stop the program. SOURCE CODE:
#include<stdio.h> #include<dos.h> #include<conio.h> #include<graphics.h> #include<stdlib.h> void rain(int x1,int y1,int x2,int y2) { int s,dx,dy,m,c=0,t=1; float xi,yi,x,y; dx=x2-x1; dy=y2-y1; if(abs(dx)>abs(dy)) s=abs(dx); else s=abs(dy); xi=dx/(float)s; yi=dy/(float)s; x=x1; y=y1; putpixel(x1+0.5,y1+0.5,9); for(m=0;m<s;m++) { c++; x+=xi; y+=yi; if(getpixel(x,y)==4) break; if(c%10==0) t++; 88

putpixel(x+0.5,y+0.5,0); if(t%2==0) putpixel(x+0.5,y+0.5,9); } } void main() { int gd=DETECT,gm=DETECT,c=-200,i=0,x=40,l=15,h=15,ht=0; initgraph(&gd,&gm,"c:\\tc\\bgi"); cleardevice(); setcolor(BROWN); line(0,201,600,201); cont: while(!kbhit()) { setcolor(4); ellipse(x,100,0,180,50,30); line(x-50,100,x+50,100); line(x,100,x,150); circle(x-20,115,15); line(x-20,130,x-20,175); line(x-20,175,x-20-l,200); line(x-20,175,x-20+l,200); line(x-20,140,x,150); line(x-20,140,x-20-h,160); for(i=0;i<620;i+=20) { rain(i,c,i,200); } c++; if(c==0) c=-100; setcolor(0); delay(50); ellipse(x,100,0,180,50,30); line(x-50,100,x+50,100); line(x,100,x,150); circle(x-20,115,15); line(x-20,130,x-20,175); line(x-20,175,x-20-l,200); line(x-20,175,x-20+l,200); line(x-20,140,x,150); line(x-20,140,x-20-h,160); line(x+50,100,x+50,200); x++; l--; if(l==-15) l=15; if(ht==1) h++; else h--; if(h==15) ht=0; else if(h==-15) ht=1; 89

} if(getch()==' ') { while(!kbhit()); getch(); goto cont; } }

OUTPUT:

RESULT: Thus the above code was implemented and executed successfully and the output was shown.

90

TRAFFIC CONTROL SIMULATION

AIM: To write a C graphics program to implement Traffic Control Simulation. PROCEDURE: Start the program by importing the header files such as graphics.h and conio.h, dos.h, stdlib.h. Initialize the graph. Calculate the midx and midy values. Draw the basic shapes by using the different functions such as rectangle(), circle(). Use the setcolor() to differentiate the different shapes. Use settextstyle() and settextjustify() to set the styles and justify the styles respectively. Display the text using outtextxy(). Close the graph and stop the program. SOURCE CODE:
#include<graphics.h> #include<conio.h> #include<dos.h> #include<stdlib.h> main() { int gd = DETECT, gm, midx, midy; initgraph(&gd, &gm, "C:\\TC\\BGI"); midx = getmaxx()/2; midy = getmaxy()/2; setcolor(RED); settextstyle(SCRIPT_FONT, HORIZ_DIR, 3); settextjustify(CENTER_TEXT, CENTER_TEXT); outtextxy(midx, midy-10, "Traffic Light Simulation"); outtextxy(midx, midy+10, "Press any key to start"); getch(); cleardevice(); setcolor(WHITE); settextstyle(DEFAULT_FONT, HORIZ_DIR, 1); rectangle(midx-30,midy-80,midx+30,midy+80); circle(midx, midy-50, 22); setfillstyle(SOLID_FILL,RED); floodfill(midx, midy-50,WHITE); setcolor(BLUE); outtextxy(midx,midy-50,"STOP"); delay(2000); graphdefaults(); cleardevice(); setcolor(WHITE); rectangle(midx-30,midy-80,midx+30,midy+80); circle(midx, midy, 20); 91

setfillstyle(SOLID_FILL,YELLOW); floodfill(midx, midy,WHITE); setcolor(BLUE); outtextxy(midx-18,midy-3,"READY"); delay(2000); cleardevice(); setcolor(WHITE); rectangle(midx-30,midy-80,midx+30,midy+80); circle(midx, midy+50, 22); setfillstyle(SOLID_FILL,GREEN); floodfill(midx, midy+50,WHITE); setcolor(BLUE); outtextxy(midx-7,midy+48,"GO"); setcolor(RED); settextstyle(SCRIPT_FONT, HORIZ_DIR, 4); outtextxy(midx-150, midy+100, "Press any key to exit..."); getch(); closegraph(); return 0; }

OUTPUT:

92

RESULT: Thus the above code was implemented and executed successfully and the output was shown.

93

You might also like