unit-i: graphics primitives and scan conversion...points. for instance, if we want to display point...

24
UNIT-I: GRAPHICS PRIMITIVES AND SCAN CONVERSION 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: Imaging: It is the process of representing 2D images. Modeling: It is the process of creating and representing 3D objects. Mainly deals with the shape of the object. Rendering: It is the process of constructing 2D images from 3D models. It focuses on light and perspective of objects. Animation: It is the process of describing the change in objects in time. I represent movement of the object [10]. Applications are listed as follows: 1) Graphical User Interface 2) Education and Training 3) CAD (Computer Aided Design) 4) Entertainment (animations, games etc.) 5) Presentation 6) Computer Generated Art 7) Scientific and Business Visualization 8) Image Processing

Upload: others

Post on 10-Jun-2021

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: UNIT-I: GRAPHICS PRIMITIVES AND SCAN CONVERSION...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

UNIT-I: GRAPHICS PRIMITIVES AND SCAN CONVERSION

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:

Imaging: It is the process of representing 2D images.

Modeling: It is the process of creating and representing 3D objects. Mainly deals with the

shape of the object.

Rendering: It is the process of constructing 2D images from 3D models. It focuses on light and

perspective of objects.

Animation: It is the process of describing the change in objects in time. I represent movement

of the object [10].

Applications are listed as follows:

1) Graphical User Interface

2) Education and Training

3) CAD (Computer Aided Design)

4) Entertainment (animations, games etc.)

5) Presentation

6) Computer Generated Art

7) Scientific and Business Visualization

8) Image Processing

Page 2: UNIT-I: GRAPHICS PRIMITIVES AND SCAN CONVERSION...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

Applications of Computer Graphics

1) Graphical User Interface (GUI)

It is a program interface which makes use of the computer’s graphics capabilities to make the

program easier to use [10]. With the help of graphical interface displays such as menus and icons,

user gets free from learning complex command languages to work on computer. It requires less

screen space and easily understood by users to operate. Most popularly used Graphical User

Interfaces are Microsoft Windows, Apple System 7 and Macintosh, Chrome OS, Linux variants

like Ubuntu [4]. The major components of any graphical user interface are graphic pointer, a set

of icons, rectangular frames holding the text usually called as windows or dialogs, drop down

menus, scroll bars, input devices like, mouse, keyboard, joysticks, paddle controller etc [1][3][5].

The examples of GUI are depicted in figure as follows:

Fig.1. Graphical User Interfaces (GUI) [5][10]

2) Education and Training:

Computer generated models are required for physical, financial and economic systems so that they

can be used as educational aids for training purpose. These kind of educational aids provide help to

trainees for understanding the operation of the system very well in easy manner. Various

specialized systems are nowadays used for training applications such as simulators for practice

sessions or training session of ship captains, aircraft pilots, heavy equipment operators, air traffic-

control personnel, vehicle drivers and many more [10]. Two examples are shown in pictures below:

Page 3: UNIT-I: GRAPHICS PRIMITIVES AND SCAN CONVERSION...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

Fig.2. Simulators for aircraft pilot and vehicle driving [10]

3) Computer Aided Design (CAD)

A CAD system is a combination of hardware and software which enables engineers and architects

to design everything from buildings, aircraft, automobiles, spacecraft, watercraft, computers,

textiles & many other products. In addition to the software, CAD systems require a high-quality

graphics monitor; a mouse, light pen, or digitizing tablet for drawing; and a

special printer or plotter for printing design specifications. Using CAD systems, one can view a

design from any angle with the push of a button and to zoom in or out for close-ups and long-

distance views. Animations are also used in CAD applications. Also, CAD provides realistic

displays of architectural design that simulates virtual reality systems. Nowadays, CAD

software runs on general-purpose workstations as well as personal computers [6][7].

Fig.3 (a) AutoCAD 2002 Fig.3 (b) Interior Design.

Page 4: UNIT-I: GRAPHICS PRIMITIVES AND SCAN CONVERSION...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

