You are on page 1of 59

Practical File On Computer Graphics

Submitted To: Submitted By:-

Computer Graphics 201 1

INDEX
Sr.No 1. 2. 3. 4. 5. 6. 7. 8. 9. 10 11. 12. 13. Topic Introduction to Computer Graphics Applications of Computer Graphics Inbuilt Functions in Computer Graphics Program to draw line with direct method Program to draw line with Bresenham Line Algorithm Program to draw line with Direct Differential Algorithm Program to draw a circle with Direct Method Program to draw circle with Bresenham Circle Algorithm Program to draw circle with Midpoint Circle Algorithm Program to draw circle with Polar Algorithm Program to draw ellipse with Polynomial Algorithm Program to draw ellipse with Polar Algorithm Programs of 2D Transformations. Program to show the Translation of a line Program to show the Scaling Transformation Program to show the Rotation of an object Program to show the Reflection Transformation Program to show Shearing Transformation 14. Programs of Seven Applications Page No. 3 4 7 10 12 14 16 18 20 22 24 26 28 28 30 31 32 34 36

Computer Graphics 201 1

Computer Graphics
Computer graphics are graphics created using computers and, more generally, the representation and manipulation of image data by a computer. The development of computer graphics has made computers easier to interact with, and better for understanding and interpreting many types of data. Developments in computer graphics have had a profound impact on many types of media and have revolutionized animation, movies and the video game industry. The term computer graphics has been used in a broad sense to describe "almost everything on computers that are not text or sound. Typically, the term computer graphics refers to several different things:

the representation and manipulation of image data by a computer the various technologies used to create and manipulate images the images so produced, and the sub-field of computer science which studies methods for digitally synthesizing and manipulating visual content, see study of computer graphics Computer graphics is the life line of today's computer world. Today, computers and computer-generated images touch many aspects of daily life. Computer imagery is found on television, in newspapers, for example in weather reports, or for example in all kinds of medical investigation and surgical procedures. A well-constructed graph can present complex statistics in a form that is easier to understand and interpret. Computer generated imagery can be categorized into several different types: 2D, 3D, 5D, and animated graphics. As technology has improved, 3D computer graphics have become more common, but 2D computer graphics are still widely used. Computer graphics has emerged as a sub-field of computer science

2D Computer Graphics
2D computer graphics are the computer-based generation of digital imagesmostly from twodimensional models, such as 2D geometric models, text, and digital images, and by techniques specific to them. The word may stand for the branch of computer science that comprises such techniques, or for the models themselves.

Computer Graphics 201 1


2D computer graphics are mainly used in applications that were originally developed upon traditional printing and drawing technologies, such as typography, cartography, technical drawing, advertising, etc.. In those applications, the two-dimensional image is not just a representation of a real-world object, but an independent artifact with added semantic value; twodimensional models are therefore preferred, because they give more direct control of the image than 3D computer graphics, whose approach is more akin to photography than to typography.

3D Computer Graphics:
3D computer graphics in contrast to 2D computer graphics are graphics that use a threedimensional representation of geometric data that is stored in the computer for the purposes of performing calculations and rendering 2D images. Such images may be for later display or for real-time viewing. Despite these differences, 3D computer graphics rely on many of the same algorithms as 2D computer vector graphics in the wire frame model and 2D computer raster graphics in the final rendered display. In computer graphics software, the distinction between 2D and 3D is occasionally blurred; 2D applications may use 3D techniques to achieve effects such as lighting, and primarily 3D may use 2D rendering techniques.

Computer Animation
Computer animation is the art of creating moving images via the use of computers. It is a subfield of computer graphics and animation. Increasingly it is created by means of 3D computer graphics, though 2D computer graphics are still widely used for stylistic, low bandwidth, and faster real-time rendering needs. Sometimes the target of the animation is the computer itself, but sometimes the target is another medium, such as film. It is also referred to as CGI (Computergenerated imagery or computer-generated imaging), especially when used in films.

Application area of Computer Graphics


