Anda di halaman 1dari 17

Computer Engg.

/TE/I/CG/Practical/1-7Computer Engineering / TE / I / CG /
Practical / 1-7Applied Science / FE / I / CP / Practical / 1-12Applied
Science / FE / I / CP / Practical / 2-12Computer Engineering / SE / I / DC /
Practical / 8-8

20013
201431

T.E. Computer Engineering

g
Computer ProgrammingGraphics
PracticalExperiment No
.:
. 182

1
Line generation using DDA and Bresenhams
algorithmImplementation of ChecksumProgram
Forfor finding largest from 3 numbers using
Nested-if statementbasic arithmetic operations
Goal:

To learn to implement Checksum learn how to generate line


usingworking of DDA and Bresenhams algorithm for generating a line.how
to implement a C program for basic Arithmetic Operationsfinding largest
from 3 numbers-

Related units/ topics: Unit VII- Error Detection and Correction-Selection


Statement C Operators-Arithmetic Operators. - Line drawing primitive approach,
Incremental algorithm
Prerequisite: Types of error and error detection techniques
C
programming,C/ C++ programming, basic concept of computer graphics, Graphics
library functions in C/C++Algorithms, Flow Chart, Building blocks of C, Structure of
C program, Decision Control Statement
Software Requirements: C /complier, C++ Compiler with Graphics facilityTurbo
C
Theory / Analysis:

Line generation using DDA and Bresenhams algorithStudy of


Networking CablesmProgram for basic Arithmetic Operationfinding
largest of 3 numbers using nested-if statement

Computer Engg./TE/I/CG/Practical/1-7Computer Engineering / TE / I / CG /


Practical / 1-7Applied Science / FE / I / CP / Practical / 1-12Applied
Science / FE / I / CP / Practical / 2-12Computer Engineering / SE / I / DC /
Practical / 8-8

20013
201431

Any communication system must be capable to transfer data from one


device to another with accuracy but data get corrupted during transmission.
In reliable communication system, there must be a mechanism for error
detection and correction.
AlgorithmDecision Control StatementDDA algorithm:
DDA algorithm is used for line dawning, this algorithm consists a recursion relation for
successive values of x & y along the required line. Such a way of rasterizing a line is
called a digital differential analyzer.An algorithm is a sequence of steps written in form
of English phrases that specify the tasks that are to be performed while solving a
problemControl statements are defined as the statements that are responsible for
controlling execution of various instructions included in a program. Classification of
control statements is as:

Control Statements

--

Selection Statements

if
if-else
nested if-else
else-if
switch case

Iterative Statements

for loop
while loop
do-while loop

Jump Control Statements

break
continue
go-to

Bresenhams algorithmFlow ChartSelection statements :


Bresenhams algorithms principal principle is to select optimum raster locations to
represent a straight line. To accomplish thisTo draw the line, the algorithm increments x
or y by one unit at a time. The increment is determined by examining the distance
between the actual line location and the nearest pixel is called as decision variable or
error.A flow chart is the graphical representation of the flow of control and logic in the
solution of a problem. The flow chart is a pictorial representation of the
algorithm.Selection statements are statements that test a particular condition in a
program. Following are the various selection statements.
If Statement
If-else statement
Nested if-else statement
Else-if statement
Switch case statement

Nested if else statement Structure of C Program:

Preprocessor Directives- Are used to specify instruction in a program


which is processed before the actual program is executed. The common
preprocessor directives are #define and #include
Line generation using DDA and Bresenhams algorithStudy of
Networking CablesmProgram for basic Arithmetic Operationfinding
largest of 3 numbers using nested-if statement

Computer Engg./TE/I/CG/Practical/1-7Computer Engineering / TE / I / CG /


Practical / 1-7Applied Science / FE / I / CP / Practical / 1-12Applied
Science / FE / I / CP / Practical / 2-12Computer Engineering / SE / I / DC /
Practical / 8-8

20013
201431

