Professional Documents
Culture Documents
LINE& CIRCLE
ALGORITHMS
Basic Concepts in Line
Drawing
In computer line is drawn by calculating intermediate points on the
line path between two end points of line
Analog devices like r andom scan display draws straight line between
two end points by applying horizontal and v ertical deflection voltages in
'x' and 'y' direction
of line
Basic Concepts in Line
Drawing End point of
line
( x2, y2 )
discrete (distinct)
points of line
( x1, y1)
End point of
line
Basic Concepts of Line Drawing
Agorithms
Cartesian slope - intercept equation for straigh line is
y = m*x + b ................................... ( 1 )
if (x1, y1) and (x2, y2) are end points of line then slope of line is calculated
as
y2 -
................
m= y1
x2 - (2)
x1
and b = y1 - m*x1 ................( 3 )
=
Equation ( 4 ) and ( 5 ) decides deflection voltages in analog display devices
So, we must sample line at distinct position and find the nearest pixel to line at
the sample position
= yk + m ................
yk+1
(6)
= xk + 1/m ................( 7 )
xk+1
Equation ( 6 ) and ( 7 ) are useful if we process line from left endpoint to right
end point
Digital Differential Analyzer (DDA) - Line Algorithm
If we process the line from right endpoint to left endpoint and the value of slope of
line | m | < 1 or | m | = 1 then we sample line at unit x interval ( x = -1 ) and
calculate each next values of y as below :
= yk - m ................
yk+1
(8)
Similarly if | m | > 1 and line is processed from right endpoint to left endpoint then
we take y = -1 and each next x values are calculated as :
= xk - 1/m ................( 9 )
xk+1
Digital Differential Analyzer (DDA) - Line Algorithm
x2 - x1 y
=
St ep 4 : xIncrement = x / steps
yIncrement = y2 y- y1
/
steps
Step 3: If abs( x) > abs( y)
then steps = x
Digital Differential Analyzer (DDA) - Line Algorithm
St ep 5 : x = x1 + 0.5* sign(
x) y = y1 + 0.5* sign(
y)
Step 6 : k = 0
while (k < steps)
{
setPixel ( ROUND(x), ROUND(y)) x = x +
xIncrement
y =y + yIncrement
k =k +1
}
Step 7 : End
Digital Differential Analyzer (DDA) - Line Algorithm
It uses only integer calculations that can be used to display display and other
curves
Bresenham's Line Algorithm
Step 1 : Input the two line endpoints and store the left endpoint in ( x 0, y
0)
Step 2 : Load ( x0 , y0 ) into the frame buffer ; that is , plot the first point
St ep 3: Calculat e x, y, 2 y, and 2 y - 2 x and obtain the starting value for
const ant s decision
parameter as
p0 =
Step 4: At each
2 x i along the
y - line starting at k=0, perform the following
test : if p< 0, the next
x point to be plot is (x k + 1, yk) and
k
pk + 1
= pk + 2
y
Bresenham's Line Algorithm
pk + 1
= pk + 2 y-2
x
Step 5 : Repeat step
x t imes
4
Step 5 :
End
Solved Example - Bresenham Line
Algorithm
Use Bresenham line algorithm to rasterize the line from (20,10) and
(30,18)
As pk > 0, so (22,12)
2 2
x = x+1 = 21+1 =
22 y = y+1 = 11+1
= 12 (x,y)=(22,12)
pk = pk+2* y-2* x
= 2+16-20
= -2
Solved Example - Bresenham Line
Solution Algorithm
: st ep value of pk calculation of (x,y) Plot
s point As pk < 0, so (23.12)
-2
3 x = x+1 = 22+1 = 23
y = y = 12
(x,y)=(23,12)
pk = pk+2* y
= -2+16
pk =
14
As pk > 0, so (24,13)
4 14
x = x+1 = 23+1 =
24 y = y+1 = 12+1
=13 (x,y)=(24,13)
pk = pk+2* y-2* x
= 14+16-20
= 10
Solved Example - Bresenham Line
Solution Algorithm
: st ep value of pk calculation of (x,y) Plot
s point As pk > 0, so (25,14)
10
5 x = x+1 = 24+1 =
25 y = y+1 = 13+1
=14 (x,y)=(25,14)
pk = pk+2* y-2* x
= 10+16-20
pk = 6
As pk > 0, so (26,15)
6 6
x = x+1 = 25+1 =
26 y = y+1 = 14+1
= 15 (x,y)=(26,15)
pk = pk+2* y-2* x
= 6+16-20
=2
Solved Example - Bresenham Line
Solution Algorithm
: st ep value of pk calculation of (x,y) Plot
s point As pk > 0, so (27,16)
2
7 x = x+1 =
26+1=27 y = y+1
= 15+1=16
(x,y)=(27,16)
pk = pk+2* y-2*
x
= 2+16-20
Aspkpk < 0, so
= -2 (28,16)
8 -2
x = x+1 =
27+1=28
y = y = 16
(x,y)=(28,16)
pk = pk+2* y
= -2+16
Solved Example - Bresenham Line
Solution Algorithm
: st ep value of pk calculation of (x,y) Plot
s point As pk > 0, so (29,17)
14
9 x = x+1 =
28+1=29 y = y+1
= 16+1=17
(x,y)=(29,17)
pk = pk+2* y-2*
x
= 14+16-20
As
pkpk > 0, so
= 10 (30,18)
10 10
x = x+1 =
29+1=30 y = y+1
= 17+1=18
As we have completed 10 steps and
caculated 10 points we will stop here
C Program of Bresenham Line
while(i<deltax)
Algorithm
#include<conio.h> {
#include<graphics.h> if(pk<0)
#include<math.h> void {
main() x = x+1;
{ y = y;
int gd = DETECT,gm; pk =
int x,y,x1,y1,x2,y2,deltax,deltay,pk; int pk+2*de
i=0; ltay;
printf("\n Enter the first coordinates x1 and y1 "); }
scanf("%d%d",&x1,&y1); if(pk>=0)
printf("\n Enter the second coordinates x2 and y2 "); {
scanf("%d%d",&x2,&y2); x=x+1;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
deltax = x2-x1; y=y+1;
deltay = y2-y1; pk =
x=x1; pk+2*
deltay-
y=y1;
2*delta
pk x;
=(2*deltay) -
}
deltax;
Output of Bresenham Line
Program
Symmetry of a
Circle y
In circle generating algorithms ( -y, x ) ( y, x)
computations can be reduced, if
we consider symmetry of circle
( x,
( -x, y)
Shape of circle is similar in e y)
ach 450
quadrant x
We can draw section of second
quadrant from first quadrant as it ( -x,
is -y ) ( x,
symmetrical around y - axis -y )
Similarly circle section of third and
fourth qaudrant can be drawn ( -y, -x) ( y, -x )
from first and second quadrant as
it
symmetrical around x -axis
Symmetry of a
Circle y
There is also symmetry between ( -y, x ) ( y, x)
o ctants in a
circle
( x,
Two octants divided by 450 and in one ( -x, y)
quadrant are symmetric to each y)
450
other
x
Taking advantage of circle symmetry
in this way , we can generate all
( -x,
pixels of circle by calculating only
-y ) ( x,
points within a sector from x = 0 to -y )
x=y
( -y, -x) ( y, -x )
Bresenham's Circle Algorithm
Step 1 : Read the radius ( r ) of a circle Step 5 : Plot pixels in all octants
as : Plot( x, y )
Step 2: Calculate initial decision variable Plot( y, x)
Plot( -y, x )
PiStep 3: Set x = 0 and y = r Plot( -x, y )
Plot( -x, -y )
Step 4: if (Pi < 0 )
Plot(-y, -x )
{
Plot( y, -x )
x=x+1
Plot( x, -y )
Pi = P i + 4*x + 6
}
elseif ( Pi >= 0 ) Step 6 : Stop
{
x=x+
1 y=y
-1
P =
C Program of Bresenham Circle
#include<stdio.h>Algorithm
#include<conio.h> else if(pk>=0)
#include<graphics.h> {
void main() x=x+1;
{
y=y-1;
int gd=DETECT,gm;
int x,y,xc,yc;
pk=pk
int radius; +4*(x-
float pk; y)+10;
printf("\n Enter the coordinates of center of circle xc and yc: "); }
scanf("%d%d",&xc,&yc); putpixel(xc+x,yc+y,BLUE);
printf("\n Enter the radius of circle: ");
putpixel(xc+y,yc+x,BLUE);
scanf("%d",&radius);
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI"); putpixel(xc-y,yc+x,BLUE);
x=0; putpixel(xc-x,yc+y,BLUE);
y=radius; putpixel(xc-x,yc-y,BLUE);
pk=3-2*radius;
putpixel(xc-y,yc-x,BLUE);
while(x<=y)
{
putpixel(xc+y,yc-x,BLUE);
if(pk<0) putpixel(xc+x,yc-y,BLUE);
{ }
x=x+1; getch();
y=y;
closegraph();
pk=pk
+4*x+6 }
Output of Bresenham Circle
Program
Midpoint Circle Algorithm
Step 1 : Input radius 'r' and circle center ( xc , yc ), and obtain the first point
on circumference of a circle whose center is at origin as :
( x0 , y0 ) = (0,r)
p0 = ( 5/4 ) - r
k
if p < 0, the next point along the circle centered on (0,0) is (x k
otherwise, ,y k ) next point along the circle is (x
the k + 1 , y k - 1 ) and
+1
pk + 1 = pk + 2*x k +1 +1
Midpoint Circle Algorithm
otherwise if p k < 0, the next points along the circle is (xk +1, yk - 1)
and
= pk + 2*x k + 1 + 1 - 2*y k + 1
pk + 1
Step 5: Move each calculated pixel position ( x, y ) on to the circular path centered
on ( xc , yc ) and plot the coordinate values as :
x = x + xc and y = y + yc
Not a
polygon
Types of Polygons
1) Convex Polygon :
Any line segment with endpoints on the boundary passes through only
interior points between its endpoints
Convex Polygon
Types of Polygons
2) Simple Polygon :
3) Concave Polygon :
A line may be found which meets its boundary
more than twice ( non-convex and simple )
4) Star-shaped Polygon
5) Self-intersecting Polygon
Self-intersecting Polygon
Types of Polygons
6) Star Polygon
Star Polygon
Polygons : Inside - Out side Test
Area filling algorithms with colors and other graphics process require to identify
i nterior and e xterior regions of objects such as polygons
It is not always clear that which region in xy plane should be called as interior
region and which region should called as exterior region
Graphics package uses following two methods to identify interior regions of objects
In odd-even rules straight line is drawn from any point 'P' to distant point outside
the coordinate extent of object or polygon
The path of straight line should not i ntersect or pass through any vertices of a
polygon
If the number of edges crossed by this line is o dd then point 'P' is interior point of
the polygon
If the number of edges crossed by this line is e ven then point 'P' is exterior point of
the polygon
Odd - Even Rule - Example
Imagine a straight line is drawn from any point 'P' to distant point outside
the coordinate extent of object or polygon
The path of straight line should not i ntersect or pass through any vertices of
a polygon
Nonzero Winding Number
Rule
As we move along the line from position 'P' tothe distant point, we calculate n
umber
of edges that crosses the lin in each direction
1 is added to the winding number if edge of the polygon crosses the line from right
to
left
1 is subtracted from the winding number if edge of the polygon crosses the line from
left to right
If the final value of the winding number after all crossings have been calculated is
n onzero thenpoint 'P' is i nterior point of polygon
If the final value of the winding number is z ero then point 'P' is exterior point
Nonzero Winding Number Rule -
Example
1)Draw a straight line point from points H and K as
shown in figure
4- 8- 4- 8-
Connect ed Connect ed Connect ed Connect ed
Algorithm Algorithm Algorithm Algorithm
Scan Line Polygon Fill Algorithm
For each scan that crosses the polygon, the
algorithm locates intersection point between
s can line and edge of polygon as shown in fig. y
We can fill the polygon with color by taking an interior point (seed point) and then
start filling color from interior to outward till the boundary of polygon is reached
If the boundary is specified in single color, the fill algorithm proceeds outward pixel
by pixel until the boundary color is identified
The algorithm starts from interior point ( x, y ) and checks the neighboring pixels
to decide whether they are of boundary color or not
If not, those pixels are painted with fill color and and their neighbors are tested
This process continues till all the pixels upto the boundary color for the area have
been tested
1 2 3
1
8 4
4 2
7 6 5
3
current pixel
4 - Connected Boundary-Fill Algorithm
Step 4: Change the default color with fill color at (x, y-1 )
the seed point
Step 4: Change the default color with fill color at ( x-1, y (x, y-1 ) (x+1, y -1
-1)
the seed point
Step 6: Exit
Output of Boundary Fill 8-Connected
Program
Flood-Fill Algorithm
Step 4: If all the interior pixels have been painted and no pixel of old color is
found then go to step 5
Step 5: Stop
C Program of Flood Fill
Algorithm
#include<stdio.h> void flood(int x,int y,int fillcolor,int defaultcolor)
#include<conio.h> {
#include<graphics.h> if(getpixel(x,y)==defaultcolor)
void flood(int x,int y,int fillcolor,int defaultcolor); {
void main() delay(1);
putpixel(x,y,fillcolor);
{
flood(x+1,y,fillcolor,defaultcolor);
int oldcolor;
flood(x,y+1,fillcolor,defaultcolor);
int gd = DETECT,gm;
flood(x-1,y,fillcolor,defaultcolor);
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI"); flood(x,y-1,fillcolor,defaultcolor);
rectangle(100,100,300,300); }
oldcolor = getpixel(120,120); }
flood(120,120,GREEN,oldcolor);
getch();
closegraph();
}
Output of Flood Fill
Program
Scan Conversion
Video display device of a computer converts binary values ' 0' or ' 1' into pixel's on and
off s tate
' 0' means pixel is off and ' 1' means pixel is o n
Using this information graphics computer draws image with d istinct dots
Any image or object can be drawn from d ense matrix of dots or points or pixel
Suppose point P( 2.50, 1.75 ) is a point in an image area, the coordinates of point are r
eal
numbers. We need to scan convert this point to draw it on display device. For scan
converting this point we only take integer part of the point like FLOOR(x) and
FLOOR(y) which equal to P( 2, 1 ). Then this point is point is drawn on screen.
y Location of point P( 2, 1) after
scan conversion
2
first row second column
1
0 1 2 3 4 x
Scan Conversion Process of Straight
Line
y-
axis
Straight line
y2
y1
x-
axis
x1 x2
Sampling of line
Frame Buffers
Frame buffer is a portion of Random Accesss Memory (RAM) which stores picture
defination (information)
This memory area holds set of intensity values for all the screen points
Stored intensity values are then taken from frame buffer and painted on
screen on row (scan line) at a time
System Bus
I/O Devices
Monitor : It is output display device like CRT, which shows images and
pictures
System bus : This is the main bus that connects main components of
computer system, it transfers d ata, a ddresses and also decide control
Character Generation Methods
Out of these 24 line segments only those line segments are drawn which
are required to display a particular character
The pattern of lines for each character are stored in 2 4 bit code, each bit in
this code is a line segment
For example - 24 bit code for character ' A' is 0 11 0000 0011 1100 1110 0001
Similarly 24 bit code for character ' M' is 0 000 0011 0000 1100 1111 0011
Starbust Method
Disadvantages of starbust method :
5x7 array is commonly used to draw characters but, 7 x9 and 9 x13 are also
used
Higher resolution devices like inkjet printers and laser printers even use array
of more than 1 00x100
Bitmap Method
dot matrix with 5
columns and 7 dot (pixel)
rows