ScanConvertingaStraightLine
Algorithmfordrawinglineusingequation:
Step1:StartAlgorithm
Step2:Declarevariablesx1,x2,y1,y2,dx,dy,m,b,
Step3:Entervaluesofx1,x2,y1,y2.
The(x1,y1)areco-ordinatesofastartingpointoftheline.
The(x2,y2)areco-ordinatesofaendingpointoftheline.
Step4:Calculatedx=x2-x1
Step5:Calculatedy=y2-y1
Step6:Calculatem=
Step7:Calculateb=y1-m*x1
Step8:Set(x,y)equaltostartingpoint,i.e.,lowestpointandxend
equaltolargestvalueofx.
Step9: Check whether the complete line has been drawn if x=xend, stop
Step10: Plot a point at current (x, y) coordinates
Step11: Increment value of x, i.e., x = x+1
Step12: Compute next value of y from equation y = mx + b
Step13: Go to Step9.
Scan Converting a
Straight Line …
Program to draw a line using LineSlopeMethod
1
#include<graphics.h>
#include<stdlib.h>
#include<math.h>
#include<stdio.h>
#include<conio.h>
#include<iostream.h>
classbresen
{
floatx,y,x1,y1,x2,y2,dx,dy,m,c,xend;
public:
voidget();
voidcal();
};
voidmain()
{
bresenb;
b.get();
b.cal();
getch();
}
Voidbresen::get() 2
{
print("Enterstart&endpoints");
print("enterx1,y1,x2,y2");
scanf("%f%f%f%f",sx1,sx2,sx3,sx4)
}
voidbresen::cal()
{
intgdriver=DETECT,gmode,errorcode;
initgraph(&gdriver,&gmode,"");
errorcode=graphresult();
if(errorcode!=grOK)
DDAAlgorithm….
Example:Calculatethepoints
betweenthestartingpoint(1,7)
andendingpoint(11,17).
X
p+1
Y
p+1
DDAAlgorithm….
The advantagesof DDA Algorithm are-
•It is a simple algorithm.
•It is easy to implement.
•It avoids using the multiplication operation which is
costly in terms of time complexity.
ThedisadvantagesofDDAAlgorithmare-
•Thereisanextraoverheadofusingroundoff()
function.
•Usingroundoff()functionincreasestime
complexityofthealgorithm.
•Resultedlinesarenotsmoothbecauseofround
off()function.
•Thepointsgeneratedbythisalgorithmarenot
accurate.