Computer graphics encompasses a wide variety of applications. It has expanded its scope from the mundane business/presentation graphics to placing desktop publishing at everyones ngertips. Highly interactive real- time systems are used in ight simulators where the display represents changes in the scene or landscape. In engineering, computer-aided design (CAD) systems allow users to create, store, manipulate, and test objects and designs. Fully integrated systems allow standard component parts libraries to be incorporated into a product. Product design and drafting information is fed into manufacturing operations via numerical control interfaces. Other engineering applications that make extensive use of graphics include very large
4

Computer Graphics 201 1


scale integration (VLSI) and solid modeling. Some of the common application areas of Computer Graphics.

Computer-Aided Design (CAD)


Engineering has been transformed by computer graphics in the form of CAD. In the past drawings had to be painstakingly prepared by draughts people and mock-ups and prototypes of products built at every stage of development. With CAD, however, the product can be designed and tested within the computer, drastically reducing the time taken to get products from the draughting table to the market. CAD is used in all areas of engineering, from car design to electronics. Not surprisingly, every CAD drawing is a vector graphic composed of objects that the designer can manipulate at will. Both individual objects and collections of objects can be tested: the designer can apply known inputs to an electronic circuit, for example, to see if it produces the required output, or a wing section could be put under stress in a 'virtual' wind tunnel by the computer. So long as the models of behavior of objects like wing struts and electronic components are accurate then the testing precludes the production of physical mock-ups. When such physical objects are required, the computer can drive a machine to create a model for testing, a process which is both quick and totally accurate.

Visualization
Visualization is the process of rendering data as images in order to enhance our understanding. The idea is not new: maps are visualizations of terrain and since the early days of printing graphs and charts have been used to represent both empirical data and mathematical functions. Although visualization is used in nearly all fields of human Endeavour where computers may be used to process and display data, it's fair to say that the technique is used to the greatest extent in the commercial and scientific worlds.

Business Graphics
This term covers charts and graphs that represent economic statistics and which are generally used by managers in both in the public and private sectors. In particular, business visualization is used in analysis, to extract meaning from data, and for presentation, to explain data to an audience. The use of images in economics considerably predates the computer age, but of course computers enable the production of many kinds of highly effective and colorful charts from raw data in seconds; as a result a thriving market in presentation software exists.

Graphical User Interface


5

Computer Graphics 201 1


Every software packages these days are GUI based. Major component of GUI based software is a window. With a pointing device such as mouse; select an option in the menu of the window. Multiple windows can be displayed, each of which can contain different process. It makes operating computer very easy, because it contain more graphical pictures than the text. Graphics based user interface allows new users to control simple application programs.

Entertainment
Computer Graphics can be seen commonly used in music videos, television advertisement, animation pictures and various movies. Computer graphic techniques are used in these applications to produce transformation of one object into another, the process called morphing. Virtually every game on the computer uses computer graphics or animation. This includes Packman, Doom. Same CD games are actually interactive computer rendered movies such as The Seventh Guest.

Education and Presentation


People are always looking for new ways to educate their children. If they are having fun, they learn better. Computer animation can be used to make very exciting and fun videos into which education can easily be incorporated. In science computer animation might be used to show how our solar system works, and in math, a computer animation might show a student how one can algebraically manipulate a equation.

Image Processing
Image processing is manipulation of images with the help of computers. Some of the algorithms used in image processing include thinning or skeletonisation, edge detection and contrast enhancement. These are usually implemented in software but may also use special purpose hardware for speed. Image processing contrasts with computer graphics, which is usually more concerned with the generation of artificial images, and visualization, which attempts to understand real-world data by displaying it as an artificial image.

Virtual Reality
Virtual reality is a way for humans to visualize, manipulate and interact with computers and extremely complex data. The visualization part refers to the computer generating visual, auditory or other sensual outputs to the user of a world within the computer. This world may ne CAD model, a scientific simulation, or a view into a database. The user can interact with the world and directly manipulate objects within the world. Interaction with the virtual world, at least with near real time control of the viewpoint, in my opinion, is a critical test for a virtual reality.
6

Computer Graphics 201 1 Cartography


Computer Graphics is used to produce both accurate representations of geographical maps etc., Exploration maps for drilling and mining, Whether maps and Population density maps.