Standard Functions in Cmain()- It indicates the compiler where program begins, all statements
are written after main in curly bracket.
printf()- It used to print output or data.
scanf()-It is used to get the input
A large number of if else statements are required in the complex
programs so they are nested inside each other which is called nested if
else statement. Syntax is as follows
if(condition)
{
if(condition)
{
statements;
}
else
{
statements
}
}
else
If(condition)
{
{
Statements
If(condition)
{
}
Statements;
}
Else(condition)
{
Statements;
}

Design:
DDA Algorithm
}
Start
Else
Start
{ variables a, b, c
Initialize the
Statements;
Get the input
from user
}
If a is greater than b and c, print
the Initialize
variable aPerform addition print
Declare
Sum
variables a, b, c
Perform subtraction print Difference
If b is greater than a, find b is greater
than
Input
fromc, if so print the variable b
Perform Multiplication print Product user
Perform Division print Division Else print the variable CResult
Stop
True
False
if
a>b
True
True
False
False
if
if a>c
b>c
Input
Input
Input
fromPrint
fromPrint
fromPrint
Bresenhams algorithStudy
of
b user
aLine
usergeneration using DDA and
c user

Networking CablesmProgram for basic Arithmetic Operationfinding


largest of 3 numbers using nested-if statement
Start

Computer Engg./TE/I/CG/Practical/1-7Computer Engineering / TE / I / CG /


Practical / 1-7Applied Science / FE / I / CP / Practical / 1-12Applied
Science / FE / I / CP / Practical / 2-12Computer Engineering / SE / I / DC /
Practical / 8-8

20013
201431

Types of Error
Single bit error- Only one bit in the data unit has changed.
Burst error- Two or more bits in the data unit have changed.
Error DetectionIn error detection we are looking only to see if any error has occurred. The
answer is simple yes or no. We are not interested in number of errors. The
most common methods used for error detection are simple parity, two
dimensional parity, CRC, and checksum.
Checksum
The checksum is higher level error detecting method used by layers.
Suppose we want to send our data which is a list of 5 numbers to
destination. In addition to sending these numbers we will send sum of the
numbers.
The receiver adds original 5 numbers and checksum if the result is zero
then data is accepted otherwise rejected. Following are the steps to
calculate checksum
Steps involving ChecksumSender Side1. Add all the numbers which you want to send and calculate sum of the
numbers
2. Represent calculated sum into binary format
3. Calculate wrapping sum if the number of bits in calculated sum are
greater than the number of bits in all the numbers which we are
sending.
4. The final sum is complemented and becomes the checksum
F
Flow Chart

Line generation using DDA and Bresenhams algorithStudy of


Networking CablesmProgram for basic Arithmetic Operationfinding
largest of 3 numbers using nested-if statement

Computer Engg./TE/I/CG/Practical/1-7Computer Engineering / TE / I / CG /


Practical / 1-7Applied Science / FE / I / CP / Practical / 1-12Applied
Science / FE / I / CP / Practical / 2-12Computer Engineering / SE / I / DC /
Practical / 8-8

20013
201431

Receiver side-

1. Add all the numbers including checksum and calculate new sum of the

numbers
2. Represent calculated sum into binary format
3. Calculate wrapping sum if the number of bits in calculated sum are

greater than the number of bits in all the numbers which are received
4. Complement the final sum
5. If the result is zero, the data are accepted; otherwise rejected
Line generation using DDA and Bresenhams algorithStudy of
Networking CablesmProgram for basic Arithmetic Operationfinding
largest of 3 numbers using nested-if statement

Computer Engg./TE/I/CG/Practical/1-7Computer Engineering / TE / I / CG /


Practical / 1-7Applied Science / FE / I / CP / Practical / 1-12Applied
Science / FE / I / CP / Practical / 2-12Computer Engineering / SE / I / DC /
Practical / 8-8

20013
201431

Perform
Addition

E.g. Suppose we want to send 5 numbers 7,11,12,0,6


Vvariables:Print
x, y, x, y, xi,yi,x2,y2
Sum
Functions: sign (t ),
returns 1, 0, -1 if given argument t is >0, =0, <0
abs (t ), which returns absolute value of t
Read the co-ordinates of line endpoints (x1,y1) and (x2,y2) such that they are not equal.
if abs(x2-x1) >= abs (y2-y1)
length= abs (x2-x1)
else
length= abs(y2-y1)
end if
x = (x2-x1)/length
y = (y2-y1)/length
{Select larger x or y to be one raster unit, round the values rather then than truncate.}
x = x1+ 0.5 * sign(x)
y= y1 + 0.5 * sign (y)
{Here sign function makes the algorithm work on all four quadrants and 0.5 makes it
possible to round up the values.}
begin main loop
i=1
while (i<=length)
set pixel (integer (x) , integer (y) )
x = x + x
y = y + y
i=i+1
End while
Finish
Stop
Bresenhams Algorithm
vaVariables:x, y, x, y, xi,yi,x2,y2
Line generation using DDA and Bresenhams algorithStudy of
Networking CablesmProgram for basic Arithmetic Operationfinding
largest of 3 numbers using nested-if statement

