Raster Scan Graphics
Raster Scan Graphics
(Marks 18)
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.
Step7: xinc=dx/step
yinc=dy/step
assign x = x1
assign y = y1
Step9: x = x + xinc
y = y + yinc
Set pixels (Round (x), Round (y))
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");
scanf("%d%d",&x1,&y1);
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;
yinc=dy/(float)length;
x=x1
y=y1
putpixel(x,y,10);
for(i=0;i<length;i++)
putpixel(x,y,10);
x=x+xinc;
y=y+yinc;
delay(10);
getch();
closegraph();
Example Consider the line from (0, 0) to (-6, -6). Use the simple DDA algorithm line.
Xl = 0 Y1 = 0
X2 = - 6 Y2 = - 6
Length = l X2-X1l |Y2-Y1l = 6
∆X = ∆Y = -1
Initial values for
X = 0 + 0.5 * Sign (-1) =-0.5
Y = 0 + 0.5 * Sign (-1) =-0.5
Tabulating the results of each iteration in the step 6 we get,
2. Bresenham's Algorithm.
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.
1.Read line end points (x1,y1) and (x2,y2) such that they are not equal.
4.e=2*dy-dx
5.i=1
6.plot (x,y)
7.while (e>0)
x=y+1
e=e-2*dx
x=x+1
e=e+2*dy
8.i=i+1
10. Stop.
#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)
{
x=x+1;
y=y;
e=e+2*dy;
}
else
{
x=x+1;
y=y+1;
e=e+2*(dy-dx);
}
putpixel(x,y,15);
}
getch();
closegraph();
}
Problem: - Consider the line from (5, 5) to (13, 9). Use the Bresenham's algorithm to rasterize the
line.
Solution :-
Evaluating steps 1 through 4 in the Bresenham’s algorithm we have,
Given: -
step 1 :- x1 = 5 , y1 = 5 and x2 = 13 , y2 = 9 .
step 2 :- Δx = | 13 - 5| =8 Δy = | 9 - 5 | = 4
step 3 :- x = 5 , y = 5
step 4 :- e = 2 * Δy - Δx = 2 * 4 - 8 = 0 .
Tabulating the results of each iteration in the step 5 through 10.
Operations Uses multiplication and division in its Uses only subtraction and addition
operations. in its operations.
Accuracy & not as accurate and efficient more efficient &much accurate
Efficiency
Drawing DDA algorithm can draw circles and curves Draw circles and curves with much
but that are not as accurate as Bresenhams more accuracy than DDA algorithm.
algorithm.
Round Off DDA algorithm round off the coordinates to Bresenhams algorithm does
not round off but takes the
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 = r2
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).
6. stop
#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);
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);
int d,x,y;
d=3-(2*r);
x=0;
y=r;
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");
scanf("%d%d%d",&xc,&yc,&radius);
bressn(xc,yc,radius);
getch();
closegraph();
Example :
Calculate the pixel position along the circle path with radius r=10 centered on the origin using
Bresenham’s circle drawing algorithm from point (0,10) to point x=y.
Solution:
The value of d is
d=3-2r=3-2*10=-17
x y d
0 10 -17
1 10 -11
2 10 -1
3 10 13
4 9 -5
5 9 17
6 8 11
7 7 13
Polygon:
A polygon is any 2-dimensional shape formed with straight lines. Triangles, quadrilaterals,
pentagons, and hexagons are all examples of polygons.
Types of polygon:
The difference between convex and concave polygons lies in the measures of their angles.
For a polygon to be convex, all of its interior angles must be less than 180 degrees. Otherwise, the
polygon is concave.
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.
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.
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.
• Start at a point inside a region− Replace a specified interior color (old color) with fill color
• Fill the 4−connected or 8−connected region until all interior points being replaced.
4−Connected Regions
From a given pixel, the region that you can get to by a series of 4 way moves (north, south, east,
west)
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);
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);
}
}
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)
fillcolor(x+1,y-1,old_color,new_color);
fillcolor(x-1,y-1,old_color,new_color);
fillcolor(x-1,y+1,old_color,new_color);
}
}
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);
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);
fillcolor(x+1,y+1,old_color,new_color);
fillcolor(x+1,y-1,old_color,new_color);
fillcolor(x-1,y-1,old_color,new_color);
fillcolor(x-1,y+1,old_color,new_color);
}
}
ADVANTAGE
• Flood fill algorithm is simplest algorithm.
DISADVANTAGE
• Flood fill algorithm is slow.
• For large polygon flood fill algorithm is fail because it requires a large frame.
• In boundary fill algorithm Recursive method is used to fill the whole boundary.
• Start at a point inside a region.
• Paint the interior outward toward the boundary.
• The boundary is specified in a single color.
#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);
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
rectangle(50,50,100,100);
boundary_fill(55,55,6,15);
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);
boundary_fill(x+1,y,fcolor,bcolor);
boundary_fill(x-1,y,fcolor,bcolor);
boundary_fill(x,y+1,fcolor,bcolor);
boundary_fill(x,y-1,fcolor,bcolor);
}
}
#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);
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
rectangle(50,50,100,100);
boundary_fill(55,55,6,15);
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);
boundary_fill(x+1,y,fcolor,bcolor);
boundary_fill(x-1,y,fcolor,bcolor);
boundary_fill(x,y+1,fcolor,bcolor);
boundary_fill(x,y-1,fcolor,bcolor);
boundary_fill(x+1,y+1,fcolor,bcolor);
boundary_fill(x-1,y+1,fcolor,bcolor);
boundary_fill(x+1,y-1,fcolor,bcolor);
boundary_fill(x-1,y-1,fcolor,bcolor);
}
}
• To successfully fill in a polygon three main components will be used: Edge Buckets, an Edge
Table and an Active List.
• These components will contain an edge’s information, hold all of the edges that compose the
figure and maintain the current edges being used to fill in the polygon, respectively.
• When a scan line intersects an edge endpoint, it intersects two edges. Consider, Two cases:
– Case A: edges are monotonically increasing or decreasing, we should consider this as only ONE
edge intersection
– Case B: edges reverse direction at endpoint; we should consider this as TWO edge intersections
Stepwise Procedure
4. Store the initial x value (x1),y values (y1 and y2) and x increment dx from scan line to scan line
for each edge in the array edges[n] [4].
a. While doing this check that y1> y2, if not , interchange y1 and y2 corresponding x1 and x2 so that
for each edge, y1 represents its maximum y coordinate and y2 represents its minimum y coordinate.
5. Sort the rows of array, edges[n] [4] in descending order of y1, descending order of y2, and
ascending order of x2
i {edge is active}
b. Else
8. Compute the x intersects for all active edges for current y value. Initially x intersect is x1 and x
intersects for successive y values can be given as
a. x(i+1)dxi+dx
9. if x intersect is vertex i.e. x intersect =x1 and y=y1 then apply vertex test to check whether to
consider one intersect or two intersects. Store all x intersects in the x intersects array.
11. Extract pairs of intersects from the sorted x intersect arr ay.
12. Pass pairs of x values to line drawing routine to draw corresponding line segments.
13.-set y=y-1
15. Stop
Program:
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
main()
{
int n,i,j,k,gd,gm,dy,dx;
int x,y,temp;
int a[20][2],xi[20];
float slope[20];
clrscr();
printf("\n\n\tEnter the no. of edges of polygon : ");
scanf("%d",&n);
printf("\n\n\tEnter the cordinates of polygon :\n\n\n ");
for(i=0;i<n;i++)
{
printf("\tX%d Y%d : ",i,i);
scanf("%d %d",&a[i][0],&a[i][1]);
}
a[n][0]=a[0][0];
a[n][1]=a[0][1];
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
for(i=0;i<n;i++)
{
line(a[i][0],a[i][1],a[i+1][0],a[i+1][1]);
}
getch();
for(i=0;i<n;i++)
{
dy=a[i+1][1]-a[i][1];
dx=a[i+1][0]-a[i][0];
if(dy==0) slope[i]=1.0;
if(dx==0) slope[i]=0.0;
if((dy!=0)&&(dx!=0)) /*- calculate inverse slope -*/
{
slope[i]=(float) dx/dy;
}
}
for(y=0;y< 480;y++)
{
k=0;
for(i=0;i<n;i++)
{
if( ((a[i][1]<=y)&&(a[i+1][1]>y))||
((a[i][1]>y)&&(a[i+1][1]<=y)))
{
xi[k]=(int)(a[i][0]+slope[i]*(y-a[i][1]));
k++;
}
}
setcolor(35);
for(i=0;i<k;i+=2)
{
line(xi[i],y,xi[i+1]+1,y);
getch();
}
Scan conversion
• Scan conversion or scan converting rate is a video processing technique for changing the
vertical / horizontal scan frequency of video signal for different purposes and applications.
• The application of scan conversion: video projectors, cinema equipment, TV and video
capture cards, standard and HDTV televisions, LCD monitors, radar displays and many
different aspects of picture processing.
• There are two distinct methods for changing a picture's data rate:
• Analog Methods
(Non retentive, memory-less or real time method)
• This conversion is done using large numbers of delay cells and is appropriate for analog
video.It may also be performed using a specialized scan converter vacuum tube.
• In this case polar coordinates (angle and distance) data from a source such as a radar
receiver, so that it can be displayed on a raster scan (TV type) display.
• Digital methods
(Retentive or buffered method)
• In this method, a picture is stored in a line or frame buffer with n1 speed (data rate) and is
read with n2 speed.
Frame buffer:
• Each screen pixel corresponds to a particular entry in a 2D array residing in memory. This
memory is called a frame buffer or a bit map.
• The number of rows in the frame buffer equals to the number of raster lines on the display
screen. The number of columns in this array equals to the number of pixels on each raster
line.
• Frame buffer is a large part of computer memory used to store display image. Different kind
of memory can be used for frame buffers like drums, disk or IC – shift registers.
• To generate a pixel of desired intensity to read the disk or drum. The information stored in
disk or drum is in digital for, hence it is necessary to convert it into analog from using DAC
and then this analog signal is used to generate the pixel.
Character Generation
• Most of the times characters are builts into the graphics display devices, usallay as hardware
but sometimes through software.
– Stroke method
– Starbust method
– Bitmap method
1. Stroke method
• The small series of line segments are drawn like a strokes of a pen to form a character as
shown in figure.
• Here it is necessary to decide which line segments are needed for each character and
• It does this by changing the length of the line segments used for character drawing.
2. Starbust method
• In this method a fix pattern of line segments are used to generate characters.
• Out of 24 line segments, segments required to display for particular character, are
highlighted
1. The 24-bits are required to represent a character. Hence more memory is required
2. Requires code conversion software to display character from its 24-bit code
3. Bitmap Method
• Also known as dot matrix because in this method characters are represented by an array of
dots in the matrix form.
• It’s a two dimensional array having columns and rows : 5 X 7 as shown in figure.