Inbuilt Functions in Computer Graphics


1.

To draw Line: We have a line function directly to draw a line. It contains 4 parameters x1, y1, x2, y2. Here (x1, y1) are the starting point of the line and (x2, y2) are the ending points of the line. Syntax: line(x1, y1, x2, y2);

Example: line(20,10,30,40); Here x1 should be less than x2 and y1 should be less than y2.
2.

To draw an arc: To draw an arc we have an arc function. It has five parameters x1, y1, staring angle, ending angle, and radius.

Syntax: arc(x1, y1, starting angle, ending angle, radius); Example:


7

Computer Graphics 201 1


arc(20,30,90,180,50);
3.

To draw circle: to draw circle we have an inbuilt function with three parameters x centre, y centre and radius.

Syntax: circle(x centre, y centre, radius); Example: circle(100,200,50);


4.

To draw ellipse: to draw ellipse we have an inbuilt function with three six parameters x, y, starting angle, ending angle, x radius and y radius). An ellipse has two radius so we are here x and y radius are used.

Syntax: ellipse(x, y, staring angle, ending angle, x radius, y radius); Example: ellipse (100,150,70,200,60,70);
5.

To plot pixel on screen: to plot a pixel we use putpixel function with three parameters x, y, color. Here x represents pixels on x-axis and y represents pixels on y-axis. Syntax:

putpixel(x, y, COLOR);

Example:

Computer Graphics 201 1


putpixel(30,40,BLUE);

6.

To write text on the screen: to write text on screen we use outtext function. It has three parameters x, y, Name.

Syntax:

outtextxy (x, y, Text);

Example:

outtextxy (50,100,Poonam);

7.

To set style of the text: to set style of the text we use set text style function. It has three parameters style name, direction, size. Syntax: settextstyle (stylename, direction, size);

Example:

Settextstyle (TRIPLEX_FONT,0,7);
9

Computer Graphics 201 1


By default direction is horizontal . horizontal-> 0 vertical-> 1 size limit-> 0-15

8.

To set style of a line: to set style of a line we use set line style function having three parameters style, unsigned pattern, and thickness. By default line is dotted in nature. horizontal-> 0 vertical-> 1 thickness range-> 0-1

Syntax: setlinestyle(style, Unsigned pattern, thickness);

Example:

setlinestyle(SOLID_LINE, 0, 1);

9.

To fill color in an object: to fill color in objects we have set fill style in-built function. It has two parameters fill style pattern, color.

Syntax:

10

Computer Graphics 201 1


setfillstyle(fill style pattern, COLOR);

Example:

Setfillstyle(SOLID_FILL,RED);

Scan Conversion Algorithms


Program to draw line with Direct Method.
#include<graphics.h> #include<iostream.h> #include<conio.h> void main() { int gd=DETECT,gm; initgraph(&gd,&gm," "); int x1,x2,y1,y2,m,c; cout<<"enter starting and ending points";
11

Computer Graphics 201 1


cin>>x1>>y1>>x2>>y2; int dy=y2-y1; int dx=x2-x1; m=dy/dx; c=y1-m*x1; while(x1<=x2) { if(m<=1) { x1=x1+1; y1=m*x1+c; putpixel(x1,y1,WHITE); } else { y1=y1+1; x1=(y1-c)/m; putpixel(x1,y1,WHITE); } } getch(); }

Output

12

Computer Graphics 201 1

Program to draw line with Bresenham Line Algorithm


#include<iostream.h> #include<conio.h> #include<graphics.h> void main() { int x1,x2,y1,y2; int gd=DETECT,gm; initgraph(&gd,&gm," "); cout<<"enter starting and ending coordinates";
13

Computer Graphics 201 1


cin>>x1>>y1>>x2>>y2; int f,dy,dx; f=2*dy-dx; while(x1<=x2) { if(f<=0) { x1=x1+1; f=f+2*dy; putpixel(x1,y1,WHITE); } else { x1++; y1++; f=f+2*dy-2*dx; putpixel(x1,y1,WHITE); } } getch(); }

Output

14

Computer Graphics 201 1