4) Entertainment (animations, games etc.)

One of the main fields of application of computer graphics is the entertainment industry, in

particular motion pictures, music videos, computer games, television shows, making cartoon and

animated movies. As per the assumption, the first film that was made using computer graphics was

the Throne, and the first feature-length film produced completely using computer was Toy Story

[8]. Computer graphics are used in motion pictures, music videos, and television shows. Game

Industry mainly focuses on interactivity, cost effective solutions, avoiding computations and other

tricks, which can be easily achieved using computer graphics usage [8][10].

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:

Page 5: UNIT-I: GRAPHICS PRIMITIVES AND SCAN CONVERSION...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

Fig.5 Various presentation graphics [10]

6) Computer Generated Arts:

Computer graphics plays a very important role in generating computer generated arts. It is used in

fine arts as well as commercial arts, which include artist’s paintbrush programs, paint packages,

CAD packages and animation packages. These packages are helpful in designing object shapes &

specifying motions to be applied to objects. Cartoon drawing, paintings, product advertisements,

logo design includes such kind of packages. On the other hand, electronic painting also makes use

of graphics to paint pictures electronically on a graphics tablet or digitizer using a cordless,

pressure sensitive stylus. For transformation operation, such as morphing concept, where graphics

are used to transform one object into another. In most animation films, morphing is widely used.

Figure 6 presents different computer generated arts.

(a) (b) (c)

Fig.6 Computer generated arts: (a) Metacreation Painter (b) Electronic painting (c) Morphing [10]

Page 6: UNIT-I: GRAPHICS PRIMITIVES AND SCAN CONVERSION...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

7) Visualization:

Visualization is a technique for creating images, diagrams, or animations to communicate a

message. Visualization through visual imagery is a useful means of communication in terms of

abstract as well as concrete ideas. Cave paintings, Egyptian hieroglyphs, Greek geometry,

and Leonardo da Vinci's revolutionary methods of technical drawing for engineering and scientific

purposes are the famous examples of visualization. Visualization finds its applications in number

of field’s namely, science, education, engineering), multimedia, medicine, etc [9][10]. Some

important applications of visualization are listed as below:

i) Scientific Visualization: It is the transformation, selection, or representation of data from

simulations or experiments. It used an implicit or explicit geometric structure in order to allow the

exploration, analysis, and understanding of the data thoroughly [9].

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].

iii) Information Visualization: Information visualization concentrates on the use of computer-

supported tools to explore large amount of abstract data [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

Page 7: UNIT-I: GRAPHICS PRIMITIVES AND SCAN CONVERSION...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

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].

Fig. 8 N Image Processing in Medical Field

Page 8: UNIT-I: GRAPHICS PRIMITIVES AND SCAN CONVERSION...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

Important Aspects of Computer Graphics

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

Fig. 9. Pixel display area of 6 × 5

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.

Page 9: UNIT-I: GRAPHICS PRIMITIVES AND SCAN CONVERSION...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

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.

Plotting Primitives of Computer Graphics

1) Scan Conversion: Display processor digitizes a picture definition given in an application

program into a set of pixel intensity values for storage in the frame buffer. This digitization

process is called scan conversion.

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

Page 10: UNIT-I: GRAPHICS PRIMITIVES AND SCAN CONVERSION...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

m

b

0,0

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:

Fig. 10. Slope of line m

y = mx +b

(The intercept b is the height at which the line cross the y axies)

-There is a difference between line and line segment.

-The lines can extend forever both forward and backward direction

-A line segment is a piece of line lying in between two end points P1 and P2.

-If the end points are P1 = (x1, y1) and P2 = (x2, y2) and these two points yield a equation

then point P3 = (x3, y3) lies on the segment if y3 = m x3 + b,

Where x3 is in between x1 and x2 and y3 is in between y1 and y2. We can identify where two lines

are intersecting.

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

