




















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





















(Marks 18)
Basic concepts in line drawing:
We say these computers have vector graphics capability (the line is a vector). The process is turning on pixels for a line segment is called vector generation and algorithm for them is known as vector generation algorithm.
Problems of vector generation:
Line is straight but width is no constant. Brightness of line dependent on orientation of the line. Calculate only an approximate line length. Reduce the calculations using simple integer arithmetic.
Line Drawing Algorithms
Step1: Start Algorithm
Step2: Declare x 1 ,y 1 ,x 2 ,y 2 ,dx,dy,x,y as integer variables.
Step3: Enter value of x 1 ,y 1 ,x 2 ,y 2.
Step4: Calculate dx = x 2 - x 1
Step5: Calculate dy = y 2 - y 1
Step6: If ABS (dx) > ABS (dy) Then step = abs (dx) Else
Step7: xinc=dx/step yinc=dy/step assign x = x 1 assign y = y 1
Step8: Set pixel (x, y)
Step9: x = x + xinc y = y + yinc Set pixels (Round (x), Round (y))
Step10: Repeat step 9 until x = x 2
(Marks 18)
Program:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int x1,y1,x2,y2,dx,dy,length,i;
float x,y,xinc,yinc;
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\turboc3\bgi");
printf("Enter the starting coordinates");
scanf("%d%d",&x1,&y1);
printf("Enter the ending coordinates");
scanf("%d%d",&x2,&y2);
dx=x2-x1;
dy=y2-y1;
if(abs(dx)>abs(dy))
length=abs(dx);
else
length=abs(dy);
xinc=dx/(float)length;
(Marks 18)
Initial values for
X = 0 + 0.5 * Sign (-1) =-0. Y = 0 + 0.5 * Sign (-1) =-0.
Tabulating the results of each iteration in the step 6 we get,
It’s an accurate and efficient raster line generating Algorithm. Bresenham's line-drawing algorithm uses an iterative scheme. A key feature of the algorithm is that it requires only integer data and simple arithmetic. This makes the algorithm very efficient and fast.
Procedure for Bresenham’s algorithm:
1.Read line end points (x1,y1) and (x2,y2) such that they are not equal.
3.x=x1 and y=y
4.e=2*dy-dx
5.i=
(Marks 18)
6.plot (x,y)
7.while (e>0)
{
x=y+
e=e-2*dx
}
x=x+
e=e+2*dy
8.i=i+
Program - Bresenham Line Drawing Algorithm -:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
{
int gd=DETECT,gm=DETECT,length,dx,dy,x1,y1,x2,y2,e;
float x,y;
clrscr();
printf("\n enter coordinates of x1,y1,x2,y2");
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
dx=x2-x1;
dy=y2-y1;
e=2*(dy-dx);
x=x1;
y=y1;
initgraph(&gd,&gm,"c://Turboc3//BGI");
while(x<=x2) {
if(e<0)
(Marks 18)
DDA Line Drawing Algorithm vs Bresenhams Line Drawing Algorithm
Arithmetic uses floating points Integer points
Operations (^) Uses multiplication and division in its
operations.
Uses only subtraction and addition in its operations.
Speed slowly than Bresenhams algorithm faster than DDA
Accuracy &
Efficiency
not as accurate and efficient more efficient &much accurate
Drawing (^) DDA algorithm can draw circles and curves
but that are not as accurate as Bresenhams algorithm.
Draw circles and curves with much more accuracy than DDA algorithm.
Round Off DDA algorithm round off the coordinates to Bresenhams algorithm does
not round off but takes the
(Marks 18)
integer that is nearest to the line. incremental value in its operation.
Expensive expensive. less expensive
Circle generating algorithm:
Symmetry of circle:
A circle is a geometric figure which is round, and can be divided into 360 degrees. A circle is a symmetrical figure which follows 8-way symmetry.
8- Way symmetry: Any circle follows 8-way symmetry. This means that for every point (x,y) 8 points can be plotted. These (x,y), (y,x), (-y,x), (-x,y), (-x,-y), (-y,-x), (y,-x), (x,-y).
Drawing a circle: To draw a circle we need two things, the coordinates of the centre and the radius of the circle.
Here r is the radius of the circle. If the circle has origin (0,0) as its centre then the above equation can be reduced to x2 + y2 = r
Bresenham’s Algorithm
We cannot display a continuous arc on the raster display. Instead, we have to choose the nearest pixel position to complete the arc.
From the following illustration, you can see that we have put the pixel at (X, Y) location and now need to decide where to put the next pixel − at N (X+1, Y) or at S (X+1, Y-1).
(Marks 18)
Program : bresenhams circle drawing algorihm
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<dos.h>
#include<graphics.h>
void bressn(int,int,int);
void plot(int,int,int,int);
void plot(int xc,int yc,int x,int y)
{
putpixel(xc+x,yc+y,WHITE);
putpixel(xc-x,yc+y,RED);
putpixel(xc-x,yc-y,GREEN);
putpixel(xc+x,yc-y,YELLOW);
putpixel(xc+y,yc+x,WHITE);
putpixel(xc-y,yc+x,GREEN);
putpixel(xc-y,yc-x,YELLOW);
putpixel(xc+y,yc-x,RED);
}
void bressn(int r,int xc,int yc)
{
int d,x,y;
d=3-(2*r);
x=0;
y=r;
(Marks 18)
while(x<y)
{
plot(xc,yc,x,y);
x=x+1;
if(d<0)
{
d=d+(4*x)+6;
}
else
{
y=y-1;
d=d+4*(x-y)+10;
}
plot(xc,yc,x,y);
}
}
void main()
{
int gd,gm;
int xc,yc,radius;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:\turboc3\bgi");
printf("\n enter xc,yc,radius");
scanf("%d%d%d",&xc,&yc,&radius);
bressn(xc,yc,radius);
(Marks 18)
Inside-Outside Test
This method is also known as counting number method. While filling an object, we often need to identify whether particular point is inside the object or outside it. There are two methods by which we can identify whether particular point is inside an object or outside.
Odd-Even Rule
Nonzero winding number rule
Odd-Even Rule
In this technique, we will count the edge crossing along the line from any point (x,y) to infinity. If the number of interactions is odd, then the point (x,y) is an interior poin t; and if the number of interactions is even, then the point (x,y) is an exterior point. The following example depicts this concept.
(Marks 18)
From the above figure, we can see that from the point (x,y), the number of interactions point on the left side is 5 and on the right side is 3. From both ends, the number of interaction points is odd, so the point is considered within the object.
Nonzero Winding Number Rule
This method is also used with the simple polygons to test the given point is interior or not.
Give the value 1 to all the edges which are going to upward direction and all other -1 as direction values.
Check the edge direction values from which the scan line is passing and sum up them.
If the total sum of this direction value is non-zero, then this point to be tested is an interior point, otherwise it is an exterior point.
In the above figure, we sum up the direction values from which the scan line is passing then the total is 1 – 1 + 1 = 1; which is non-zero. So the point is said to be an interior point.
Polygon filling:
Polygon filling algorithms are classified as: seed fill algorithm and scan line algorithm.
One way to fill polygon is to start from given “seed” , point known to be inside the polygon and
highlight outward from this point i.e. neighboring pixels until we encounter the boundary pixels.
This approach is called seed fill.
Seed fill algorithm further classified as flood fill and boundary fill algorithm.
Algorithms the fill interior defined regions are flood fill algorithms; those that fill boun dary defined
regions are called boundary fill algorithm.
Another approach to fill the polygon is to apply the inside test i.e. to check whether the pixels is
inside the polygon or outside the polygon and then highlight pixels which lie inside the polygon.
This approach is known as scan-line algorithm.
(Marks 18)
Program:
#include<stdio.h> #include<conio.h> #include<graphics.h> #include<dos.h> void fillcolor(int,int,int,int); void main() { int gd,gm; detectgraph(&gd,&gm); initgraph(&gd,&gm,"c:\turboc3\bgi"); rectangle(50,50,100,100); fillcolor(55,55,0,11); getch(); closegraph(); } void fillcolor(int x,int y,int old_color,int new_color) { if(getpixel(x,y)==old_color) { delay(5); putpixel(x,y,new_color); f illco lo r( x + 1 ,y,o ld _ co lo r ,ne w_ co lo r); f illco lo r(x - 1 ,y ,o ld _ co lo r ,ne w_ co lo r); f illco lo r( x ,y + 1 ,o ld _ co lo r ,ne w_ co lo r); f illco lo r( x ,y - 1 ,o ld _ co lo r ,ne w_ co lo r); } }
8−Connected Regions
From a given pixel, the region that you can get to by a series of 8-way moves (north, south, east,
west, NE, NW, SE, SW)
void fillcolor(int x,int y,int old_color,int new_color) { if(getpixel(x,y)==old_color) { delay(5); putpixel(x,y,new_color); f illco lo r( x + 1 ,y,o ld _ co lo r ,ne w_ co lo r); f illco lo r(x - 1 ,y ,o ld _ co lo r ,ne w_ co lo r); f illco lo r( x ,y + 1 ,o ld _ co lo r ,ne w_ co lo r); f illco lo r( x ,y - 1 ,o ld _ co lo r ,ne w_ co lo r); f illco lo r( x + 1 ,y + 1 ,o ld _ co lo r ,ne w_ co lo r);
(Marks 18)
f illco lo r( x + 1 ,y - 1 ,o ld _ co lo r ,ne w_ co lo r); f illco lo r(x - 1 ,y - 1 ,o ld _ co lo r ,ne w_ co lo r); f illco lo r(x - 1 ,y + 1 ,o ld _ co lo r ,ne w_ co lo r); } }
Program:
#include<stdio.h> #include<conio.h> #include<graphics.h> #include<dos.h> void fillcolor(int,int,int,int); void main() { int gd,gm; detectgraph(&gd,&gm); initgraph(&gd,&gm,"c:\turboc3\bgi"); rectangle(50,50,100,100); fillcolor(55,55,0,11); getch(); closegraph(); } void fillcolor(int x,int y,int old_color,int new_color) { if(getpixel(x,y)==old_color) { delay(5); putpixel(x,y,new_color); f illco lo r( x + 1 ,y,o ld _ co lo r ,ne w_ co lo r); f illco lo r(x - 1 ,y ,o ld _ co lo r ,ne w_ co lo r); f illco lo r( x ,y + 1 ,o ld _ co lo r ,ne w_ co lo r); f illco lo r( x ,y - 1 ,o ld _ co lo r ,ne w_ co lo r); f illco lo r( x + 1 ,y + 1 ,o ld _ co lo r ,ne w_ co lo r); f illco lo r( x + 1 ,y - 1 ,o ld _ co lo r ,ne w_ co lo r); f illco lo r(x - 1 ,y - 1 ,o ld _ co lo r ,ne w_ co lo r); f illco lo r(x - 1 ,y + 1 ,o ld _ co lo r ,ne w_ co lo r); } }
(Marks 18)
closegraph(); } void boundary_fill(int x,int y,int fcolor,int bcolor) { if((getpixel(x,y)!=bcolor)&&(getpixel(x,y)!=fcolor)) { delay(5); putpixel(x,y,fcolor); b o u nda ry_ f ill( x + 1 ,y ,f co lo r ,bco lo r); b o u nda ry_ f ill( x - 1 ,y ,f co lo r ,bco lo r); b o u nda ry_ f ill( x ,y + 1 ,f co lo r ,bco lo r); b o u nda ry_ f ill( x ,y - 1 ,f co lo r ,bco lo r); } }
8- connected boundary fill Algorithm:-
boundary_fill(x, y, f_colour, b_colour)
{
if(getpixel(x, y)! = b_colour && getpixel(x, y)! = f_colour) { putpixel(x, y, f_colour); boundary_fill(x + 1, y, f_colour, b_colour); boundary_fill(x - 1, y, f_colour, b_colour); boundary_fill(x, y + 1, f_colour, b_colour); boundary_fill(x, y - 1, f_colour, b_colour); boundary_fill(x + 1, y + 1, f_colour, b_colour); boundary_fill (x - 1, y - 1, f_colour, b_colour); boundary_fill(x + 1, y - 1, f_colour, b_colour); boundary_fill(x - 1, y + 1, f_colour, b_colour); }
}
Program:
#include<stdio.h> #include<conio.h> #include<graphics.h> #include<dos.h> void boundary_fill(int,int,int,int); void main() { int gd,gm; detectgraph(&gd,&gm);
(Marks 18)
initgraph(&gd,&gm,"c:\turboc3\bgi"); rectangle(50,50,100,100); bo u ndary_ f ill ( 55 , 55 , 6 , 1 5 ) ; getch(); closegraph(); } void boundary_fill(int x,int y,int fcolor,int bcolor) { if((getpixel(x,y)!=bcolor)&&(getpixel(x,y)!=fcolor)) { delay(5); putpixel(x,y,fcolor); b o u nda ry_ f ill( x + 1 ,y ,f co lo r ,bco lo r); b o u nda ry_ f ill( x - 1 ,y ,f co lo r ,bco lo r); b o u nda ry_ f ill( x ,y + 1 ,f co lo r ,bco lo r); b o u nda ry_ f ill( x ,y - 1 ,f co lo r ,bco lo r); b o u nda ry_ f ill( x + 1 ,y + 1 ,f co lo r ,bco lo r); b o u nda ry_ f ill( x - 1 ,y + 1 ,f c o lo r ,bco lo r); b o u nda ry_ f ill( x + 1 ,y - 1 ,f c o lo r ,bco lo r); b o u nda ry_ f ill( x - 1 ,y - 1 ,f c o lo r ,bco lo r); } }
void fillcolor(int x,int y,int old_color,int new_color) { if(getpixel(x,y)==old_color) { putpixel(x,y,new_color); fillcolor(x+ 1 ,y,old_color,new_color); fillcolor(x- 1 ,y,old_color,new_color); fillcolor(x,y+ 1 ,old_color,new_color); fillcolor(x,y- 1 ,old_color,new_color); } }
boundary_fill(x, y, f_colour, b_colour) { if(getpixel(x, y)! = b_colour && getpixel(x, y)! = f_colour) { putpixel(x, y, f_colour); boundary_fill(x + 1, y, f_colour, b_colour); boundary_fill(x, y + 1, f_colour, b_colour); boundary_fill(x - 1, y, f_colour, b_colour); boundary_fill(x, y – 1, f_colour, b_colour); } }