Program to draw line with Direct Differential Algorithm


#include<graphics.h> #include<iostream.h> #include<conio.h> void main() { int gd=DETECT,gm;
15

Computer Graphics 201 1


initgraph(&gd,&gm," "); int x1,x2,y1,y2,m,c,dy,dx; cout<<"enter starting and ending points"; cin>>x1>>y1>>x2>>y2; dy=y2-y1; dx=x2-x1; m=dy/dx; while(x1<=x2) { if(m<=1) { x1=x1+1; y1=y1+m; putpixel(x1,y1,WHITE); } else { y1=y1+1; x1=x1+1/m; putpixel(x1,y1,WHITE); } } getch(); }
16

Computer Graphics 201 1


Output

Program to draw a circle with Direct Method


#include<math.h> #include<iostream.h> #include<conio.h> #include<graphics.h> void main()
17

Computer Graphics 201 1


{ clrscr();. int x,y,r,xc,yc; int x2; cout<<"enter x-centre ,y-centre and radius"; cin>>xc>>yc>>r; int gd=DETECT,gm; initgraph(&gd,&gm," "); x=0; x2=r/sqrt(2); while(x<=x2) { x=x+1; y=sqrt((r*r)-(x*x)); putpixel(x+xc,y+yc,WHITE); putpixel(y+xc,x+yc,WHITE); putpixel(x+xc,-y+yc,WHITE); putpixel(-y+xc,x+yc,WHITE); putpixel(-x+xc,-y+yc,WHITE); putpixel(-y+xc,-x+yc,WHITE); putpixel(-x+xc,y+yc,WHITE); putpixel(y+xc,-x+yc,WHITE); } getch();
18

Computer Graphics 201 1


}

Output

Program to draw circle with Bresenham Circle Algorithm


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

Computer Graphics 201 1


#include<graphics.h> void main() { clrscr(); int x,y,r,xc,yc; int x2; cout<<"enter x-centre ,y-centre and radius"; cin>>xc>>yc>>r; int gd=DETECT,gm; initgraph(&gd,&gm," "); x=0; y=r; int f=3-2*r; while(x<=y) { if(f<0) { x=x+1; f=f+(4*x+6); putpixel(x+xc,y+yc,WHITE); } else { x=x+1;
20

Computer Graphics 201 1


y=y-1; f=f+(4*x-4*y+10); } putpixel(x+xc,y+yc,WHITE); putpixel(y+xc,x+yc,WHITE); putpixel(x+xc,-y+yc,WHITE); putpixel(-y+xc,x+yc,WHITE); putpixel(-x+xc,-y+yc,WHITE); putpixel(-y+xc,-x+yc,WHITE); putpixel(-x+xc,y+yc,WHITE); putpixel(y+xc,-x+yc,WHITE); } getch(); }

Output

Program to draw circle with midpoint Bresenham Circle Algorithm


#include<math.h>
21

Computer Graphics 201 1


#include<iostream.h> #include<conio.h> #include<graphics.h> void main() { clrscr(); int x,y,r,xc,yc; int x2; cout<<"enter x-centre ,y-centre and radius"; cin>>xc>>yc>>r; int gd=DETECT,gm; initgraph(&gd,&gm," "); x=0; y=r; int f=1-r; while(x<=y) { if(f<0) { x=x+1; f=f+(2*x+3); putpixel(x+xc,y+yc,WHITE); } else
22

Computer Graphics 201 1


{ x=x+1; y=y-1; f=f+(2*x-2*y+5); } putpixel(x+xc,y+yc,WHITE); putpixel(y+xc,x+yc,WHITE); putpixel(x+xc,-y+yc,WHITE); putpixel(-y+xc,x+yc,WHITE); putpixel(-x+xc,-y+yc,WHITE); putpixel(-y+xc,-x+yc,WHITE); putpixel(-x+xc,y+yc,WHITE); putpixel(y+xc,-x+yc,WHITE); } getch(); }

Output

23

Computer Graphics 201 1