Page 11: UNIT-I: GRAPHICS PRIMITIVES AND SCAN CONVERSION...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

V

Dx

3) Vectors and Vector Generation:

a) Vector has a single direction and length.

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 ]

Dy

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.

Page 12: UNIT-I: GRAPHICS PRIMITIVES AND SCAN CONVERSION...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

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

- Line should displayed with constant Brightness

- Line should be drawn rapidly.

Page 13: UNIT-I: GRAPHICS PRIMITIVES AND SCAN CONVERSION...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

1) Digital Differential Analyzer (DDA):

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.

if (dx > dy)

length = abs (dx);

else

length = abs (dy);

Step 4: Calculate the increment in x coordinate and y coordinate.

xincrement = dx / (float) length;

xincrement = dy / (float) length;

Page 14: UNIT-I: GRAPHICS PRIMITIVES AND SCAN CONVERSION...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

Step 5: x = x1 + 0.5 * Sign(xincrement);

y = y1 + 0.5 * Sign(yincrement);

Here, Sign() function will make the algorithm to work in all quadrant. It returns -1,0, 1 depending on

whether its argument is <0, =0, >0 respectively.

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++) {

Step 7: Stop.

x = x + xincrement;

y = y + yincrement;

putpixel(x,y);

}

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

1. Floating point arithmetic in DDA algorithm is still time-consuming.

2. The algorithm is orientation dependent. Hence end point accuracy is poor [14].

Page 15: UNIT-I: GRAPHICS PRIMITIVES AND SCAN CONVERSION...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

Example Illustration

Q. Consider the line from (0,0) to (4,6). Use the simple DDA algorithm to rasterize this line.

Sol. Evaluating steps 1 to 5 in the DDA algorithm we have,

xl = 0 y1 = 0

x2 = 4 y2 = 6

dx = |x2 – x1| = |4 – 0| = 4

dy = |y2 – y1| = |6 – 0| = 6

Since dy > dx, length = |y2 – y1l = 6

Xincrement = dx / length

= 4/6 = 0.66

Yincrement = dy / length

= 6/6=1

Initial value for

x = 0 + 0.5 * Sign (0.66) = 0.5

y = 0 + 0.5 * Sign (1) = 0.5

Tabulating the results of each iteration in the step 6 we get,

i Plot x y

0.5 0.5

1 (0,0)

1.167 1.5

2 (1,1)

Page 16: UNIT-I: GRAPHICS PRIMITIVES AND SCAN CONVERSION...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

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

Fig. 13. Rasterized line for points (0,0) to (4,6).

Page 17: UNIT-I: GRAPHICS PRIMITIVES AND SCAN CONVERSION...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

C++ code for DDA Line Drawing Algorithm [17]

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

if (dx >= dy) length=dx;

else length=dy;

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]

Page 18: UNIT-I: GRAPHICS PRIMITIVES AND SCAN CONVERSION...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

{

putpixel(int(x),int(y),15); x=x+dx; y=y+dy; i=i+1;

}

getch(); closegraph();

return 0;

}

Output

Fig. 14. Output of DDA Line Drawing Algorithm

2) Bresenham’s Line Drawing Algorithm:

The Bresenham’s algorithm is another incremental scan conversion algorithm. The big advantage of

this algorithm is that, it uses only integer calculations. Moving across the x axis in unit intervals and at

each step choose between two different y coordinates. The basis program of Bresenham’s line drawing

algorithm is to select the optimum raster location to represent a straight line. The increment in the other

variable is determined by examining the distance between the actual line location and the nearest pixel

[17]. This distance is called decision variable or the error. Consider a line with initial point (x1, y1) and

terminal point (x2, y2) in device space. If ∆x = x2 − x1 and ∆y = y2 − y1, we define the driving axis

(DA) to be the x-axis if |∆x| ≥ |∆y|, and the y-axis if |∆y| > |∆x|. The DA is used as the “axis of

control” for the algorithm and is the axis of maximum movement. Within the main loop of the

