Sei sulla pagina 1di 25

1

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 mainlyinterested in the generation of images of 3-dimensional scenes. Computer imagery
has applications for filmspecial effects, simulation and training, games, medical imagery,
flying logos, etc.

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,
2

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.

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
3

 Computer simulation

 Digital art

 Education

 Graphic design

 Infographics

 Information visualization

 Rational drug design

 Scientific visualization

 Video Games

 Virtual reality

 Web design
4

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 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
5

1.Direct Method to drawing a line.

#include<iostream.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int x1,x2,y1,y2,dy,dx,c,m;
int gd=DETECT,gm;
initgraph(&gd,&gm,"");
clrscr();
cout<<"Enter the values as starting and ending points";
cin>>x1>>y1>>x2>>y2;
dy=y2-y1;
dx=x2-x1;
m=dy/dx;
c=y1-(m*x1);
while(x1<=x2)
{
if(m<=1)
{
x1++;
y1=m*x1+c;
putpixel(x1,y1,WHITE);
}
else
{
y1++;
x1=(y1+c)/m;
putpixel(x1,y1,WHITE);
}
}
getch();
}

Output
6

2.Program to draw a line using Digital differential Analyzer.


#include<iostream.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int x1,x2,y1,y2,dy,dx,m=0;
int gd=DETECT,gm;
initgraph(&gd,&gm,"");
clrscr();
cout<<"Enter the values as starting and ending points";
cin>>x1>>y1>>x2>>y2;
dy=y2-y1;
dx=x2-x1;

m=dy/dx;

while(x1<=x2)
{
if(m<=1)
{
7

x1++;
y1=y1+m;
putpixel(x1,y1,WHITE);
}
else
{
y1++;
x1=x1+(1/m);
putpixel(x1,y1,WHITE);
}
}
getch();
}
Output
8

3.Draw a line using Bresenhan Line Algorithm.

#include<iostream.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int x1,x2,y1,y2,dy,dx,f;
int gd=DETECT,gm;
initgraph(&gd,&gm,"");
clrscr();
cout<<"Enter the values as starting and ending points";
cin>>x1>>y1>>x2>>y2;
dy=y2-y1;
dx=x2-x1;
f=(2*dy)-dx;
while(x1<=x2)
{
if(f<0)
{
f=f+(2*dy);
x1++;
putpixel(x1,y1,WHITE);
}
else
{
f=f+(2*dy)-(2*dx);
x1++;
y1++;
putpixel(x1,y1,WHITE);
}
}
getch();
}
Output
9

4.Bresenhan circle algorithm.


# include<iostream.h>
# include<conio.h>
# include<graphics.h>
void main()
{
clrscr();
int xc,yc,r,f,fe,fse,x,y;
int gd=DETECT,gm;
initgraph(&gd,&gm,"");
cout<<"\nEnter the value of x-center,y-center and radius : ";
cin>>xc>>yc>>r;
x=0;
y=r;
f=3-(2*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);
putpixel(-x+xc,-y+yc, WHITE);
putpixel(-y+xc,-x+yc, WHITE);
putpixel(x+xc,-y+yc, WHITE);
putpixel(y+xc,-x+yc, WHITE);
if(f<0)
{
fe=4*x+6;
f=f+fe;
x++;

}
else if(f>0)
{
fse=10+(4*x)-(4*y);
f=f+fse;
x++;
y--;
}
}
getch();
}
10

Output

5.Draw
circle
using Polar co-ordinate
# include<iostream.h>
# include<conio.h>
# include<graphics.h>
# include<math.h>
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"");
float xc,yc,r,c,s;
float x,y,theta;
cout<<"\nEnter the value of x-center,y-center and radius : ";
cin>>xc>>yc>>r;
x=0;
y=r;
theta=1/r;
c=cos(theta);
s=sin(theta);
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);
putpixel(-x+xc,-y+yc, WHITE);
11

putpixel(-y+xc,-x+yc, WHITE);
putpixel(x+xc,-y+yc, WHITE);
putpixel(y+xc,-x+yc, WHITE);
float temp=x;
x=x*c-y*s;
y=y*c+temp*s;
}
getch();
}

Output
12

6.Draw a circle by using mid point circle algorithm

# include<iostream.h>
# include<conio.h>
# include<graphics.h>
void main()
{
clrscr();
int xc,yc,r,f,fe,fse,x,y;
int gd=DETECT,gm;
initgraph(&gd,&gm,"");
cout<<"\nEnter the value of x-center,y-center and radius : ";
cin>>xc>>yc>>r;
x=0;
y=r;
f=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);
putpixel(-x+xc,-y+yc, WHITE);
putpixel(-y+xc,-x+yc, WHITE);
putpixel(x+xc,-y+yc, WHITE);
putpixel(y+xc,-x+yc, WHITE);
if(f<0)
{
fe=2*x+3;
f=f+fe;
x++;
}
else if(f>0)
{
fse=5+(2*x)-(2*y);
f=f+fse;
x++;
y--;
}
}
getch();
}
13