Program to draw circle with Polar Algorithm
#include<math.h> #include<iostream.h> #include<conio.h> #include<graphics.h> #include<stdlib.h> void main() { clrscr(); int gd=DETECT,gm; initgraph(&gd,&gm," "); int x,y,r,xc,yc; int x2; cout<<"enter x-centre ,y-centre and radius"; cin>>xc>>yc>>r; x=0; y=r; float theta=1/r; while(x<=y) { putpixel(x+xc,y+yc,WHITE); putpixel(y+xc,x+yc,WHITE); putpixel(x+xc,-y+yc,WHITE); putpixel(-y+xc,x+yc,WHITE);
24

Computer Graphics 201 1


putpixel(-x+xc,-y+yc,WHITE); putpixel(-y+xc,-x+yc,WHITE); putpixel(-x+xc,y+yc,WHITE); putpixel(y+xc,-x+yc,WHITE); float temp=y; y=y*cos(theta)+x*sin(theta); x=x*cos(theta)-temp*sin(theta);

} getch(); }

Output

25

Computer Graphics 201 1

Program to draw ellipse with Polynomial Algorithm


#include<math.h> #include<iostream.h> #include<conio.h> #include<graphics.h> #include<stdlib.h> void main() { clrscr(); int gd=DETECT,gm; initgraph(&gd,&gm," "); float xr,yr,xc,yc; float x,y; cout<<"enter x-centre ,y-centre and radius"; cin>>xc>>yc>>xr>>yr; float x1=0; float x2=xr; while(x1<=x2) { y=sqrt(1-((x1*x1)/(xr*xr)))*yr; putpixel(x1+xc,y+yc,WHITE); putpixel(-x1+xc,y+yc,WHITE); putpixel(-x1+xc,-y+yc,WHITE);
26

Computer Graphics 201 1


putpixel(x1+xc,-y+yc,WHITE); x1=x1+1; } getch(); }

Output

27

Computer Graphics 201 1

Program to draw ellipse with Polar Algorithm


#include<math.h> #include<iostream.h> #include<conio.h> #include<graphics.h> #include<stdlib.h> void main() { clrscr(); int gd=DETECT,gm; initgraph(&gd,&gm," "); float xr,yr,xc,yc,x,y; float theta1,theta2; theta1=0; theta2=90 cout<<"enter x-centre ,y-centre and radius"; cin>>xc>>yc>>xr>>yr;

while(theta1<=theta2) { x=xr*cos(theta1);
28

Computer Graphics 201 1


y=yr*sin(theta1); putpixel(x+xc,y+yc,WHITE); putpixel(-x+xc,y+yc,WHITE); putpixel(-x+xc,-y+yc,WHITE); putpixel(x+xc,-y+yc,WHITE); theta1=theta1+1; } getch(); }

Output

29

Computer Graphics 201 1

Programs of 2D Transformations
Program to show the Translation of a line
#include<graphics.h> #include<iostream.h> #include<conio.h> #include<dos.h> #include<stdlib.h> void main() { int gd=DETECT,gm; int x,x1,y,y1,r,tx,ty; initgraph(&gd,&gm," "); cleardevice(); setcolor(7); cout<<"enter value x,y,r"; cin>>x>>y>>r; circle(x,y,r);

30

Computer Graphics 201 1


cout<<"enter value tx,ty"; cin>>tx>>ty; x1=tx+x; y1=ty+y; setcolor(5); circle(x1,y1,r); getch(); closegraph(); }

Output

31

Computer Graphics 201 1

Program to show the Scaling Transformation


#include<graphics.h> #include<iostream.h> #include<conio.h> #include<dos.h>
32

Computer Graphics 201 1


#include<stdlib.h> void main() { int gd=DETECT,gm; int x1,x2,y1,y2,sx,sy,newx,newy; initgraph(&gd,&gm," "); cleardevice(); setcolor(7); cout<<"enter value of x1,x2,y1,y2"; cin>>x1>>x2>>y1>>y2; rectangle(x1,y1,x2,y2); cout<<"enter value sx,sy"; cin>>sx>>sy; x1=x1*sx; y1=y1*sy; x2=x2*sx; y2=y2*sy; rectangle(x1,y1,x2,y2); getch();

closegraph(); }

Output
33