Computer Engg./TE/I/CG/Practical/1-7Computer Engineering / TE / I / CG /


Practical / 1-7Applied Science / FE / I / CP / Practical / 1-12Applied
Science / FE / I / CP / Practical / 2-12Computer Engineering / SE / I / DC /
Practical / 8-8

20013
201431

Functions: sign (t )),


returns 1, 0, -1 if given argument t is >0, =0, <0
abs (t ) , which rreturns absolute value of t
Read the co-ordinates of line endpoints (x1, y1) and (x2, y2) such that they are not equal.
Iinitialize
x = x1
y = y1
x = abs (x2-x1)
y = abs (y2-y1)
s1 = sign (x2-x1)
s2 = sign (y2-y1)
{interchange x and y depending on the slope of line}
if y > x then
temp = x
x = y
y = temp
interchange = 1
else
interchange = 0
end if
(initialize error term}
e = 2 * y - x
{main loop}
for i= 1 to x
setpixel (x, y)
while (e >0)
if interchange = 1 then
x= x + s1
else
y = y +s2
end if
e = e- 2 * x
end while
if interchange = 1 then
y = y+s2
else
x = x +s1
end if
e = e + 2 * y
next i
Stop

Line generation using DDA and Bresenhams algorithStudy of


Networking CablesmProgram for basic Arithmetic Operationfinding
largest of 3 numbers using nested-if statement

Computer Engg./TE/I/CG/Practical/1-7Computer Engineering / TE / I / CG /


Practical / 1-7Applied Science / FE / I / CP / Practical / 1-12Applied
Science / FE / I / CP / Practical / 2-12Computer Engineering / SE / I / DC /
Practical / 8-8

20013
201431

Perform Division

Stop

Perform
Multiplication

Perform
Subtraction
Print
Product
Print

Print Division
Result

Difference

Line generation using DDA and Bresenhams algorithStudy of


Networking CablesmProgram for basic Arithmetic Operationfinding
largest of 3 numbers using nested-if statement

Computer Engg./TE/I/CG/Practical/1-7Computer Engineering / TE / I / CG /


Practical / 1-7Applied Science / FE / I / CP / Practical / 1-12Applied
Science / FE / I / CP / Practical / 2-12Computer Engineering / SE / I / DC /
Practical / 8-8

20013
201431

Implementation:
Source code of the Program in C/C++

Compilation / Execution Process:


Type in the
source code using text editor of Turbo C / C++ IDEE.
Compile using
Ctrl F9.
Execute using
Alt F5
Input & Output:
Threewo vValues for P1 (x1, y1) and P2(x2, y2) is are given then basic
arithmetic operations are performeduserslargest of them is found using nested if
statement

.line is generated from P1 to P2


Conclusion:
By using DDA and Bresenhams algorithm a line can be generated. DDA uses float numbers
and uses operators such as division and multiplication in its calculation whereas Bresenhams
algorithm uses ints integers and only usesoperators such as addition and subtraction.
DDA uses rounding off of the pixel position obtained by multiplication or division, which causes an
accumulation of error in the proceeding pixels whereas in Bresenhams line algorithm the new pixel
is calculated with a small unit change in one direction and checking of nearest pixel with the
decision variable thus satisfying the line equation.
Due to the use of only addition, subtraction, and bit shifting (multiplication and division use more
resources and processor power) and decision variable Bresenhams algorithm is faster than DDA in
producing the line.

For line drawing, the Bresenhams algorithm is better that that of DDA as it is
faster and more accurate.By using basic C fundamentals Thus we can develop a
program for basic arithmetic operations.detect single bit error using checksum
method
Line generation using DDA and Bresenhams algorithStudy of
Networking CablesmProgram for basic Arithmetic Operationfinding
largest of 3 numbers using nested-if statement

Computer Engg./TE/I/CG/Practical/1-7Computer Engineering / TE / I / CG /


Practical / 1-7Applied Science / FE / I / CP / Practical / 1-12Applied
Science / FE / I / CP / Practical / 2-12Computer Engineering / SE / I / DC /
Practical / 8-8