Output
14

7.Draw a ellipse by using Polar ellipse algorithm


#include<iostream.h>

#include<conio.h>

#include<graphics.h>

#include<math.h>

void main()

int gd=DETECT,gm;

initgraph(&gd,&gm,"");

float xc,yc,rx,ry,t1,t2,x1,y1,x2;

cout<<”\n\t\t\t\t--------------------------------------”;

cout<<"\nenter the value xc,yc,x radius,y radius\n" ;

cin>>xc>>yc>>rx>>ry;

t1=0;

t2=90;

while(t1<=t2)

y1=ry*sin(t1);

x1=rx*cos(t1);

putpixel(x1+xc,y1+yc,WHITE);

putpixel(x1+xc,-y1+yc,WHITE);
15

putpixel(-x1+xc,-y1+yc,WHITE);

putpixel(-x1+xc,y1+yc,WHITE);

t1=t1+1;

getch();

closegraph();

Output
16

8.Draw a ellipse by using direct ellipse algorithm

#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"");
float xc,yc,xr,yr;
cout<<" Enter the value of center and xradius y radius ";
cin>>xc>>yc>>xr>>yr;
int x1=0;
int x2=xr;
while(x1<=x2)
{
putpixel(x1+xc,y+yc,WHITE);
putpixel(-x1+xc,y+yc,WHITE);
putpixel(-x1+xc,-y+yc,WHITE);
putpixel(x1+xc,-y+yc,WHITE);
float y=sqrt(1-((x1*x1)/(xr*xr)))*yr;
x1+=1;
}
getch();
}

Output
17

9. Draw a ellipse by using mid point ellipse algorithm.

#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 xcenter,ycenter,xradius & yradius";

cin>>xc>>yc>>xr>>yr;

x=0;

y=yr;

f=(yr*yr)+(xr*xr)/4-(xr*xr)*yr;
18

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);

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);

}
19

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);
20

putpixel(x+xc,-y+yc,WHITE);

getch();

}
21

Static Application(Hut)
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm," ");
setcolor(GREEN);
line(60,10,20,100);
line(60,10,100,100);
line(20,100,100,100);
line(60,10,290,10);
line(290,10,320,100);
line(100,100,320,100);
line(20,100,20,340);
line(100,100,100,340);
line(20,340,100,340);
line(320,100,320,340);
line(100,340,320,340);
setfillstyle(SOLID_FILL,YELLOW);
floodfill(319,339,GREEN);
floodfill(61,291,GREEN);
floodfill(61,11,GREEN);
floodfill(21,99,GREEN);
setcolor(GREEN);
line(40,250,40,340);
line(80,250,80,340);
line(40,250,80,250);
setcolor(BROWN);
circle(60,55,10);
line(140,200,140,240);
line(140,200,265,200);
line(265,200,265,240);
line(140,240,265,240);
setfillstyle(HATCH_FILL,BROWN);
floodfill(41,251,GREEN);
setfillstyle(SOLID_FILL,BROWN);
floodfill(61,56,BROWN);
floodfill(141,201,BROWN);
getch();
}
22

Output:
23

Dynamic application(Cart):
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<stdlib.h>
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm," ");
int m=5;
while(!kbhit())
{
circle(150+m,300,50);
circle(150+m,300,15);
circle(450+m,300,50);
circle(450+m,300,15);
line(100+m,250,500+m,250);
line(100+m,300,135+m,300);
line(150+m,250,150+m,285);
line(165+m,300,200+m,300);
line(150+m,315,150+m,350);
line(400+m,300,435+m,300);
line(450+m,250,450+m,285);
line(465+m,300,500+m,300);
line(450+m,315,450+m,350);
line(450+m,250,575+m,150);
line(500+m,150,620+m,150);
delay(150);
cleardevice();
m+=5;
}
getch();
}
24

Output:

Dynamic application(smilly face)


#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<stdlib.h>
int main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm," ");
int m=5;
while(!kbhit())
{
setcolor(YELLOW);
25

circle(200+m,200,90);
setfillstyle(SOLID_FILL,YELLOW);
floodfill(201+m,201,YELLOW);
setcolor(WHITE);
circle(150+m,165,15);
circle(250+m,165,15);
setfillstyle(SOLID_FILL,BLACK);
floodfill(151+m,166,WHITE);
floodfill(251+m,166,WHITE);
setcolor(GREEN);
line(200+m,155,200+m,175);
setcolor(RED);
arc(200+m,235,180,0,15);
delay(150);
cleardevice();
m+=5;
}
getch();
}
Output:

Potrebbero piacerti anche