Computer Graphics 201 1

Program to show the Rotation of an object


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

Computer Graphics 201 1


#include<dos.h> #include<stdlib.h> #include<math.h> void main() { int gd=DETECT,gm; int x1,x2,y1,y2,sin,cos,theta; initgraph(&gd,&gm," "); cleardevice(); setcolor(7); cout<<"Enter Value X1,Y1,X2,Y2"; cin>>x1>>y1>>x2>>y2; line(x1,y1,x2,y2); x1=x1*cos(theta) - y1*sin(theta); y1=y1*cos(theta) + x1*sin(theta); line(x1,y1,x2,y2); getch(); closegraph(); }

Program to show the Reflection Transformation


#include<iostream.h> #include<conio.h>
35

Computer Graphics 201 1


#include<graphics.h> #include<dos.h> void main() { int x1,y1,r,sx,sy; char n; int gd=DETECT,gm; initgraph(&gd,&gm,""); int p=getmaxx()/2; int q=getmaxy(); int t=getmaxx(); int s=getmaxy()/2; line(p,0,p,q); line(0,s,t,s);

cout<<"Enter value for x1,y1,r"; cin>>x1>>y1>>r; int xn1=x1+p; int yn1=y1+s; circle(xn1,yn1,r); cout<<"which reflection do u want"; cin>>n; if(n=='x') {
36

Computer Graphics 201 1


xn1=x1+p; yn1=-y1+s; circle(xn1,yn1,r); cout<<"the new circle after x reflection is"; circle(xn1,yn1,r); } if(n=='y') { xn1=-x1 +p; yn1=y1+s; cout<<"the new circle is"; circle(xn1,yn1,r); } getch(); closegraph(); }

Program to show Shearing Transformation


37

Computer Graphics 201 1


#include<graphics.h> #include<iostream.h> #include<dos.h> #include<conio.h> void main() { int gd=DETECT,gm; initgraph(&gd,&gm,); int x1,x2,y1,y2,shx,shy; char n; cout<<Enter values of x1,x2,y1,y2; cin>>x1>>x2>>y1>>y2; rectangle(x1,y1,x2,y2); cout<<enter values of shx and shy; cin>>shx>>shy; cout<<what type of shearing do you want; cin>>n; if (n==x) { x1=x1+y1*shx; x2=x2+y2*shx; rectangle(x1,y1,x2,y2); }

38

Computer Graphics 201 1


elseif(n==y) { y1=y1+x1*shy; y2=y2+x2*shy; rectangle(x1,y1,x2,y2); } else { x1=x1+y1*shx; x2=x2+y2*shx; y1=y1+x1*shy; y2=y2+x2*shy; rectangle(x1,y1,x2,y2); } getch(); closegraph(); }

Output

39

Computer Graphics 201 1

Programs of 7 Applications Developed


Program to depict a Pie Chart
#include<graphics.h> #include<iostream.h> #include<stdlib.h> #include<math.h> #include<dos.h> #include<conio.h> void main() { int gd=DETECT,gm; float xc,yc,r,x,y,xend,sa,ea; clrscr(); initgraph(&gd,&gm," "); cleardevice(); setcolor(5); setbkcolor(7); pieslice(200,300,0,90,90); setcolor(2); pieslice(200,300,90,150,90); setcolor(3); pieslice(200,300,150,180,90);
40

Computer Graphics 201 1


setcolor(4); pieslice(200,300,180,220,90); setcolor(6);

pieslice(200,300,220,250,90); setcolor(8); pieslice(200,300,250,300,90); setcolor(9); pieslice(200,300,300,360,90); setcolor(10); outtextxy(150,150,"Food Items"); outtextxy(215,275,"MILK 30%"); outtextxy(175,275,"Egg"); outtextxy(120,275,"Curd"); outtextxy(120,315,"Chicken"); outtextxy(130,355,"Barfi"); getch(); closegraph(); }

41

Computer Graphics 201 1

Program to depict many Circles


