Unit 1 GRAPHICS
Unit 1 GRAPHICS
Computer Graphics:
An ability of drawing pictures, lines, charts, etc by means of computers through programming is
defined as computer graphics. It converts data to and/or from visual arrangement using computers. It
uses a computer to create pictures. Algorithms, mathematics, data structures enable a computer to
make Pretty Pictures. Ivan Edward Sutherland is widely regarded as the "father of computer graphics”
[2]. Computer Graphics has two main aspects namely technical and art. Computer Scientists create
libraries and tools etc for artists or non technical person, so that they can make use of it to create
pretty pictures. Artistic person uses these computer graphics tools to create attractive pictures [1]. The
technical definition of computer graphics includes composition of four important steps:
Fig.4 (a) ToyStory Fig.4 (b) Fighting Sequence Fig.4 (c) Titanic Fig. 4. (d) Games
5) Presentation:
This is very active application of computer graphics which is used in almost all industries either for
training presentation or for demonstration of a particular product. It is used to produce reports or
slides to use with projectors, commonly summarizes financial, mathematical, statistical,
economical, and scientific, for research reports, managerial reports & customer information
bulletins. Various graphics that are used in presentations are Bar charts, line graphs, pie charts,
surface graphs, time chart, medical images etc. Technical report writing, conference papers,
magazine articles, newspapers etc are required to make use of such graphics on daily basis [10].
Few graphics are shown in next pictures:
Fig.5 Various presentation graphics [10]
ii) Educational Visualization: Educational visualization uses a simulator, which is very useful
when teaching about a topic that is difficult to demonstrate without expensive and difficult to use
scientific equipment, for example, atomic structure, because atoms are far too small to be studied
easily [9].
iv) Product visualization: It involves visualization software technology to view and manipulate 3D
models, technical drawing and documentation of manufactured components and large assemblies
of products.
Fig. 7 (a) Nebula Fig. 7 (b) Medical Image Fig. 7 (c) Molecule Structure
8) Image Processing:
Computer is used to create a picture and image processing is a technique to modify and interpret
existing pictures. Image processing improves the quality of pictures and machine perception of
visual information. It finds its application in medical industry most importantly, to enhance
pictures, tomography, ultrasonic and nuclear medicine scanners [10]. Image processing includes
following steps:
i) Digitize a photograph (or picture) into an image file.
ii) Apply digital methods to rearrange picture parts to enhance color separations and improve
quality of shading.
iii) Tomography: It is a technique of X-ray photography which allows cross-sectional views
of physiological systems to be displayed.
iv) Computed X-ray tomography (CT) and position emission tomography (PET) use
projection methods to reconstruct cross sections from digital data.
v) Computer-Aided Surgery is a medical application technique to model and study physical
functions to design artificial limbs and to plan & practice surgery [10].
1) Pixel: In computer graphics, pictures or graphics objects or each screen point is represented as
a collection of discrete picture elements which are called as pixels. It is also referred to as a pel,
as it is a smallest addressable screen element. It is the smallest piece of screen, which we can
control by setting the intensity and color. Each pixel on the graphics display does not represent
the mathematical point but the region which theoretically can contain an infinite number of
points. For instance, if we want to display point P1 & P2 with coordinates (4.2, 3.8) and (4.8,
3.1) respectively, then points P1 & P2 are represented by only one pixel (4, 3) as shown in fig.
below. In general point is represented by Pixel (int (x), int (y)).
2) Frame Buffer: A special area of memory is dedicated to graphics only, and picture definition
is stored in this memory area which is called frame buffer or refresh buffer. This memory area
holds the set of intensity values for all the screen points and the stored intensity value are then
retrieved from this frame buffer and painted on the screen one row at a time to display picture.
3) Resolution: It refers to the maximum number of dots or points that can be displayed on the
screen without overlap on the Cathode Ray Tube (CRT). It is expressed as the number of points
per centimeter that can be plotted horizontally and vertically. Resolution depends on the type of
phosphor, the intensity to be displayed and the focusing and deflection systems used in the
CRT. The most commonly used four resolutions today are: 640 × 480, 1024 × 768, 800 × 600,
1280 × 1024.
4) Aspect Ratio: This number gives the ratio of vertical points to horizontal points necessary to
produce equal-length line in both directions on the screen. Sometimes it is also referred to as
the ratio of horizontal to vertical points. An aspect ratio of 5/6 means that a vertical line plotted
with 5 points has the same length as a horizontal line plotted with 6 points. eg. aspect ratio of
12” × 16” display is 12/16 = 3/4.
5) Persistence: The operation of most video monitors is based on standard Cathode Ray Tube
(CRT). The screen of the video monitors is usually phosphor coated. Different kinds of
phosphors are available for use in a CRT. Besides color, a major difference between phosphors
is their persistence i.e. how long they continue to emit light after the CRT beam is removed.
Persistence is basically defined as the time it takes the emitted light from the screen to decay to
one-tenth of its original intensity. Lower persistence phosphors required higher refresh rates to
maintain a picture on the screen without flicker. A phosphor with low persistence is useful for
animation, a high –persistence phosphor is used 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.
2) Lines and Line Segments: Point can be specified by giving pair of numbers (x, y), where x
represents horizontal distance and y represents vertical distance. Every line is described by the
equation such that if a point (x, y) satisfies the equation, then that point is on the line. If the two
points used to specify a line are (x1, y1) and (x2, y2) then the equation of line will be,
Above equation justify that slope between any point on the line and point (x1, y1) is the same as
the slope between (x1, y1) and (x2, y2). Slope is usually denoted by m and is defined as the
change in height divided by the change in width for any two points on the line. Slope m is
defined in equation form as follows:
m
b
0,0
Fig. 10. Slope of line m
y = mx +b
(The intercept b is the height at which the line cross the y axies)
Consider two lines having slope m1 and m2 and y intercept b1 and b2 respectively. Then we can
write equation for these two lines is
Line 1 is y= m1x+b1
Line 2 is y=m2x+b2
3) Vectors and Vector Generation:
b) A vector is denoted by [Dx, Dy], where Dx indicates how far to move in x-axis direction and
Dy indicates how far to move in y-axis direction as shown in fig
c) Vectors specify how far and in what direction to move, but don’t specify from where to
start.
d) Addition of two Vectors If vector V1 = [Dx1, Dy1] and vector V2 = [Dx2, Dy2] then resultant
vector can be derived as:
V1 + V2 = [Dx1, Dy1] + [Dx2, Dy2]
= [Dx1 + Dx2, Dy1 + Dy2 ]
V
Dy
Dx
Fig. 12. Vector
Multiplication of Two Vector We can multiply vector by a number and length of the vector
can be given by Pythagorean theorem.
If we multiply a vector by reciprocal of its length then the resultant vector is of length equal to
1 and such a vector is called unit vector.
The process of “Turning ON” the pixels for a line segment is called a vector generation.
Whenever we need to draw a line, we need to turn ON the pixels on that line i.e. we need to
change the intensity of the pixels present on the line.
Two well known algorithms used for vector generation are Digital Differential Analyzers
(DDA) and Bresenhans Line Drawing Algorithm. Both the algorithms make use of
increment method.
Line Drawing Algorithms
A basic element in graphics is that a line connects two points.. To draw a line, we need two points
between which we can draw a line. In line drawing algorithms, line is drawn between one point of line
as (x0, y0) and the second point of line as (x1, y1).
Before Discussing Line Drawing Algorithm it ie useful to note general requirements for such
algorithm.
- The line should appear as straight line and should start and end accurately
DDA algorithm is an incremental scan conversion method. Here we perform calculations at each step
using the results from the preceding step. The characteristic of the DDA algorithm is to take unit steps
along one coordinate and compute the corresponding values along the other coordinate. The unit steps
are always along the coordinate of greatest change, e.g. if dx = 10 and dy = 5, then we would take unit
steps along x and compute the steps along y . Algorithm for DDA line drawing is described stepwise as
follows:
Algorithm [15]
Step 1: Accept the two end points (x0 , y0 ) and (x1 , y1 ) as an input.
Step 2: Calculate the horizontal and vertical difference between two end points.
dx = |x1 - x0|
dy = |y1 - y0|
Step 3: Based on the calculated difference in step 2, identify the number of steps to put pixel. If dx >
dy, then we need more steps in x coordinate; otherwise in y coordinate.
Step 6: Put the pixel by successfully incrementing x and y coordinates accordingly and complete the
drawing of the line.
for(int v=0; v < length; v++)
{
x = x + xincrement;
y = y + yincrement;
putpixel(x,y);
}
Step 7: Stop.
Advantages
1. It is the simplest algorithm and it does not require special skills for implementation.
2. It is a faster method for calculating pixel positions than the direct use of equation y = mx + b. It
eliminates the multiplication in the equation by making use of raster characteristics, so that appropriate
increments are applied in the x or y direction to find the pixel positions along the line path [14].
Disadvantages
Q. Consider the line from (0,0) to (4,6). Use the simple DDA algorithm to rasterize this line.
xl = 0 y1 = 0
x2 = 4 y2 = 6
dx = |x2 – x1| = |4 – 0| = 4
dy = |y2 – y1| = |6 – 0| = 6
Xincrement = dx / length
= 4/6 = 0.66
Yincrement = dy / length
= 6/6=1
i Plot x y
0.5 0.5
1 (0,0)
1.167 1.5
2 (1,1)
1.833 2.5
3 (1,2)
2.5 3.5
4 (2,3)
3.167 4.5
5 (3,4)
3.833 5.5
6 (3,5)
4.5 6.5
#include <iostream>
#include <graphics.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, NULL);
cout<<"\n Enter X1,Y1,X2,Y2";
int x1,y1,x2,y2;
cin>>x1>>y1>>x2>>y2;
int dx = x2 - x1;
int dy = y2 - y1;
int length;
dx=dx/length;
dy=dy/length;
int sx;
if (dx >= 0)
sx=1;
else
sx=-1;
int sy;
if(dy >= 0)
sy=1;
else
sy=-1;
float x = x1+0.5*(sx);
float y = y1+0.5*(sy);
int i=0;
while(i<=length)
DEPT OF COMPUTER ENGG, DYPCOE, AKURDI | [PREPARED BY: MS. VISHAKHA A. METRE]
{
putpixel(int(x),int(y),15);
x=x+dx; y=y+dy; i=i+1;
}
getch();
closegraph();
return 0;
}
Output
Step 1: Read the line end points (x1, y1) and (x2, y2) such that they are not equal.
Step 4: Initialize the value of error or decision varaiable to compensate for non-zero interrupts;
e = 2 * dx - dy
Step 5:Initialize counter; i = 0
Step 6: Plot(x, y)
Example Illustration
Q. Consider the line from (5, 5) to (13, 9). Use Bresenham’s algorithm to rasterize this line.
xl = 5 y1 = 5
x2 = 13 y2 = 9
x = x1 =5 and y = y1 =5
Now for each iteration, tabulate the results using steps 5 through 10, which are represented in table
below:
i Plot x y e
5 5 0
1 (5,5) 6 6 -8
2 (6,6) 7 6 0
(7,6)
3 8 7 -8
(8,7)
4 9 7 0
5 (9,7) 10 8 -8
6 (10,8) 11 8 0
7 (11,8) 12 9 -8
8 (12,9) 13 9 0
9 (13,9) 14 10 -8
void Bresenham(int,int,int,int);
initgraph(&gDriver,&gMode,"c:\\tc\\bgi");
cout<<endl<<"x1 : ";
cin>>x1;
cout<<endl<<"y1 : ";
cin>>y1;
cout<<endl<<"x2 : ";
cin>>x2;
cout<<endl<<"y2 : ";
cin>>y2;
line(320,0,320,480);
line(0,240,640,240);
Bresenham(x1,x2,y1,y2);
getch();
}
Output
REFERENCES:
1) http://www.webopedia.com/TERM/G/Graphical_User_Interface_GUI.html
2) https://www.google.co.in/url?sa=t&rct=j&q=&esrc=s&source=web&cd=3&cad=rja&uact=8&
ved=0ahUKEwjXrK2fw_DQAhUIP48KHV7bAPwQFgghMAI&url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fen.wi
kipedia.org%2Fwiki%2FGraphical_user_interface&usg=AFQjCNFnYc2ymWigzI7_sAtzXlAV
woYkaA&sig2=0djsI4hrwM8QM3JKYxvFwQ&bvm=bv.141320020,d.c2I
3) http://www.computerhope.com/jargon/g/gui.htm
4) http://www.computerhope.com/jargon/g/gui.htm
5) http://study.com/academy/lesson/what-is-a-graphical-user-interface-gui-definition-components-
examples.html
6) http://whatis.techtarget.com/definition/CAD-computer-aided-design
7) http://www.webopedia.com/TERM/C/CAD.html
8) http://brasil.cel.agh.edu.pl/~12sustrojny/en/przykladowe-zastosowania-nowoczesnej-grafiki-
komputerowej/przemysl-rozrywkowy/
9) https://en.wikipedia.org/wiki/Visualization_(graphics)
10) http://www.slideshare.net/aainakatyal/applications-of-computer-graphics
11) http://www.slideshare.net/VishnuRam7/computer-graphics-12567217
12) https://www.tutorialspoint.com/computer_graphics/computer_graphics_basics.htm
13) https://www.cise.ufl.edu/~sitharam/COURSES/CG/kreveldintrolinesegment.pdf
14) http://ecomputernotes.com/computer-graphics/simple-line-drawing-method/write-short-note-
on-digital-differential-analyzer-dda
15) https://www.tutorialspoint.com/computer_graphics/pdf/line_generation_algorithm.pdf
16) https://sites.google.com/site/assignmentssolved/mca/semester3/mc0072/2
17) http://www.eazynotes.com/notes/computer-graphics/programs/program-to-draw-a-line-using-
dda-algorithm.pdf
18) http://mcasjcet.weebly.com/uploads/4/4/7/9/4479347/computer_graphics_first_module_second.
pdf
19) http://graphics.idav.ucdavis.edu/education/GraphicsNotes/Bresenhams-Algorithm.pdf
20) http://www.dailyfreecode.com/code/bresenham-line-drawing-algorithm-694.aspx