graphics file
DESCRIPTION
TRANSCRIPT
INTRODUCTION TO GRAPHICS
Computer graphics are graphics created using computers and, more generally,
the representation and manipulation of image data by a computer with help from
specialized software and hardware. 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 is 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
Although computer graphics is a vast field that encompasses almost any graphical
aspect, we are mainly interested in the generation of images of 3-dimensional
scenes. Computer imagery has applications for film special effects, simulation and
training, games, medical imagery, flying logos, etc.
1 | P a g e
Concepts and Principles
1. Image
In common usage, an image or picture is an artifact, usually two-dimensional, that has a similar appearance to some subject—usually a physical object or a person. Images may be two-dimensional, such as a photograph, screen display, and as well as a three-dimensional, such as a statue. They may be captured by optical devices—such as cameras, mirrors, lenses, telescopes, microscopes, etc. and natural objects and phenomena, such as the human eye or water surfaces.
2. Pixel
Each pixel is a sample of an original image, where more samples typically provide a more accurate representation of the original. The intensity of each pixel is variable; in color systems, each pixel has typically three or four components such as red, green, and blue, or cyan, magenta, yellow, and black.
3. Graphics
Graphics are visual presentations on some surface, such as a wall, canvas, computer screen, paper, or stone to brand, inform, illustrate, or entertain.
Examples are photographs , drawings, Line Art, graphs, diagrams, typography, numbers, symbols, geometric designs, maps, engineering drawings, or other images. Graphics often combine text, illustration, and color.
4. Rendering
Rendering is the process of generating an image from a model, by means of computer programs. The model is a description of three dimensional objects in a strictly defined language or data structure. It would contain geometry, viewpoint, texture, lighting, and shading information.
2 | P a g e
APPLICATION OF GRAPHICS
Computers have become a powerful tool for the rapid and economical production of pictures. Advances in computer technology have made interactive computer graphics a practical tool. Today, computer graphics is used in the areas as science, engineering, medicine, business, industry, government, art, entertainment, advertising, education, and training.
Computer-aided design Computer simulation Digital art Education Graphic design Info graphics Information visualization Rational drug design Scientific visualization Video Games Virtual reality Web design
1. COMPUTER AIDED DESIGN
A major use of computer graphics is in design processes, particularly for
engineering and architectural systems. For some design applications; objects are
first displayed in a wireframe outline form that shows the overall sham and internal
features of objects. Software packages for CAD applications typically provide the
designer with a multi-window environment. Each window can show enlarged
sections or different views of objects. Standard shapes for electrical, electronic,
and logic circuits are often supplied by the design package. The connections
between the components have been mad automatically. Animations are often used
in CAD applications. When object designs are complete, realistic lighting models
and surface rendering are applied. Manufacturing process of object can also be
3 | P a g e
controlled through CAD. Interactive graphics methods are used to layout the
buildings. Three-dimensional interior layouts and lighting also provided. With
virtual-reality systems, the designers can go for a simulated walk inside the
building.
2. PRESENTATION GRAPHICS
It is used to produce illustrations for reports or to generate slide for with
projections. Examples of presentation graphics are bar charts, line graphs, surface
graphs, pie charts and displays showing relationships between parameters. 3-D
graphics can provide more attraction to the presentation.
3. COMPUTER ART
Computer graphics methods are widely used in both fine are and commercial art
applications. The artist uses a combination of 3D modeling packages, texture
mapping, drawing programs and CAD software. Pen plotter with specially
designed software can create “automatic art”. “Mathematical Art” can be produced
using mathematical functions, fractal procedures. These methods are also applied
in commercial art. Animations are also used frequently in advertising, and
television commercials are produced frame by frame. Film animations require 24
frames for each second in the animation sequence. A common graphics method
employed in many commercials is morphing, where one object is transformed into
another.
4 | P a g e
4. ENTERTAINMENT
CG methods are now commonly used in making motion pictures, music videos and
television shows.Many TV series regularly employ computer graphics method.
Graphics objects can be combined with a live action.
5. EDUCATION AND TRAINING
Computer-generated models of physical, financial and economic systems are often
used as educational aids. For some training applications, special systems are
designed. Eg. Training of ship captains, aircraft pilots etc., Some simulators have
no video screens, but most simulators provide graphics screen for visual operation.
Some of them provide only the control panel.
6. VISUALIZATION
The numerical and scientific data are converted to a visual form for analysis and to
study the behavior called visualization. Producing graphical representation for
scientific data sets are calls scientific visualization. And business visualization is
used to represent the data sets related to commerce and industry. The visualization
can be either 2D or 3D.
7. IMAGE PROCESSING
Computer graphics is used to create a picture. Image processing applies techniques
to modify or interpret existing pictures. To apply image processing methods, the
image must be digitized first. Medical applications also make extensive use of
image processing techniques for picture enhancements, simulations of operations,
etc.
5 | P a g e
8. GRAPHICAL USER INTERFACE
Nowadays software packages provide graphics user interface (GUI) for the user to
work easily. A major component in GUI is a window. Multiple windows can be
opened at a time. To activate any one of the window, the user needs just to check
on that window. Menus and icons are used for fast selection of processing
operations.
SOME INBUILT GRAPHICS FUNCTION
1. Initgraph( )
It is use to initialize the graph. Initgraph initializes the graphics system by loading a graphics driver from disk and putting the system into graphics mode.
To start the graphics system, first call the initgraph function. Initgraph loads the graphics driver and puts the system into graphics mode.
Syntax:-
#include <graphics.h>
void initgraph(int *graphdriver, int *graphmode, char *path driver);
2. Closegraph( )
It is used to close the graph . Closegraph deallocates all memory allocated by the graphics system, then restores the screen to the mode it was in before you called initgraph
Syntax:-
#include <graphics.h>
void closegraph(int wid=ALL_WINDOWS);
6 | P a g e
3. Line( )
The line statement we use in C programming for graphics and it is a part of GRAPHICS.H header file
Syntax:-
line(x1, y1, x2, y2)
4. Arc( )
The arc statement we use in C programming for draw curve on The screen and it is a part of GRAPHICS.H
Syntax:-
arc (midx, midy, stangle, endangle, radius);
5. Circle( )
Circle function in computer graphics give value center of the circle and then its radius.
Syntax:-
circle(xcentre,ycentre,radius);
6. getmaxx( )
getmaxx() function is used to get the maximum x value of graphics screen, usually it is 639 for VGA driver and VGAHI graphics mode. getmaxy is used to get the maximum y value of graphics screen.
Syntax:-
rectangle(0,0,getmaxx(),getmaxy())
7 | P a g e
7. Floodfill ( )
The floodfill is use to paint the screen or specific areawithin boundaries through a point , i.e.-e in Circle,Ellipse,Box, or any kind of shape which is fully locked area or two dimensional .
Syntax:-
floodfill (int x, int y, int border)
8. Setfillstyle ( )
It is used to choose the style and color .
Syntax:
setsfillstyle( int pattern, int color)
9. Setcolor ( )
It can be sets the current drawing color.
Syntax:-
Setcolor(int color);
10. cleardevice ( )
Cleardevice erases (that is, fills with the current background color) the entire graphics screen and moves the CP (current position) to home (0,0).
Syntax
void cleardevice(void);
8 | P a g e
11. delay ( )
Delay function is used to suspend execution of a program for a particular time.
Syntax :- void delay(unsigned int);
12.Outtextxy ( )
outtextxy displays a text string in the viewport at the given position (x, y), using the current justification settings and the current font, direction, and size. To maintain code compatibility when using several fonts, use textwidth and textheight to determine the dimensions of the string. If a string is printed with the default font using outtext or outtextxy, any part of the string that extends outside the current viewport is truncated. outtextxy is for use in graphics mode; it will not work in text mode.
Syntax:-
void outtextxy(int x, int y, char *textstring);
13.Rectangle ( )
This sets up a new Rectangle object called recOne. In between the round brackets of Rectangle, we have two sets of numbers. The first set (150, 10) is how far to the left you want your rectangle, and how far down. The second set (50, 200) is the width of the rectangle, and then the height.
Syntax:-
Rectangle recOne = new Rectangle( 150, 10, 50, 200 );
14.Ellipse ( )
It is used to draw the ellipse.
Syntax :-
ellipse(xcentre, ycentre, StAngle, EndAngle, xRadius, Yradius);
9 | P a g e
15.Setbkcolor ( )
setbkcolor function changes current background color e.g.setbkcolor(YELLLOW) changes the current background color to YELLOW.Remember that default drawing color is WHITE and background color is BLACK.
Syntax :-
void setbkcolor(int color);
16.Getmaxy ( )
getmaxy function returns the maximum Y coordinate for current graphics mode and driver.
Syntax :-
int getmaxy();
10 | P a g e
1. Program to show the ‘DIRECT METHOD” for drawing a line.
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
void main()
{
clrscr();
int gd=DETECT, gm;
int x1,y1,x2,y2,m=0,c=0;
initgraph(&gd,&gm,"");
cout<<"Enter the value of starting and ending points";
cin>>x1>>y1>>x2>>y2;
m= (y2-y1)/(x2-x1);
c=y1-(m*x1);
while(x1<=x2)
{
if(m<=1)
{
y1=(m*x1)+c;
x1++;
putpixel(x1,y1,RED);
}
else
11 | P a g e
{
x1=(y1-c)/m;
y1++;
putpixel(x1,y1,RED);
}
}
getch();
closegraph();
}
OUTPUT:
12 | P a g e
2. Program to show “DIGITAL DIFFERENTIAL ANALYZER”Algorithm.
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
void main()
{
clrscr();
int gd=DETECT, gm;
int x1,y1,x2,y2,m=0,c=0;
initgraph(&gd,&gm,"");
cout<<"Enter the value of x1,y1,x2,y2";
cin>>x1>>y1>>x2>>y2;
m=(y2-y1)/(x2-x1);
c=y1-(m*x1);
while(x1<=x2)
{
if(m<=1)
{
x1=x1+1;
y1=y1+m;
putpixel(x1,y1,RED);
}
else
13 | P a g e
{
y1=y1+1;
x1=x1+1/m;
putpixel(x1,y1,RED);
}
}
getch();
closegraph();
}
OUTPUT:
14 | P a g e
3. Program to show “BRESENHAM LINE” Algorithm.
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
void main()
{
clrscr();
int gd=DETECT, gm;
int x1,y1,x2,y2,dy,dx,f,fe,fne;
initgraph(&gd,&gm,"");
cout<<"Enter the value of x1 and y1";
cin>>x1>>y1;
cout<<"Enter the value of x2 and y2";
cin>>x2>>y2;
dy=y2-y1;
dx=x2-x1;
f=2dy-dx;
fe=2dy;
fne=2dy-2dx;
while(x1<=x2)
{
if(f<=0)
{
15 | P a g e
f=f+fe;
x1++;
putpixel(x1,y1,WHITE);
}
else
{
f=f+fne;
x1++;
y1++;
putpixel(x1,y1,WHITE);
}
}
getch();
closegraph();
}
OUTPUT:
16 | P a g e
4. Program of “DIRECT METHOD” to draw a circle.
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
void main()
{
clrscr();
int gd=DETECT, gm;
int xc,yc,xend ,r;
initgraph(&gd,&gm,"");
cout<<”Enter x-centre, y-centre and radius”;
cin>>xc>>yc>>r;
int x=0;
xend=r/sqrt(2);
while(x<xend)
{
y=sqrt(r*r-x*x);
putpixel(x+xc, y+yc, WHITE);
putpixel(y+xc, x+yc, RED);
putpixel(y+xc, -x+yc, WHITE);
putpixel(-x+xc, y+yc, RED);
putpixel(-x+xc, -y+yc, WHITE);
putpixel(-y+xc, x+yc, RED);
17 | P a g e
putpixel(x+xc,-y+yc,WHITE);
x++;
}
getch();
closegraph();
}
OUTPUT:
18 | P a g e
5. Program of “POLAR COORDINATES METHOD” to draw a circle.
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
void main()
{
clrscr();
int gd=DETECT,gm;
int xc,yc,x,y ,r,th;
initgraph(&gd,&gm,"");
cout<<”Enter x-centre, y-centre and radius”;
cin>>xc>>yc>>r;
th=1/r;
x=0;
y=r;
while(x<y)
{
putpixel(x+xc, y+yc, WHITE);
putpixel(y+xc, x+yc, RED);
putpixel(y+xc, -x+yc, WHITE);
putpixel(-x+xc, y+yc, RED);
putpixel(-x+xc, -y+yc, WHITE);
putpixel(-y+xc, x+yc, RED);
19 | P a g e
putpixel(x+xc,-y+yc,WHITE);
float temp=y;
y=y*cos(th)+x*sin(th);
x=x*cos(th)-temp*sin(th);
}
getch();
}
OUTPUT:
6. Program of “MID-POINT METHOD” to draw a circle.
20 | P a g e
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
void main()
{
clrscr();
int gd=DETECT, gm;
int xc,yc,x,y,r,f;
initgraph(&gd,&gm,"");
cout<<”Enter x-centre, y-centre and radius”;
cin>>xc>>yc>>r;
x=0;
y=r;
f=1-r;
while(x<=y)
{
if(f<0)
{
f=f+(3+2*x);
x++;
}
else {
f=f+(2*x-2*y+5)
21 | P a g e
x++;
y--;
}
putpixel(x+xc, y+yc, WHITE);
putpixel(y+xc, x+yc, RED);
putpixel(y+xc, -x+yc, WHITE);
putpixel(-x+xc, y+yc, RED);
putpixel(-x+xc, -y+yc, WHITE);
putpixel(-y+xc, x+yc, RED);
putpixel(x+xc,-y+yc,WHITE);
}
getch();
closegraph();
}
OUTPUT:
7. Program of “BRESENHAM’S ALGORITHM” to draw a circle.
22 | P a g e
# include<iostream.h>
# include<conio.h>
# include<graphics.h>
void cpoint(int,int,int,int);
void main()
{
clrscr();
int xc,yc,r,f,fe,fse,x,y;
int gd=DETECT, gm;
initgraph(&gd,&gm,"");
cout<<"\nEnter x-center,y-center,radius : ";
cin>>xc>>yc>>r;
x=0;
y=r;
f=3-r;
while(x<=y)
{
if(f<0)
{
fe=4*x+6;
f=f+fe;
x++;
cpoint(x,y,xc,yc);
}
else if(f>0)
{fse=10+(4*x)-(4*y);
f=f+fse;
23 | P a g e
x++;
y--;
cpoint(x,y,xc,yc);
}
}
getch();
}
void cpoint(int x,int y, int xc, int yc)
{
putpixel(x+xc,y+yc,1);
putpixel(y+xc,x+yc,1);
putpixel(-x+xc,y+yc,1);
putpixel(-y+xc,x+yc,1);
putpixel(-x+xc,-y+yc,1);
putpixel(-y+xc,-x+yc,1);
putpixel(x+xc,-y+yc,1);
putpixel(y+xc,-x+yc,1);
}
OUTPUT:-
8. Program to draw an Ellipse using Direct Method
24 | P a g e
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
{
int gd,gm;
float xc,yc,xr,yr;
gd=DETECT,gm;
initgraph(&gd,&gm,"");
cout<<"Enter xcentre ,ycentre, xradius,yradius";
cin>>xc>>yc>>xr>>yr;
float x=0;
float y=xr;
while(x<=y)
{
y=yr*sqrt(1-((x*x)/(xr*xr)));
putpixel(x+xc,y+yc,WHITE);
putpixel(-x+xc,y+yc,WHITE);
putpixel(-x+xc,-y+yc,WHITE);
putpixel(x+xc,-y+yc,WHITE);
x++;
}
getch();
}
25 | P a g e
OUTPUT:
9.
Program to draw an Ellipse using Polar Co-ordinates Method
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
{
int gd=DETECT, gm;
clrscr();
initgraph(&gd,&gm," ");
float xc,yc,xr,yr;
cout<<"Enter xc,yc,xr,yr";
cin>>xc>>yc>>xr>>yr;
26 | P a g e
float theta1=0;
float theta2=90;
while(theta1<theta2)
{
float x=xr*cos(theta1);
float 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++;
}
getch();
}
OUTPUT:
10 .Program to draw an Ellipse using Mid Point method.
27 | P a g e
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
{
int gd=DETECT, gm;
initgraph(&gd,&gm,"");
float xc,yc,x,y,xr,yr,f;
cout<<"Enter xc,yc,xr ,yr";
cin>>xc>>yc>>xr>>yr;
x=0;
y=yr;
f=(yr*yr)+(xr*xr)/4-(xr*xr)*yr;
while(2*(yr*yr)*x<=2*(xr*xr)*y)
{
if(f<=0)
{
f=f+(3+(2*x))*(yr*yr);
x++;
putpixel(x+xc,y+yc,WHITE);
putpixel(-x+xc,y+yc,WHITE);
putpixel(-x+xc,-y+yc,WHITE);
putpixel(x+xc,-y+yc,WHITE);
}
else
{
f=f+3*(yr*yr)+2*x*(yr*yr)+2*(xr*xr)-2*y*(xr*xr);
28 | P a g e
x++;
y--;
putpixel(x+xc,y+yc,WHITE);
putpixel(-x+xc,y+yc,WHITE);
putpixel(-x+xc,-y+yc,WHITE);
putpixel(x+xc,-y+yc,WHITE);
}
}
f=(yr*yr)/4-2*(xr*xr)*yr+(xr*xr);
while(y>=0)
{
if(f<=0)
{ f=f+(3-(2*y))*(xr*xr);
x++;
y--;
putpixel(x+xc,y+yc,WHITE);
putpixel(-x+xc,y+yc,WHITE);
putpixel(-x+xc,-y+yc,WHITE);
putpixel(x+xc,-y+yc,WHITE);
}
else
{ f=f+2*(yr*yr)+2*x*(yr*yr)+3*(xr*xr)-2*y*(xr*xr);
y--;
putpixel(x+xc,y+yc,WHITE);
putpixel(-x+xc,y+yc,WHITE);
putpixel(-x+xc,-y+yc,WHITE);
putpixel(x+xc,-y+yc,WHITE);
29 | P a g e
}
}
getch();
closegraph();
}
OUTPUT:
STATIC APPLICATION
11. Program to make a flag.
#include<graphics.h>
#include<conio.h>
#include<iostream.h>
void main()
{
int gd = DETECT,gm;
clrscr();
initgraph(&gd,&gm," ");
setcolor(5);
30 | P a g e
line(20,50,20,180);
line(20,50,100,50);
line(20,70,100,70);
line(20,90,100,90);
line(20,110,100,110);
line(100,50,100,110);
circle(60,80,10);
line(52,80,68,80);
line(60,72,60,88);
setfillstyle(SOLID_FILL,22);
floodfill(21,51,5);
setfillstyle(SOLID_FILL,7);
floodfill(21,75,5);
setfillstyle(SOLID_FILL,2);
floodfill(21,95,5);
setfillstyle(SOLID_FILL,7);
floodfill(81,75,5);
setfillstyle(SOLID_FILL,7);
floodfill(61,81,5);
getch();
closegraph();
}
Output:-
31 | P a g e
DYNAMIC APPLICATIONS
12. Program to show stars and moon on the screen .
#include<iostream.h>
#include<stdlib.h>
#include<graphics.h>
#include<conio.h>
#include<dos.h>
void main()
{
int x,y,i;
int gd=DETECT,gm;
initgraph(&gd,&gm," ");
outtextxy(350,350,"GOOD NIGHT JI");
32 | P a g e
circle(200,200,50);
setfillstyle(1,15);
floodfill(200,200,15);
setcolor(0);
setfillstyle(1,0);
circle(180,180,50);
floodfill(180,180,0);
for (i=0;i<1000;i++)
{
x = random(1500);
y = random(1500);
putpixel(x,y,15);
}
getch();
closegraph();
}
33 | P a g e
Output:-
13. Program to implement an application of circle to show the different patterns and colors.
#include<iostream.h>
#include<stdlib.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<math.h>
void circlepoints(float,float,float,float);
void main()
34 | P a g e
{
int gd=DETECT,gm;
initgraph(&gd,&gm," ");
float x,y,xc,yc,r,xend;
cout<<"enter xc,yc,radius";
cin>>xc>>yc>>r;
x=0;
xend=r/sqrt(2);
while(x<xend)
{
y=sqrt(r*r-x*x);
circlepoints(x,y,xc,yc);
delay(100);
x++;
}
while(!kbhit())
{
setfillstyle(random(10),random(7));
floodfill(xc,yc,7);
}
getch();
closegraph();
void circlepoints(float x,float y,float xc,float yc)
{
35 | P a g e
putpixel(x+xc,y+yc,7);
putpixel(y+xc,x+yc,7);
putpixel(-y+xc,x+yc,7);
putpixel(-x+xc,y+yc,7);
putpixel(-x+xc,-y+yc,7);
putpixel(-y+xc,-x+yc,7);
putpixel(x+xc,-y+yc,7);
putpixel(y+xc,-x+yc,7);
}
Output
36 | P a g e