20013
201431

References:
1. B. A. Forouzan, "Data communication and networking", Third Edition TMH
2. ISRD group, Data communication and computer network, TMH

1. Reference:
[1] P K Sinha, "Computer Fundamentals" 4th Edition, BPB Publication, PP:184-189
E Balagurusamy, "Programming in ANSI",5th Edition, Tata Mc Graw Hill PP:23114-12539
David F. Rogers, Procedural Elements for Computer Graphics, Tata McGraw Hill, 2nd Ed.ition
ppPage No: 67,, 76

Line generation using DDA and Bresenhams algorithStudy of


Networking CablesmProgram for basic Arithmetic Operationfinding
largest of 3 numbers using nested-if statement

10

Computer Engg./TE/I/CG/Practical/1-7Computer Engineering / TE / I / CG /


Practical / 1-7Applied Science / FE / I / CP / Practical / 1-12Applied
Science / FE / I / CP / Practical / 2-12Computer Engineering / SE / I / DC /
Practical / 8-8

Line generation using DDA and Bresenhams algorithStudy of


Networking CablesmProgram for basic Arithmetic Operationfinding
largest of 3 numbers using nested-if statement

20013
201431

11

Computer Engg./TE/I/CG/Practical/1-7Computer Engineering / TE / I / CG /


Practical / 1-7Applied Science / FE / I / CP / Practical / 1-12Applied
Science / FE / I / CP / Practical / 2-12Computer Engineering / SE / I / DC /
Practical / 8-8

20013
201431

Source Code
C /C++ Source code:
//Program for basic Arithmetic Operationsfinding
numbers using nested if-else statement

largest

of

three

#include<stdio.h>
main()
{
int a,b,c;
clrscr();
printf("Enter the numbers\n");
printf("a=");
scanf("%d",&a);
printf("b=");
scanf("%d",&b);

//declare variables
//clear screen
//input
//input

c=a+b;
printf("\nSum=%d",c);

//perform addition

c=a-b;
printf("\nDifference=%d",c);

//perform substraction

c=a*b;
printf("\nProduct=%d",c);

//perform multiplication

c=a/b;
printf("\nDivision=%d",c);

//perform division

getch();
}
/*
** pgm Program for generationtion of line style
** DDA & Bresenhams algorithm
**
*/
#
#
#
#
#
#
#

include
include
include
include
include
include
include

by using

<stdio.h>
<graphics.h>
<math.h>
<conio.h>
<stdlib.h>
<bios.h>
<dos.h>

Line generation using DDA and Bresenhams algorithStudy of


Networking CablesmProgram for basic Arithmetic Operationfinding
largest of 3 numbers using nested-if statement

12

Computer Engg./TE/I/CG/Practical/1-7Computer Engineering / TE / I / CG /


Practical / 1-7Applied Science / FE / I / CP / Practical / 1-12Applied
Science / FE / I / CP / Practical / 2-12Computer Engineering / SE / I / DC /
Practical / 8-8

20013
201431

