Complete CG Full Notes (PPT & PDF
Complete CG Full Notes (PPT & PDF
GRAPHICS
(Mr. Gaurav Srivastava)
WHAT IS COMPUTER GRAPHICS ?
Computer graphics is an art of drawing pictures on computer screens
with the help of programming. It involves computations, creation, and
manipulation of data.
In other words, we can say that computer graphics is a rendering tool for
the generation and manipulation of images.
Computer graphics are graphics created by computers and, more
generally, the representation and manipulation of pictorial data by a
Computer Graphics = Data Structures + Graphics Algorithm+ Languages
computer.
1. Data Structure means those data structure that are suitable for
computer graphics.
2. Graphics algorithm for picture generation & transformation.
With computer graphics user can also control the animation speed ,
portion of the view , the goniometric relationship the object in the scene
to one another, the amount of detail shown and on.
The computer graphics provides tool called “motion dynamics”. With this
tool user can move & tumble objects with respect to a stationary
observer, or he can make objects stationary and the viewer moving
around them.
CHARACTERISTICS OF COMPUTER GRAIPHCS
It is computer graphics in which user does not have any kind of control
over the image.
Image is merely the product of static stored program and will work
according to the instructions given in the program linearly.
The image is totally under the control of program instructions not under
the user.
The user can interact with the machine as per his requirements.
2. SCIENTIFIC GRAPHICS:
It includes x y- plots, curve fitting, contour plots, flowcharts etc.
3. SCALED DRAWING :
It includes drawing of buildings , bridges and machines.
APPLICATION OF COMPUTER GRAPHICS
1. COMPUTER-AIDED DESIGN
CAD is used in the design of tools and machinery and in the drafting &
design of all types of building, from small residential types (houses) to
the largest commercial and industrial structures(hospital & factories).
2. PRESENTATION GRAPHICS
It is used to produce illustrations for reports or to generate 35-mm slides
or transparencies for use with projectors.
It is commonly used to summarize financial, statistical, mathematical,
scientific, and economic data for research reports, managerial reports,
consumer information bulletins, and other types of reports.
Workstation devices and service bureaus exist for converting screen
displays into 35-mm slides or overhead transparencies for use in
presentations.
Example:
Bar charts
Line graphs
Pie charts
Time charts
Movie Industry
Used in motion pictures, music
videos, and television shows.
Used in making of cartoon
animation films
aircraft pilots
Interfaces also display menus and icons for fast selection of processing
options or parameter values.
The data files for a vector Its data file contains the
image contains the points information about
from where a path start properties of pixels at
and ends. various locations of
image.
Conceptual Framework of
Computer Graphics
• Interactive Computer Graphics consists of two
major components at two levels:
Hardware Level
Software Level
• HARDWARE LEVEL:
The computer receives input from the interactive
devices and show images on display devices.
• SOFTWARE LEVEL :
Software level involves three components:
Application Program.
Application Model.
Graphics System.
Application Model :
The data files for a vector Its data file contains the
image contains the points information about
from where a path start properties of pixels at
and ends. various locations of
image.
Construction of Cathode Ray Tube
• The primary components are heated metal cathode
and a control grid.
• The heat is supplied to the cathode, by passing
electron through electron gun, through the
filament.
• This way, the electrons get heated up, and start
getting ejected out of cathode filament.
• This stream of negatively charged electron is
accelerated towards the phosphor screen by
supplying high positive voltage.
• This acceleration is produced by means of an
accelerating anode. Next component is Focussing
system.
• Focussing system is used to force the electron
beam to converge to small spot on the screen.
Without it, electrons will get scattered because of
their own repulsions, due to which we wont get a
sharp image.
• The focussing can be done either by means of
electrostatic means or magnetic fields.
• The deflection systems of the CRT consists of two
pairs of parallel plates , referred as vertical and
horizontal deflective plates.
• The voltage applied to vertical plates controls the
vertical deflection, of electron beam & voltage
applied to horizontal deflection plates control the
horizontal deflection of electron .
• As the electron beam strikes the positively charged
phosphorous screen at a specific position, it emits
light and enables screen to be visible.
Cathode-Ray Tubes
The basic operation of a CRT. A beam of electrons (cathode rays) emitted by an electron gun,
passes through focusing and deflection systems that direct the beam toward specified positions on the
phosphor coated screen. Fig.1.
The phosphor then emits a small spot of light at each position contacted by the electron beam.
Because the light emitted by the phosphor fades very rapidly, some method is needed for maintaining
the screen picture. One way to keep the phosphor glowing is to redraw the picture repeatedly by
quickly directing the electron beam back over the same points. This type of display is called a refresh
CRT.
Fig.3
The primary components of an electron gun in a CRT are the heated metal cathode and a control grid
Heat is supplied to the cathode by directing a current through a coil of wire, called the filament, inside
the cylindrical cathode structure. This causes electrons to be boiled off" the hot cathode surface. In the
vacuum inside the CRT envelope, the free, negatively charged electrons are then accelerated toward
the phosphor coating by a high positive voltage. The accelerating voltage can be generated with a
positively charged metal coating on the inside of the CRT envelope near the phosphor screen, or an
accelerating anode can be used, as in Fig.3.
Sometimes the electron gun is built to contain the accelerating anode and focusing system within the
same unit. Intensity of the electron beam is controlled by setting voltage levels on the control grid,
which is a metal cylinder that fits over the cathode. A high negative voltage applied to the control
grid will shut OFF the beam by repelling electrons and stopping them from passing through the small
hole at the end of the control grid structure. A smaller negative voltage on the control grid simply
decreases the number of electrons passing through. Since the amount of light emitted by the phosphor
coating depends on the number of electrons striking the screen,
The focusing system in a CRT is needed to force the electron beam to converge into a small spot as it
strikes the phosphor. Otherwise, the electrons would repel each other, and the beam would spread out
as it approaches the screen. Focusing is accomplished with either electric or magnetic fields.
The distance that the electron beam must travel to different points on the screen varies because the
radius of curvature for most CRTs is greater than the distance from the focusing system to the screen
center. Therefore, the electron beam will be focused properly only at the center to the screen.
As the beam moves to the outer edges of the screen, displayed images become blurred. To compensate
for this, the system can adjust the focusing according to the screen position of the beam.
As with focusing, deflection of the electron beam can be controlled either with electric fields or with
magnetic fields.
Magnetic deflection has two pairs of coils are used, with the coils in each pair mounted on opposite
sides of the neck of the CRT envelope. One pair is mounted on the top and bottom of the neck and the
other pair is mounted on opposite sides of the neck. The magnetic, field produced by each pair of coils
results in a transverse deflection force that is perpendicular both to the direction of the magnetic field
and to the direction of travel of the electron beam. Horizontal deflection is accomplished with one pair
of coils, and vertical deflection by the other pair. The proper deflection amounts are attained by
adjusting the current through the coils.
Electrostatic deflection has two pairs of parallel plates are mounted inside the CRT envelope. One
pair coil plates is mounted horizontally to control the vertical deflection, and the other pair is mounted
vertically to control horizontal deflection (Fig. 3). Spots of light are produced on the screen by the
transfer of the CRT beam energy to the phosphor. When the electrons in the beam collide with the
phosphor coating, they are stopped and then kinetic energy is absorbed by the phosphor. Part of the
beam energy is converted by friction into heat energy, and the remainder causes electrons in the
phosphor atoms to move up to higher quantum-energy levels. After a short time, the "excited
phosphor electrons begin dropping back to their stable ground state, giving up their extra energy as
small quantum’s of Light energy. The frequency (or color) of the light emitted
by the phosphor is proportional to the energy difference between the excited quantum state and the
ground state. Different kinds of phosphors are available for use in a CRT.
Persistence is defined as “the time it takes the emitted light from the screen to decay to one- tenth of
its original intensity”. Lower persistence phosphors require higher refresh rates to maintain a picture
on the screen without flicker. It is useful for animation High-persistence phosphor is useful for
displaying highly complex, static pictures. Although some phosphors have persistence greater than 1
second, graphics monitors are usually constructed with persistence in the range from 10 to 60
microseconds.
Resolution: The maximum number of points that can be displayed without overlap on a CRT is
referred to as the resolution.
Resolution is the number of points per centimeter that can be plotted horizontally and vertically,
although it is often simply stated as the total number of points in each direction.
Spot intensity has a Gaussian distribution, so two adjacent spots will appear distinct as long as their
separation is greater than the diameter at which each spot has an intensity of about 60 percent of that
at the center of the spot.
Typical resolution on high-quality systems is 1280 by 1024, with higher resolutions available on many
systems. High resolution systems are often referred to as high-definition systems. The physical size of
a graphics monitor is given as the length of the screen diagonal, with sizes varying from about 12
inches to 27 inches or more.
Aspect Ratio: This number gives the ratio of vertical points to horizontal points necessary to produce
equal-length lines in both directions on the screen. (Sometimes aspect ratio is stated in terms of the
ratio of horizontal to vertical points.) An aspect ratio of 3/4 means that a vertical line plotted with
three points has the same length as a horizontal line plotted with four points.
INPUT DEVICES:
Various devices are available for data input on graphics workstations. Most
systems have a keyboardand one or more additional devices include a mouse,
trackball, spaceball, joystick, digitizers, dials, andbutton boxes. Some other input
devices used in particular applications are data gloves, touch panels, image
scanners, and voice systems.
Keyboards:
Mouse:
A Trackball is a ball that can be rotated withthe fingers or palm of the hand, to
produce screencursormovement. Trackballs are often mounted onkeyboards and it
is a two-dimensional positioning device.A Spaceball provides six degrees of
freedom. Unlike the trackball, a spaceball does not actually move. Spaceballs are
used for threedimensional positioning and selection operations in
virtual-reality systems, modeling, animation, CAD, and other applications.
Trackall Spaceball
Joysticks:
Data Glove:
A Data Glove that can be used to grasp a "virtual" object.The glove is constructed
with a series of sensors that detect handand finger motions. Electromagnetic
coupling betweentransmitting antennas and receiving antennas is used to provide
information about the position and orientation of the hand. A twodimensional
projection of the scene can be viewed on a video monitor, or a three-dimensional
projection can be viewed with a headset.
Digitizers:
Image Scanners:
Drawings, graphs, color and black-and-white photos, or text can be stored for
computer processingwith an Image Scanner by passing an optical scanning
mechanism over the information to be stored. Wecan also apply various
transformations, image-processing methods to modify the array representation of
the picture.
Touch Panels:
Touch Panels allow displayed objects or screen positions to be selected with the
touch of a finger. Atypical application of touch panels is for the selection of
processing options that are represented with graphical icons.
Touch input can be recorded using optical, electrical, or acoustical methods.
Light Pens
Light Pen is a pencil-shaped device used to select screen positions by detecting the
light coming from points on the CRT screen. Light Pens sometimes give false
readings due to background lighting in a room. The pen will send a pulse whenever
phosphor below it is illuminated. While the image on a refresh display may appear
to be stable, it is in fact blinking on and off faster than the eye can detect. This
blinking is not too fast, for the light pen. The light pen as easily determines the
time at which the phosphor is illuminated. Since there is only one electron beam on
the refresh display, only one line segment can be drawn at a time and no two
segments are drawn simultaneously.
Voice Systems
HARD-COPY DEVICES:
We can obtain hard-copy output for various images in several formats. The most
important output device is Printer or Plotter and many types are there.
_ Impact Printers press formed character faces against an inked ribbon onto the
paper. E.g. : Line Printer, with the typefaces mounted on bands, chains, drums, or
wheels.
_ Character impact printers often have a Dot-Matrix print head containing a
rectangular array of
protruding wire pins, with the number of pins depending on the quality of the
printer.
_ Nonimpact Printers and plotters use laser techniques, ink-jet sprays, xerographic
processes
(photocopying), electrostatic methods, and electrothermal methods to get images
onto Paper.
_ In a Laser Device, a laser beam mates a charge distribution on a rotating drum
coated with a
photoelectric material, such as selenium. Toner is applied to the drum and then
transferred to paper.
_ Ink-Jet methods produce output by squirting ink in horizontal rows across a roll
of paper wrapped on a drum. The electrically charged ink stream is deflected by an
electric field toproduce dot-matrix patterns.
Laser Printer Inkjet Printer
_ An Electrostatic Device places a negative charge on the paper, one complete row
at a time along the length of the paper. Then the paper is exposed to a toner. The
toner is positively charged and so is attracted to the negatively charged areas,
where it adheres to produce the specified output.
_ A Pen Plotter has one or more pens mounted on a carriage, or crossbar, that
spans a sheet of paper.
Pens with varying colors and widths, wet-ink, ball-point, and felt-tip pens are all
possible choices for use with a pen plotter. Crossbars can be either moveable or
stationary, while the pen moves back and forth along the bar. E.g.: flatbed pen
plotter, rollfeed pen plotter.
Program for Cohen Sutherland Line Clipping Algorithm in C
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<graphics.h>
#include<dos.h>
int x,y;
char code[4];
}PT;
void drawwindow();
PT setcode(PT p);
void main()
int gd=DETECT,v,gm;
PT p1,p2,p3,p4,ptemp;
scanf("%d %d",&p2.x,&p2.y);
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
drawwindow();
delay(500);
drawline(p1,p2);
delay(500);
cleardevice();
delay(500);
p1=setcode(p1);
p2=setcode(p2);
v=visibility(p1,p2);
delay(500);
switch(v)
case 0: drawwindow();
delay(500);
drawline(p1,p2);
break;
case 1: drawwindow();
delay(500);
break;
case 2: p3=resetendpt(p1,p2);
p4=resetendpt(p2,p1);
drawwindow();
delay(500);
drawline(p3,p4);
break;
delay(5000);
closegraph();
void drawwindow()
line(150,100,450,100);
line(450,100,450,350);
line(450,350,150,350);
line(150,350,150,100);
}
void drawline(PT p1,PT p2)
line(p1.x,p1.y,p2.x,p2.y);
PT ptemp;
if(p.y<100)
ptemp.code[0]='1'; //Top
else
ptemp.code[0]='0';
if(p.y>350)
ptemp.code[1]='1'; //Bottom
else
ptemp.code[1]='0';
if(p.x>450)
ptemp.code[2]='1'; //Right
else
ptemp.code[2]='0';
if(p.x<150)
ptemp.code[3]='1'; //Left
else
ptemp.code[3]='0';
ptemp.x=p.x;
ptemp.y=p.y;
return(ptemp);
int i,flag=0;
for(i=0;i<4;i++)
if((p1.code[i]!='0') || (p2.code[i]!='0'))
flag=1;
if(flag==0)
return(0);
for(i=0;i<4;i++)
{
flag='0';
if(flag==0)
return(1);
return(2);
PT temp;
int x,y,i;
float m,k;
if(p1.code[3]=='1')
x=150;
if(p1.code[2]=='1')
x=450;
if((p1.code[3]=='1') || (p1.code[2]=='1'))
{
m=(float)(p2.y-p1.y)/(p2.x-p1.x);
k=(p1.y+(m*(x-p1.x)));
temp.y=k;
temp.x=x;
for(i=0;i<4;i++)
temp.code[i]=p1.code[i];
return (temp);
if(p1.code[0]=='1')
y=100;
if(p1.code[1]=='1')
y=350;
if((p1.code[0]=='1') || (p1.code[1]=='1'))
m=(float)(p2.y-p1.y)/(p2.x-p1.x);
k=(float)p1.x+(float)(y-p1.y)/m;
temp.x=k;
temp.y=y;
for(i=0;i<4;i++)
temp.code[i]=p1.code[i];
return(temp);
else
return(p1);
Output
Before Clipping
After Clipping
Polygon Clipping (Sutherland Hodgman Algorithm)
A polygon can also be clipped by specifying the clipping window. Sutherland Hodgeman
polygon clipping algorithm is used for polygon clipping. In this algorithm, all the vertices of the
polygon are clipped against each edge of the clipping window.
First the polygon is clipped against the left edge of the polygon window to get new vertices of
the polygon. These new vertices are used to clip the polygon against right edge, top edge, bottom
edge, of the clipping window as shown in the following figure.
While processing an edge of a polygon with clipping window, an intersection point is found if
edge is not completely inside clipping window and the a partial edge from the intersection point
to the outside edge is clipped. The following figures show left, right, top and bottom edge
clippings −
Text Clipping
Various techniques are used to provide text clipping in a computer graphics. It depends on the
methods used to generate characters and the requirements of a particular application. There are
three methods for text clipping which are listed below −
This clipping method is based on characters rather than entire string. In this method if the string
is entirely inside the clipping window, then we keep it. If it is partially outside the window, then
−
Step 5. Compare the vertices of each edge of the polygon, individually with the
clipping plane.
Step 6. Save the resulting intersections and vertices in the new list of vertices
according to four possible relationships between the edge and the clipping
boundary discussed earlier .
Step 7. Repeat the steps 5 and 6 for remaining edges of the clipping window. Each
time the resultant list of vertices is successively passed to process the next edge
of the clipping window.
ALGORITHM TO CLIP A LINE.
1. Start.
2. Initialize the graphic system using initgraph function.
3. Get the input of window co ordinates from the user and draw a window.
4. Get the input of line co ordinates from user and draw the line.
5. Calculate the region code of each end point of line using relation given in steps 6 to step
6. Let (x,y) be the co ordinates of end point of line and (xmin,ymin), (xmax,ymax) be co
ordinates of world window
7. If y –ymax = +ve
8. MSB region code = 1.
9. Else MSB region code = 0.
10. If ymin – y = +ve
11. Region code = 1.
12. Else Region code = 0.
13. If x – xmax = +ve
14. Region code = 1.
15. Else Region code = 0.
16. If xmin – x = +ve
17. LSB Region code = 1.
18. Else LSB Region code = 0.
19. Calculate region code of both end points.
20. Logically and both region code.
21. If Logically anded result is = 0
22. Line is not a clipping candidate.
23. Else.
24. Line is a clipping candidate.
25. Calculate slope of line using formula slope=(y2-y1)/(x2-x1).
26. If line is to be horizontally clipped.
27. New y = ymin or ymax.
28. New x = x1 + ((new y - y1)/slope).
29. If line is to be vertically clipped.
30. New x = xmin or xmax.
31. New y = y1+slope*(new x –x1).
32. Clip the lines from these intersection points.
33. Display the new line.
34. Close the graphic system.
35. Stop.
WAP TO SHOW LINE CLIPPING.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
void storepoints(int,int,int,int,int,int,int[]);
void main()
{
int gdriver=DETECT,gmode;
int x1,x2,y1,y2,xmax,ymax,xmin,ymin,a[10],b[10],xi1,xi2,yi1,yi2,flag=0; float m;
int i;
clrscr();
printf("output");
printf("\n");
printf("enter the value of x1,y1,x2,y2: >");
scanf("%d%d%d%d",&x1,&y1,&x2,&y2); printf("enter the
value of xmax,ymax,xmin,ymin:");
scanf("%d%d%d%d",&xmax,&ymax,&xmin,&ymin);
storepoints(x2,y2,ymin,ymax,xmax,xmin,b);
for(i=1;i<=4;i++)
{
if(a[i]*b[i]==0)
flag=1;
else
flag=0;
}
if(flag==1)
{
m=(y2-y1)/(x2-x1);
xi1=x1;
yi1=y1;
}
if(a[1]==1)
{
yi1=ymax;
xi1=x1+((1/m)*(yi1-y1));
}
else
{
if(a[2]==1)
{
yi1=ymin;
xi1=x1+((1/m)*(yi1-y1));
}
}
if(a[3]==1)
{
xi1=xmax;
yi1=y1+(m*(xi1-x1));
}
if(a[4]==1)
{
xi1=xmin;
yi1=y1+(m*(xi1-x1));
}
else
if(b[1]==1)
{
yi2=ymax;
xi2=x2+((1/m)*(yi2-y2));
}
else
if(b[2]==1)
{
yi2=ymin;
xi2=x2+((1/m)*(yi2-y2));
}
else
if(b[3]==1)
{
xi2=xmax;
yi2=y2+((1/m)*(xi2-x2));
}
else
if(b[4]==1)
{
xi2=xmin;
yi2=y2+(m*(xi2-x2));
}
clrscr();
initgraph(&gdriver,&gmode,"c://tc//bgi:");
rectangle(xmin,ymin,xmax,ymax);
line(x1,y1,x2,y2);
delay(5000);
closegraph();
clrscr();
initgraph(&gdriver,&gmode,"c://tc//bgi:");
line(xi1,yi1,xi2,yi2);
rectangle(xmin,ymin,xmax,ymax);
if(flag==0)
{
printf("\n no clipping is required");
}
getch();
closegraph();
}
void storepoints(int x1,int y1,int ymax,int xmax,int xmin,int ymin,int c[10])
{
if((y1-ymax)>0)
c[1]=1;
else
c[1]=0;
if((ymin-y1)>0)
c[2]=1;
else
c[2]=0;
if((x1-xmax)>0)
c[3]=1;
else
c[3]=0;
if((xmin-x1)>0)
c[4]=1;
else
c[4]=0;
}
OUTPUT
Before Clipping:
After Clipping
/* PROGRAM TO DRAW A LINE USING DDA ALGORITHM */
#include <graphics.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>
dx = x2-x1;
dy = y2-y1;
if(abs(dx) > abs(dy))
steps = abs(dx);
else
steps = abs(dy);
xincr = dx/steps;
yincr = dy/steps;
x = x1;
y = y1;
putpixel(x,y,WHITE);
for(k=1;k<=steps;k++)
{
delay(100);
x = x+xincr;
y = y+yincr;
putpixel(round(x),round(y),WHITE);
}
outtextxy(200,20,"ILLUSTRATION OF DDA ALGORITHM");
outtextxy(x1+5,y1-5,"(x1,y1)");
outtextxy(x2+5,y2+5,"(x2,y2)");
getch( );
closegraph( );
restorecrtmode( );
}
float round(float a)
{
int b = a+0.5;
return(floor(b));
}
OUTPUT :
#include <graphics.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>
void main( )
{
/* request auto detection */
int gdriver = DETECT, gmode ;
int x1,y1,x2,y2,dx,dy,p,k,x,y;
clrscr( );
dx = x2-x1;
dy = y2-y1;
p = 2*dy-dx;
x = x1;
y = y1;
putpixel(x,y,15);
for(k=1;k<=dx;k++)
{
if(p<0)
{
x = x+1;
putpixel(x,y,15);
p = p+2*dy;
}
else
{
x = x+1;
y = y+1;
putpixel(x,y,15);
p = p+2*dy-2*dx;
}
}
outtextxy(200,10,"ILLUSTRATION OF BRESENHAM'S ALGORITHM");
outtextxy(x1+5,y1-5,"(x1,y1)");
outtextxy(x2+5,y2+5,"(x2,y2)");
getch( );
closegraph( );
restorecrtmode( );
}
OUTPUT :
#include <graphics.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>
x = 0;
y = r;
symmetry(xc,yc,x,y);
p = 1-r;
while(x<=y)
{
if(p<0)
{
x = x+1;
p = p+2*x+1;
}
else
{
x = x+1;
y = y-1;
p = p+2*x+1-2*y;
}
symmetry(xc,yc,x,y);
}
outtextxy(120,20,"ILLUSTRATION OF MIDPOINT CIRCLE ALGORITHM");
outtextxy(xc-25,yc,"(xc,yc)");
getch( );
closegraph( );
restorecrtmode( );
}
void symmetry(int xc,int yc,int x,int y)
{
putpixel(xc+x,yc+y,WHITE);
putpixel(xc-x,yc+y,WHITE);
putpixel(xc+x,yc-y,WHITE);
putpixel(xc-x,yc-y,WHITE);
putpixel(xc+y,yc+x,WHITE);
putpixel(xc-y,yc+x,WHITE);
putpixel(xc+y,yc-x,WHITE);
putpixel(xc-y,yc-x,WHITE);
}
OUTPUT:
#include <graphics.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>
x = 0;
y = r;
symmetry(xc,yc,x,y);
s = 3-2*r;
while(x<=y)
{
if(s<0)
{
x = x+1;
s = s+4*x+6;
}
else
{
x = x+1;
y = y-1;
s = s+4*(x-y)+10;
}
symmetry(xc,yc,x,y);
}
outtextxy(120,20,"ILLUSTRATION OF BRESENHAM'S CIRCLE
ALGORITHM");
outtextxy(xc-25,yc,"(xc,yc)");
getch( );
closegraph( );
restorecrtmode( );
}
void symmetry(int xc,int yc,int x,int y)
{
putpixel(xc+x,yc+y,WHITE);
putpixel(xc-x,yc+y,WHITE);
putpixel(xc+x,yc-y,WHITE);
putpixel(xc-x,yc-y,WHITE);
putpixel(xc+y,yc+x,WHITE);
putpixel(xc-y,yc+x,WHITE);
putpixel(xc+y,yc-x,WHITE);
putpixel(xc-y,yc-x,WHITE);
}
OUTPUT :
#include <graphics.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>
x = 0; /*starting point*/
y = b;
symmetry(xc,yc,x,y);
fx = 0; /*initial partial derivatives*/
fy = aa2*y;
p = bb-aa*b+(0.25*aa); /*compute and round off p1*/
#include <graphics.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>
void main( )
{
/* request auto detection */
int gdriver = DETECT, gmode,ch ;
float x1,y1,x2,y2,a,x,y;
switch(ch)
{
case 1: cleardevice( );
/* draw the original line */
outtextxy(250,20,"LINE BEFORE ROTATION");
line(x1,y1,x2,y2);
a = a*(3.14/180);
x1 = x1*cos(a)-y1*sin(a);
y1 = x1*sin(a)+y1*cos(a);
x2 = x2*cos(a)-y2*sin(a);
y2 = x2*sin(a)+y2*cos(a);
getch( );
cleardevice( );
outtextxy(250,20,"LINE AFTER ROTATION");
line(x1,y1,x2,y2);
getch( );
cleardevice( );
break;
default:printf("Invalid choice");
cleardevice( );
}
}while(ch!=0);
getch( );
closegraph( );
restorecrtmode( );
}
OUTPUT:
######### MAIN MENU #########
#include <graphics.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>
void main( )
{
/* request auto detection */
int gdriver = DETECT, gmode ;
int x1,y1,x2,y2,a,b,h,k,ch;
switch(ch)
{
case 1: cleardevice( );
/* draw the original line */
outtextxy(200,20,"LINE BEFORE SCALING");
line(x1,y1,x2,y2);
x1 = x1*a;
y1 = y1*b;
x2 = x2*a;
y2 = y2*b;
getch( );
cleardevice( );
outtextxy(200,20,"LINE AFTER SCALING");
line(x1,y1,x2,y2);
getch( );
cleardevice( );
break;
default:printf("Invalid choice");
cleardevice( );
}
}while(ch!=0);
getch( );
closegraph( );
restorecrtmode( );
}
OUTPUT:
######### MAIN MENU #########
#include <graphics.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>
void main( )
{
/* request auto detection */
int gdriver = DETECT, gmode ;
int x1,y1,x2,y2,a,b;
getch( );
closegraph( );
restorecrtmode( );
}
OUTPUT:
#include <graphics.h>
#include <stdio.h>
#include <conio.h>
void main( )
{
/* request auto detection */
int gdriver = DETECT, gmode;
int x1,y1,x2,y2,a,b;
getch( );
closegraph( );
restorecrtmode( );
}
OUTPUT:
#include <graphics.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>
void main( )
{
/* request auto detection */
int gdriver = DETECT, gmode ;
float x1,y1,x2,y2,x3,y3,x4,y4,m,n,b;
int ch,x,y;
do
{
printf("\n######### MAIN MENU #########\n");
printf("\n1.Reflection about x-axis\n");
printf("2.Reflection about y-axis\n");
printf("3.Reflection about arbitary line\n");
printf("Enter your choice:0 for exit\n");
scanf("%d",&ch);
if(ch==0)
{
getch( );
exit(1);
}
switch(ch)
{
case 1: cleardevice( );
/* draw the original line */
outtextxy(250,10,"LINE BEFORE REFLECTION");
x = getmaxx( );
y = getmaxy( );
line(0,y/2,x,y/2);
line(x/2,0,x/2,y);
line(x1+x/2,y1+y/2,x2+x/2,y2+y/2);
x1 = x1+x/2;
y1 = y1+y/2-2*y1;
x2 = x2+x/2;
y2 = y2+y/2-2*y2;
getch( );
cleardevice( );
outtextxy(200,10,"LINE AFTER REFLECTION");
line(x1,y1,x2,y2);
line(0,y/2,x,y/2);
line(x/2,0,x/2,y);
getch( );
cleardevice( );
break;
case 2: cleardevice( );
/* draw the original line */
outtextxy(200,10,"LINE BEFORE REFLECTION");
x = getmaxx( );
y = getmaxy( );
line(0,y/2,x,y/2);
line(x/2,0,x/2,y);
line(x1+x/2,y1+y/2,x2+x/2,y2+y/2);
x1 = x1+x/2-2*x1;
y1 = y1+y/2;
x2 = x2+x/2-2*x2;
y2 = y2+y/2;
getch( );
cleardevice( );
outtextxy(200,10,"LINE AFTER REFLECTION");
line(x1,y1,x2,y2);
line(0,y/2,x,y/2);
line(x/2,0,x/2,y);
getch( );
cleardevice( );
break;
default:printf("Invalid choice");
cleardevice( );
}
}while(ch!=0);
getch( );
closegraph( );
restorecrtmode( );
}
OUTPUT:
#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);
void main( )
{
float x1,y1,x2,y2;
int gdriver = DETECT, gmode ;
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
void main( )
{
int wxmin,wymin,wxmax,wymax,vxmin,vymin,vxmax,vymax ;
/* request auto detection */
int gdriver = DETECT, gmode , i , n, poly[14],poly1[14];
float sx,sy;
poly[2*n] = poly[0];
poly[2*n+1] = poly[1];
/* draw the polygon */
drawpoly(n+1, poly);
rectangle(wxmin,wymin,wxmax,wymax);
sx = (vxmax-vxmin)/(wxmax-wxmin);
sy = (vymax-vymin)/(wymax-wymin);
for(i=0;i<n;i++)
{
poly1[2*i] = sx*(poly[2*i]-wxmin)+vxmin;
poly1[2*i+1] = sx*(poly[2*i+1]-wymin)+vymin;
}
poly1[2*n] = poly1[0];
poly1[2*n+1] = poly1[1];
rectangle(vxmin,vymin,vxmax,vymax);
outtextxt(150,10,” WINDOW TO VIEWPORT TRANSFORMATION”);
drawpoly(n+1,poly1);
/* clean up */
getch( );
closegraph( );
return 0;
}
OUTPUT:
#include <stdio.h>
#include <graphics.h>
#include <conio.h>
#include <math.h>
#include <process.h>
#define TRUE 1
#define FALSE 0
void main( )
{
float x1,y1,x2,y2;
/* request auto detection */
int gdriver = DETECT, gmode, n,poly[14],i;
clrscr( );
printf("Enter the no of sides of polygon:");
scanf("%d",&n);
printf("\nEnter the coordinates of polygon\n");
for(i=0;i<2*n;i++)
{
scanf("%d",&poly[i]);
}
poly[2*n]=poly[0];
poly[2*n+1]=poly[1];
printf("Enter the rectangular coordinates of clipping window\n");
scanf("%f%f%f%f",&xmin,&ymin,&xmax,&ymax);
OUTPUT:
Enter the no of sides of polygon:5
Enter the coordinates of polygon
50
50
200
100
350
350
80
200
40
80
Enter the rectangular coordinates of clipping window
150
150
300
300
INDEX
1. Program to draw a line : DDA Algorithm.
2. Program to draw a line : Bresenham’s Algorithm.
3. Program to draw a circle : Midpoint Circle Algorithm.
4. Program to draw a circle : Bresenham’s Circle Algorithm.
5. Program to draw an ellipse : Midpoint Ellipse Algorithm.
6. Rotation of line about origin and fixed point.
7. Scaling of line about origin and fixed point.
8. Translation of line.
9. Shearing of line.
10. Reflection of line about X-axis,Y-axis and arbitary line.
11. Program to clip a line : Cohen Sutherland Algorithm.
12. Window to Viewport transformation.
Computer Graphics Curves
Polygon Tables
In this method, the surface is specified by the set of vertex
coordinates and associated attributes. As shown in the following
figure, there are five vertices, from v1 to v5.
Each vertex stores x, y, and z coordinate information which
is represented in the table as v1: x1, y1, z1.
The Edge table is used to store the edge information of
polygon. In the following figure, edge E1 lies between vertex
v1 and v2 which is represented in the table as E1: v1, v2.
Polygon surface table stores the number of surfaces present
in the polygon. From the following figure, surface S1 is
covered by edges E1, E2 and E3 which can be represented in
the polygon surface table as S1: E1, E2, and E3.
Plane Equations
The equation for plane surface can be expressed as −
Ax + By + Cz + D = 0
Where (x, y, z) is any point on the plane, and the coefficients A,
B, C, and D are constants describing the spatial properties of the
plane. We can obtain the values of A, B, C, and D by solving a
set of three plane equations using the coordinate values for three
non collinear points in the plane. Let us assume that three vertices
of the plane are (x1, y1, z1), (x2, y2, z2) and (x3, y3, z3).
Let us solve the following simultaneous equations for ratios A/D,
B/D, and C/D. You get the values of A, B, C, and D.
(A/D) x1 + (B/D) y1 + (C/D) z1 = -1
(A/D) x2 + (B/D) y2 + (C/D) z2 = -1
(A/D) x3 + (B/D) y3 + (C/D) z3 = -1
To obtain the above equations in determinant form, apply
Cramer’s rule to the above equations.
For any point (x, y, z) with parameters A, B, C, and D, we can
say that −
Ax + By + Cz + D ≠ 0 means the point is not on the plane.
Ax + By + Cz + D < 0 means the point is inside the surface.
Ax + By + Cz + D > 0 means the point is outside the surface.
3D Modelling System
3D modeling is a technique in computer graphics for producing a 3D digital
representation of any object or surface.
An artist uses special software to manipulate points in virtual space(called
vertices) to form a mesh: a collection of vertices that form an object.
These 3D objects can be generated automatically or created manually
by deforming the mesh, or otherwise manipulating vertices.
3D models are used for a variety of mediums including video games, movies,
architecture, illustration, engineering, and commercial advertising.
The 3D modeling process produces a digital object capable of being fully
animated, making it an essential process for character animation and special
effects.
The core of a model is the mesh which is best described as a collection of points
in space.
It is of three types
2. Necessary for many applications where objects must be maintained as solids. e.g. finite element analysis,
computer aided design and machining (CAD/CAM), mass property determination,refraction.
DETAIL INTRODUCTION:
Solid modeling is the unambiguous representation of the solid parts of an object, that
is, models of solid objects suitable for computer processing. Today, 3d solid modeling
is preferred over 2d modeling because of its flexibility, ease of rendering and accurate
photorealism.
Solid Modeling is the representation of solid parts of an object, that is, models of solid
objects suitable for computer processing. 3D solid modeling is used in computer-
aided design (CAD), engineering analysis, computer graphics, medical testing, product
visualization and scientific research.
There are many ways to create solid modeling. These include sweeping, boundary
representation, spatial occupancy enumeration, cellular decomposition, functional
representation, feature-based modeling, parametric modeling and facet modeling.
ADVANTAGES: 3D solid modeling has been replacing 2D modeling over the past
few years.
The realistic depiction of objects is a big advantage for manufacturers, allowing
them to see how a product functions without actually spending time and
money actually creating it.
The product design can be changed frequently to ensure that it’s flawless,
thereby improving efficiency.
In a solid model, a group of features is added one at a time until the model is
complete. Engineering solid models are built mostly with sketcher-based
features — 2-D sketches that are swept along a path to become 3-D.
Apart from the manufacturing sector, 3D Solid Modeling is also used in the
entertainment sector, for animation. Computer-generated characters, for
example, involve parametric modeling.
1. Domain :
While no representation can describe all possible solids, a representation
should be able to represent a useful set of geometric objects.
2. Unambiguity :
When you see a representation of a solid, you will know what is being
represented without any doubt. An unambiguous representation is usually
referred to as a complete one.
3. Uniqueness :
That is, there is only one way to represent a particular solid. If a representation
is unique, then it is easy to determine if two solids are identical since one can
just compare their representations.
4. Accuracy :
A representation is said accurate if no approximation is required.
5. Validness :
This means a representation should not create any invalid or impossible solids.
More precisely, a representation will not represent an object that does not
correspond to a solid.
6. Closure :
Solids will be transformed and used with other operations such as union and
Sweep Representations:
Sweep representations are useful for both surface modeling and solid
modeling.
A large class of shapes (both surfaces and solid models) can be formed by
sweeping or extruding a 2D shape through space.
Sweep representations are useful for constructing 3-D objects that posses
translational or rotational symmetries. We can represent such object by
specifying a 2D shape and a sweep that move the shape through a region of
space. A set of 2D primitives can be provided for a sweep representation as
menu options.
Types of Sweep Representations:
1. Translational sweep/Linear sweep .
2. Rotation sweep.
( a vertical prism)
sweep in Creates
top direction
/* TRANSLATION SWEEP */
Creates
(a cylinder)
• Boolean operation
• each primitive solid is assumed to be a set of points, a boolean operation
is performed on point sets and the result is a solid model.
• Boolean operation à union, intersection and difference
• The relative location and orientation of the two primitives have to be
defined before the boolean operation can be performed.
• Boolean operation can be applied to two solids other than the primitives.
• Union
– The sum of all points in each of two defined sets. (logical “OR”)
– Also referred to as Add, Combine, Join, Merge
A B
AU B
• Difference:
– The points in a source set minus the points common to a second set.
(logical “NOT”)
– Set must share common volume
– Also referred to as subtraction, remove, cut
A B A-B
• Intersection
– Those points common to each of two defined sets (logical “AND”)
– Set must share common volume
– Also referred to as common, conjoin
A B AꓵB
• When using boolean operation, be careful to avoid situation that do not result in
a valid solid
A B AꓵB
Boolean operation
• CSG tree à stores the history of applying boolean operations on the primitives.
– Stores in a binary tree format
– The outer leaf nodes of tree represent the primitives
– The interior nodes represent the boolean operations performed.
(CSG) - Disadvantage
• Only boolean operations are allowed in the modeling process à with boolean
operation alone, the range of shapes to be modeled is severely restricted à not
possible to construct unusual shape.
• A solid is bounded by its surface and it has its interior and ecxterior .The solid’s
surface consists of a set of well organized faces each of which is a piece of some
surface. These faces may share vertices and edges that are curve segments. So,
Advantages:
• Capability to construct unusual shapes that would not be possible with the
available CSG a aircraft fuselages, swing shapes.
• Less computational time to reconstruct the image.
Disadvantages:
There are two types of surfaces that are commonly used in modeling systems:
Explicit surface
Impicit surface.
Parametric surface
EXPLICIT SURFACE OF SURFACE MODELING:
Example:
1. (X-X0)2 +(Y-Y0)2 + (Z-Z0 )2 - R2 =0
X=f(u,v)
Y=f(u,v)
Z=f(u.v)
Thus (u,v) is a point in the square defined by (0,0),(1,0),(0,1) & (1,1) in the uv
coordinate plane.
SURFACE OF REVOLUTION :
y f x z
2 2
SWEEP SURFACES:
A three dimensional surface is also obtained by traversing an entity, e.g.
a line, polygon or curve, along a path in space. The resulting surfaces are
called “sweep surfaces”. Sweep surface generation is frequently used in
geometric modeling.
T(w) = 1 0 0 0
0 1 0 0
0 0 1 0
0 w 0 1
The simplest sweep entity is a point. The result of sweeping a point along
a path is, of course, not a surface but a space curve. However, it serves to
illustrate the fundamental technique.
Consider the position vector P[x y z 1] swept along the path represented by the
sweep transformation [Ts]. The position vector Q(s) representing the resulting
curve is
Q(s) = P . [ TS ] s1 ≤ s < s2
[ TS] = 1 0 0 0
0 1 0 0
0 0 1 0
0 0 ns 1 0≤s≤1
QUADTREE :
Quadtrees are a knowledge structure that encodes a two-dimensional space into
adaptable cells. Quadtrees are generated by successively dividing a 2-D region(usually
a square) into quadrants. Each node in the quad tree has 4 data elements, one for
each of the quadrants in the region.
If all the pixels within a quadrant have the same color (i.e. a homogeneous quadrant),
the corresponding data element in the node stores that color. Also ,a flag is set in the
data element to indicate that quadrant is homogeneous.
Quadrant Quadrant
0 1 2 3
0 1
Quadrant Quadrant
3 2
Data Elements in the Representative Quadtree Node
Region of 2D space
0 1 2 3
Quadrant Quadrant
0 1
Quadrant 3
Quadrant 2 0 1 2 3
A frequently used class of objects is the quadric surfaces, which are described with
second-degree equations (quadratics). They include spheres, ellipsoids,
tori,paraboloids, and hyperboloids. Quadric surfaces, particularly spheres and
ellipsoids, are common elements of graphics scenes, and they are often available in
graphics packages as primitives horn which more complex objects can be constructed.
Sphere:
X2 y2 z2 = r2
The parametric representation inEqs. 10-8 provides a symmetric range for the angular
parameters θand φ. alternatively, we could write the parametric colatitude (Fig. 10-
9). Then, φis defined over the range 0≤φ≤ π and θis often taken in the range 0 ≤θ≤ 2π.
We could also set up the representation using parameters u and v, defined over the
range from 0 to 1 by substituting φ= πu and θ= 2πv.
TORUS
where r is any given offset value. Parametric representations for a torus are similar to
those for an ellipse, except that angle d extends over 360". Using latitude and
longitude angles 4 and 8, we can describe the torus surface as the set of points that
satisfy
where parameter s can be assigned any real value. When s = 1, we get an ordinary
ellipse.
…………….2
Figure 10-12 illustrates supercircle shapes that can be generated using various values
for parameters.
Superellipsoid:
………………………….3
……….4
1. The surface does not pass through the control points except for the corners of
the control point grid.
2. The surface is contained within the convex hull of the control points.
3. Along the edges of the grid patch, the beizer surface matches that of a beizer
curve through the control points along that edge.
4. A cylinder/cone can be formed from a Bezier surface, it is not possible to form
a sphere.
5. Closed surfaces can be formed by setting the last control points equal to the
first .If the tangents also match between the first two and the last controlpoints
then the closed surface will have first order continuity.
A spline curve is a
mathematical
representation for which it
is easy to build
an interface that will In B-Spline, there is
allow a user to design and local control over the
control the shape of curve surface and the
complex shape of the curve is It is a parametric curve used in
curves and surfaces. affected by every vertex. related fields.
Wireframe model consists of vertex, edge (line) and polygons. Edge is used to
join vertex. Polygon is a combination of edges and vertices. The edges can be
Polygon Meshes
3D surfaces and solids can be approximated by a set of polygonal and line elements.
Such surfaces are called polygonal meshes. In polygon mesh, each edge is shared by
at most two polygons. The set of polygons or faces, together form the “skin” of the
object.
This method can be used to represent a broad class of solids/surfaces in graphics. A
polygonal mesh can be rendered using hidden surface removal algorithms. The
polygon mesh can be represented by three ways −
Explicit representation
Pointers to a vertex list
Pointers to an edge list
Disadvantages
Equation of a cone
To find the equation of a cone with vertex V (α, β, γ) and whose guiding curve is the
conic
ax2 + 2hxy + by2 + 2gx + 2fy + c = 0, z = 0
The second standard called the Graphics Kernal System(GKS) was developed in
Europe.
Another graphics standard is Programmer’s Hierarchical Interactive Graphics
Standard (PHIGS). It takes input and output functions and viewing model CORE and
GKS, but is ta a programmer’s toolbox, intended for programming graphics
applications .It contain enhanced program structuring features.
Two other graphics standard are the Computer Graphics Metafile (CGM) and the
Computer Graphics Interface (CGI). The CGM is a file formate for picture information
that allows device independent capture ,storage and transfer. CGI is a companion
standard which provides a procedural interface for CGM primitives.