algorithm, the coordinate corresponding to the DA is incremented by one unit. The coordinate

corresponding to the other axis (usually denoted the passive axis or PA) is only incremented as needed

[19].

Page 19: UNIT-I: GRAPHICS PRIMITIVES AND SCAN CONVERSION...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

Algorithm

Step 1: Read the line end points (x1, y1) and (x2, y2) such that they are not equal.

Step 2: dx = |x2 – x1| and dy =|y2 – y1|.

Step 3: Initialize starting points; x = x1 and y = y1

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)

Step 7: while(e >= 0)

{

y = y +1; e = e – 2 * dx

}

x = x + 1; e = e + 2 * dy;

Step 8: increment counter by 1; i = i +1

Step 9: if (i <= dx) then go to Step 6.

Step 10: Stop

Example Illustration

Q. Consider the line from (5, 5) to (13, 9). Use Bresenham’s algorithm to rasterize this line.

Sol. Evaluating steps 1 to 4 of Bresenham’s algorithm we have,

xl = 5 y1 = 5

x2 = 13 y2 = 9

dx = |x2 – x1| = |13 – 5| = 8

Page 20: UNIT-I: GRAPHICS PRIMITIVES AND SCAN CONVERSION...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

dy = |y2 – y1| = |9 – 5| = 4

x = x1 =5 and y = y1 =5

error or decision variable, e = 2 * 4 – 8

e = 0

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

3 (7,6)

8 7 -8

4 (8,7)

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

Fig. 14. Rasterized line for points (5, 5) to (13, 9)

Page 21: UNIT-I: GRAPHICS PRIMITIVES AND SCAN CONVERSION...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

Code for BreC++ code for Bresenham’s Line Drawing Algorithm [20]

# include <iostream.h>

# include <conio.h>

# include <graphics.h>

# include <math.h>

char IncFlag;

void Bresenham(int x1,int x2,int y1,int y2); void DrawLine(int X,int Y,int End,int PInc,int NInc,int P,int XInc,int YInc);

void main() {

int gDriver=DETECT, gMode;

int x1,x2,y1,y2;

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

}

void Bresenham(int x1,int x2,int y1,int y2) {

int S,O,End;

int P;

int dx = abs(x1 - x2);

int dy = abs(y1 - y2); float Slope;

int PInc,NInc,XInc,YInc; if (dx == 0) //Slope Infinite

{

}

else {

Slope = (float)(y1 - y2) / (x1 - x2);

if (Slope>-1 && Slope<1) {

IncFlag = 'X'; PInc = 2 * (dy - dx);

NInc = 2 * dy;

P = 2 * dy - dx;

if (x1>x2)

Page 22: UNIT-I: GRAPHICS PRIMITIVES AND SCAN CONVERSION...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

{ S = x2;

O = y2;

End = x1;

}

else {

S = x1;

O = y1;

End = x2;

}

// DrawLine(x,y,End,PInc,NInc,P,XInc,YInc);

} else

{

IncFlag = 'Y';

PInc = 2 * (dx - dy);

NInc = 2 * dx;

P = 2 * dx - dy;

if (y1 > y2) {

O = x2;

S = y2;

End = y1;

}

else

{

O = x1;

S = y1;

End = y2; }

}

if (IncFlag == 'X')

putpixel(320+S,240-O,12);

else putpixel(320+O,240-S,12);

while (S <= End)

{

S++;

if (P<0)

P = P + NInc; else {

P = P + PInc;

if (Slope>0.0)

O++;

else

O--;

}

if (IncFlag == 'X')

putpixel(320+S,240-O,12);

else

Page 23: UNIT-I: GRAPHICS PRIMITIVES AND SCAN CONVERSION...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

putpixel(320+O,240-S,12);

} }

}

Output

Fig. 14. Output of Bresenham’s Line Drawing Algorithm

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%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/

Page 24: UNIT-I: GRAPHICS PRIMITIVES AND SCAN CONVERSION...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

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