#include<graphics.h> #include<iostream.h> #include<conio.h> #include<dos.h> #include<stdlib.h> #include<math.h> void circlepoint(int ,int, int ,int); void main() { int gd=DETECT,gm; initgraph(&gd,&gm," " ); int xc,yc,r,x,y,xend,c,a,b; cout<<"enter xc,yc and radius"; cin>>xc>>yc>>r; while(!kbhit()) { x=0; xend=r/(sqrt(2)); while(x<=xend) { x=x+1;
42

Computer Graphics 201 1


y=sqrt((r*r)-(x*x)); circlepoint(x,y,xc,yc); } r=r-10; } closegraph(); getch(); } void circlepoint(int x,int y,int xc,int yc) { int c; c=rand()%10; putpixel(x+xc,y+yc,c); putpixel(y+xc,x+yc,c); putpixel(-y+xc,x+yc,c); putpixel(-x+xc,y+yc,c); putpixel(-x+xc,-y+yc,c); putpixel(-y+xc,-x+yc,c); putpixel(x+xc,-y+yc,c); putpixel(y+xc,-x+yc,c); delay(500); }

Output

43

Computer Graphics 201 1

Program to show a Flower


#include<graphics.h> #include<iostream.h> #include<conio.h> #include<dos.h> #include<stdlib.h> void main() { int gd=DETECT,gm; int x,y,c; initgraph(&gd,&gm," "); setcolor(YELLOW); circle(300,200,58); setfillstyle(HATCH_FILL,BROWN); floodfill(290,180,YELLOW); ellipse(250,200,85,276,70,40); setfillstyle(SOLID_FILL,YELLOW);
44

Computer Graphics 201 1


floodfill(240,180,YELLOW); ellipse(350,200,265,95,70,40); setfillstyle(SOLID_FILL,YELLOW); floodfill(360,180,YELLOW); ellipse(300,150,355,185,40,70); setfillstyle(SOLID_FILL,YELLOW); floodfill(290,140,YELLOW); ellipse(300,250,175,5,40,70); setfillstyle(SOLID_FILL,YELLOW); floodfill(290,280,YELLOW); setcolor(BROWN); ellipse(345,450,270,90,90,210); setcolor(7); ellipse(377,155,0,360,11,5); circle(369,153,1); line(383,160,385,163); setcolor(0); line(366,158,368,164); setcolor(7); ellipse(380,155,0,180,7,7); getch(); closegraph(); }

Output
45

Computer Graphics 201 1

Program to depict a Fish


#include<graphics.h> #include<iostream.h> #include<conio.h> #include<dos.h> #include<stdlib.h> void main() { int gd=DETECT,gm; int x,y,r,c,mx,my; initgraph(&gd,&gm," "); cout<<"enter x, y ,c"; cin>>x>>y>>c; mx=getmaxx(); my=getmaxy(); while(!kbhit())
46

Computer Graphics 201 1


{ if((x>=mx)||(y>=my)) { while((x>0)||(y>0)) { cleardevice(); setcolor(c); ellipse(x,y,0,360,50,20); circle(x-40,y-5,2); line(x+50,y,x+80,y-30); line(x+80,y-30,x+80,y+30); line(x+80,y+30,x+50,y); setfillstyle(1,0); floodfill(x,y,c); x=x-rand()%10; y=y-rand()%10; } } else { while((x<=mx)||(y<=my)) { cleardevice(); setcolor(c);
47

Computer Graphics 201 1


ellipse(x,y,0,360,50,20); circle(x-40,y-5,2); line(x+50,y,x+80,y-30); line(x+80,y-30,x+80,y+30); line(x+80,y+30,x+50,y); setfillstyle(1,0); floodfill(x,y,c); x=x+rand()%10; y=y+rand()%10; } } delay(700); } getch(); closegraph(); }

Output

48

Computer Graphics 201 1

49

Computer Graphics 201 1


Program to depict a Kite
#include<graphics.h> #include<conio.h> #include<iostream.h> #include<dos.h> main() { int gd=DETECT,gm; int x,y; clrscr(); initgraph(&gd,&gm," "); while(!kbhit()) { cleardevice(); setcolor(7); line(300,200,200,300); line(300,200,400,300); line(200,300,300,400); line(300,400,300,200); line(300,400,400,300); line(200,300,400,300); line(300,400,275,425); line(300,400,325,425);
50