void DDA();
void BRA();
float sign(float k);
int Bitmap();
float sign(float k)
{
float j;
if(k<0) j=-1.0;
if(k==0)j=0.0;
if(k>0) j=1.0;
return j;
}
int bitmap(int no,int bit[100])
{
int i;

//Bitmap method

printf("\n How many no of pattern");


scanf("%d",&no);
printf("\n\t Enter the pattern(0/1)");
for(i=0;i<no;i++)
scanf("%d",&bit[i]);
return;
}
void main()
{
int ch,bit[100],no,x100;
printf("\n\t 1 for
printf("\n\t 2 for
printf("\n\t 3 for
printf("\n\t enter
scanf("%d",&ch);

DDA Algo.");
Bresenhams Algo.");
exit");
your choice");

switch(ch)
{

case 1:
DDADda_Method();
break;
case 2:
BRABrasenham_Method();
break;
case 3:
exit(0);

Line generation using DDA and Bresenhams algorithStudy of


Networking CablesmProgram for basic Arithmetic Operationfinding
largest of 3 numbers using nested-if statement

13

Computer Engg./TE/I/CG/Practical/1-7Computer Engineering / TE / I / CG /


Practical / 1-7Applied Science / FE / I / CP / Practical / 1-12Applied
Science / FE / I / CP / Practical / 2-12Computer Engineering / SE / I / DC /
Practical / 8-8

20013
201431

}
//switch
}
//main
void DDA()
//defination of DDA() function
{
float x,y,x1,y1,x2,y2,dx,dy,e,Length;
int i,gd,gm;
int no,bit[100],x100;
clrscr();
/*Read end points of line */
printf("\n\t Enter the first point x1 \n\t");
scanf("%f",&x1);
printf("\n\t Enter the first point y1 \n\t");
scanf("%f",&y1);
printf("\n\t Enter the first point x2 \n\t");
scanf("%f",&x2);
printf("\n\t Enter the first point x2 \n\t");
scanf("%f",&y2);
clrscr();
detectgraph(&gd,&gm);
//IntialiseInitialize the graphics mode
initgraph(&gd,&gm,"c:\\tc\\bgi");
dx=abs(x2-x1);
dy=abs(y2-y1);
if(dx>=dy)
{
}
else
{

Length=dx;

Length=dy;

}
dx=(x2-x1)/Length;
dy=(y2-y1)/Length;
x=x1+sign(dx);
y=y1+sign(dy);

Line generation using DDA and Bresenhams algorithStudy of


Networking CablesmProgram for basic Arithmetic Operationfinding
largest of 3 numbers using nested-if statement

14

Computer Engg./TE/I/CG/Practical/1-7Computer Engineering / TE / I / CG /


Practical / 1-7Applied Science / FE / I / CP / Practical / 1-12Applied
Science / FE / I / CP / Practical / 2-12Computer Engineering / SE / I / DC /
Practical / 8-8

20013
201431

Bitmap (no,bit[100]);
i=1;
while(i<=Length)
{
x100=(int)x%no;
putpixel(x,y,14*bit[x100]);
x=x+dx;
y=y+dy;
i=i+1;
delay(10);
}//while
getch();
closegraph();
}
void BRA()
//defination of BRA() function
{
int x,y,x1,y1,x2,y2,dx,dy;
int e,m;
int i,gd,gm;
clrscr();
printf("\n\t Enter the first point x1 \n\t");
//Read end points of line
scanf("%d",&x1);
printf("\n\t Enter the first point y1 \n\t");
scanf("%d",&y1);
printf("\n\t Enter the first point x2 \n\t");
scanf("%d",&x2);
printf("\n\t Enter the first point x2 \n\t");
scanf("%d",&y2);
clrscr();
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:\\tc\\bgi");
x=x1;
y=y1;
dx=x2-x1;
dy=y2-y1;

m=dy/dx;

e=m-0.5;

Line generation using DDA and Bresenhams algorithStudy of


Networking CablesmProgram for basic Arithmetic Operationfinding
largest of 3 numbers using nested-if statement

15

Computer Engg./TE/I/CG/Practical/1-7Computer Engineering / TE / I / CG /


Practical / 1-7Applied Science / FE / I / CP / Practical / 1-12Applied
Science / FE / I / CP / Practical / 2-12Computer Engineering / SE / I / DC /
Practical / 8-8

20013
201431

for(i=1;i<=dx;i++)
{
putpixel(x,y,14);
while(e>0)
{
y=y+1;
e=e-1;
}
x=x+1;
e=e+m;
}
getch();
closegraph();
}
float sign(float k)
// defination of sign function
{
float j;
if(k<0)
j=-1.0;
if(k==0)
j=0.0;
if(k>0)
j=1.0;
return j;
}
int Bitmap(int no,int bit[100])
//Bitmap method
{
int i;
printf("\n How many no of pattern");
scanf("%d",&no);
printf("\n\t Enter the pattern(0/1)");
for(i=0;i<no;i++)
{
scanf("%d",&bit[i]);
}
return;
}

Line generation using DDA and Bresenhams algorithStudy of


Networking CablesmProgram for basic Arithmetic Operationfinding
largest of 3 numbers using nested-if statement

16

Computer Engg./TE/I/CG/Practical/1-7Computer Engineering / TE / I / CG /


Practical / 1-7Applied Science / FE / I / CP / Practical / 1-12Applied
Science / FE / I / CP / Practical / 2-12Computer Engineering / SE / I / DC /
Practical / 8-8

20013
201431

3.

Line generation using DDA and Bresenhams algorithStudy of


Networking CablesmProgram for basic Arithmetic Operationfinding
largest of 3 numbers using nested-if statement

17

Anda mungkin juga menyukai