Computer Graphics 201 1


line(275,425,325,425); line(300,425,325,450); line(325,450,300,475); line(300,475,100,400); x=x+5; y=y+5; delay(100); } getch(); closegraph(); }

Output

51

Computer Graphics 201 1


Program to depict a moving Kite
#include<graphics.h> #include<iostream.h> #include<conio.h> #include<dos.h> #include<stdlib.h> void main() { int gd=DETECT,gm; int x,y,r,c,mx,my; initgraph(&gd,&gm," "); cout<<"enter x, y ,c"; cin>>x>>y>>c; mx=getmaxx(); my=getmaxy(); while(!kbhit()) { if((x>=mx)||(y>=my)) { while((x>0)||(y>0)) { cleardevice(); setcolor(c);
52

Computer Graphics 201 1


line(100,100,50,180); line(100,100,150,180); line(50,180,100,250); line(150,180,100,250); line(100,100,100,250); line(50,180,150,180); line(100,250,70,300); line(100,250,130,300); line(70,300,130,300); line(100,300,120,320); line(120,320,80,340); line(80,340,120,360); line(120,360,80,380); setfillstyle(1,0); floodfill(x,y,c); x=x-rand()%10; y=y-rand()%10; } } else { while((x<=mx)||(y<=my)) { cleardevice();
53

Computer Graphics 201 1


line(100,100,50,180); line(100,100,150,180); line(50,180,100,250); line(150,180,100,250); line(100,100,100,250); line(50,180,150,180); line(100,250,70,300); line(100,250,130,300); line(70,300,130,300); line(100,300,120,320); line(120,320,80,340); line(80,340,120,360); line(120,360,80,380); setcolor(c); setfillstyle(1,0); floodfill(x,y,c); x=x+rand()%10; y=y+rand()%10; } } delay(10); } getch(); closegraph();
54

Computer Graphics 201 1


}

Program to show a Man


#include<iostream.h> #include<conio.h> #include<graphics.h> #include<dos.h> #include<stdlib.h> #include<math.h> void main() { int gd=DETECT,gm; initgraph(&gd,&gm," "); int x,y,mx,my; cout<<"Enter X,Y"; cin>>x>>y; mx=getmaxx(); my=getmaxy(); while(!kbhit()) { if((x>=mx)||(y>=my)) {
55

Computer Graphics 201 1


while((x>=mx)||(y>=my)) { cleardevice(); setcolor(7); circle(x,y,30); line(x,y+30,x,y+100); line(x,y+100,x-50,y+200); line(x,y+100,x+50,y+200); line(x,y+50,x+50,y+50); line(x+50,y+50,x+75,y+25); line(x-50,y+200,x-75,y+175); line(x+50,y+200,x+75,y+175); circle(x-15,y-10,1); circle(x+15,y-10,1); line(x,y-5,x,y+5); line(x+75,y+25,x+75,y); circle(x+75,y-20,20); setfillstyle(1,rand()%15); floodfill(x+75,y-20,7); delay(700); arc(x,y+20,0,180,5); x=x-rand()%10; y=y+rand()%10; }
56

Computer Graphics 201 1


} else {

while((x<=mx)||(y<=my)) { cleardevice(); setcolor(7); circle(x,y,30); line(x,y+30,x,y+100); line(x,y+100,x-50,y+200); line(x,y+100,x+50,y+200); line(x,y+50,x+50,y+50); line(x+50,y+50,x+75,y+25); line(x-50,y+200,x-75,y+175); line(x+50,y+200,x+75,y+175); circle(x-15,y-10,1); circle(x+15,y-10,1); line(x,y-5,x,y+5); line(x+75,y+25,x+75,y); circle(x+75,y-20,20);
57

Computer Graphics 201 1


setfillstyle(1,rand()%15); floodfill(x+75,y-20,7); delay(700); arc(x,y+20,0,180,5); x=x+rand()%10; y=y-rand()%10; } } } getch(); closegraph(); }

Output

58

Computer Graphics 201 1

59

You might also like