2.raster Scan Algo
2.raster Scan Algo
COMPUTER GRAPHICS
Output primitives
TEXT BOOK :
COMPUTER GRAPHICS
therefore
1 3 4 5 6 7
y = Mx + B
RISE y2-y1
where SLOPE =
= RUN x2-x1
M = [(y2-y1)/(x2-x1)]
B = [-(y2-y1)/(x2-x1)]y1 + y1
DDA algorithm is an incremental scan conversion
method. Here we perform calculations at each
step using the results from the preceding step.
The characteristic of the DDA algorithm is to take
unit steps along one coordinate and compute the
corresponding values along the other coordinate.
Xl = 0 Y1 = 0
X2 = 4 Y2 = 6
Length = |Y2-Y1l = 6
∆X = |X2- Xl | / Length
= 4 =0.5
6
∆Y = |Y2- Yl | / Length
= 6/6=1 =1
Initial value for
X = 0 + 0.5 * Sign ( 4 ) = 0.5 =1
6
Y = 0 + 0.5 * Sign (1) = 0.5=1
Tabulating the results of each iteration in the step 6 we get
Example 2 Consider the line from (0, 0) to (-6, -6). Use the
simple DDA algorithm line.
Xl = 0 Y1 = 0
X2 = - 6 Y2 = - 6
1. It is the simplest algorithm and it does not require special skills for
implementation.
2. It is a faster method for calculating pixel positions than the direct
use of equation y = mx + b. It eliminates the multiplication in the
equation by making use of raster characteristics, so that appropriate
increments are applied in the x or y direction to find the pixel positions
along the line path.
= 𝑚(𝑋𝑘 + 1) + 𝑏 − 𝑌𝑘
𝑑𝑢𝑝𝑝𝑒𝑟 = (𝑦𝑘 + 1) − 𝑦
and
= 𝑌𝑘 + 1 − 𝑚(𝑋𝑘 + 1) − 𝑏
= 2𝑑𝑦 ∙ 𝑥𝑘 − 2𝑑𝑥 ∙ 𝑦𝑘 + 𝐶
1)
𝑝𝑘 = 𝑑𝑥(𝑑𝑙𝑜𝑤𝑒𝑟 − 𝑑𝑢𝑝𝑝𝑒𝑟 )
given by:
= 2𝑑𝑦 ∙ 𝑥𝑘 − 2𝑑𝑥 ∙ 𝑦𝑘 + 𝐶
The sign of the decision parameter pk is the same as that
of dlower – dupper.
If pk is negative, then choose the lower pixel, otherwise
choose the upper pixel.
Bresenham’s Line Drawing algo M<1
1.Input any two line endpoints and store the left end point in (x0,y0).
2.Load (x0,y0) into frame buffer ,that is plot the first point.
3.Calcualte constant ∆x, ∆y,2∆y and 2∆y,2∆x and obtain the starting
value for the decision parameter as
P0=2∆y - ∆x
4.At each xk along the line, starting at k=0,perform following test.
if pk<0 the next point to plot is (xk+1,yk) &
Pk+1=pk + 2∆y (- value)
Otherwise, the next point to plot is (xk+1,yk+1) &
Pk+1=pk+2∆y - 2∆x (+ value)
5.Display the point put pixel( x, y, Intensity(Color))
24
• Example: We digitize the line with endpoints (20,10)(30,18)
This line has a slope of 0.8, with
Soln: ∆x=30-20=10 ∆y =18-10=8
P0=2∆y-∆x =16-10=6 (error factor)
We plot the initial point (x0,y0)=(20,10) and determine
Successive pixel position along the line path from the decision
parameter as:
po=6 1 pixel plot (21,11)
Pk+1=pk+2∆y-2∆x 6+16-20=2 2 pixel plot (22,12)
Pk+2=pk+2∆y-2∆x 2+16-20= -2 3 pixel plot (23,12)
pk+3=pk+2∆y -2+16=14 4 pixel plot (24,13)
Pk+4=pk+2∆y-2∆x 14+16-20=10 5 pixel plot (25,14)
Pk+5=pk+2∆y-2∆x 10+16-20=6 6 pixel plot(26,15)
Pk+6=pk+2∆y-2∆x 6+16-20=2 7 pixel plot(27,16)
25
Pk+7=pk+2∆y-2∆x 2+16-20= -2 8 pixel plot(28,16)
pk+8=pk+2∆y -2+16= 14 9 pixel plot (29,17)
Pk+9=pk+2∆y-2∆x 14+16-20= 10 10 pixel plot(30,18)
k Pk(e) Xk+1,yk+1
0 6 (21,11)
1 2 (22,12)
2 -2 (23,12)
3 14 (24,13)
4 10 (25,14)
5 6 (26,15)
6 2 (27,16)
7 -2 (28,16)
8 14 (29,17)
9 10 (30,18)
27
• Example.
Consider the line from (0, 0) to (-8,- 4), use general Bresenham’s
line algorithm to rasterize this line. Evaluate and tabulate all
the steps involved. [10 marks]
• Solution: Given data, (x1,y1)= (0, 0) (x2 , y2)= (-8,-4)
∆x=x2-x1=-8-0=8 ∆y=y2-y1=-4-0=4
Decision Variable=e=2*(∆y)-(∆x) ∴e= 2*(4)-(8) =8-8 =0
• By using general Brenham's algorithm, The result is…
po=0 1 pixel plo(-1,-1)
Pk+1=pk+2∆y-2∆x 0+8-16=-8 2 pixel plot (-1,0)
pk+2=pk+2∆y -8+8=0 3 pixel plot (-2,-1)
Pk+3=pk+2∆y-2∆x 0+8-16=-8 4 pixel plot (-3,-1)
pk+2=pk+2∆y -8+8=0 5 pixel plot (-4,-2)
Pk+3=pk+2∆y-2∆x 0+8-16=-8 6 pixel plot (-5,-2)
pk+2=pk+2∆y -8+8=0 7 pixel plot (-6,-3)
Pk+3=pk+2∆y-2∆x 0+8-16=-8 8 pixel plot (-7,-3)
pk+2=pk+2∆y -8+8=0 9 pixel plot (-8,-4)
• This is required solution for the given line using Bresenham’s
algorithm.
Program step of Bresenham’s line algorithms
Void linebresenham(int xa,ya,int xb,yb)
{
Int dx=abs(xa-xb),dy=abs(ya-yb);
Int p=2*dy-dx;
Int t_dy=2*dy,t_dydx=2*(dy-dx);
Int x,y,xend;
/*determine which point to use as start, which as end*/
If (xa>xb) {
X=xb; y=yb; xend=xa; }
else
{ x=xa; y=ya; xend=xb; } putpixel (x,y,intensity);
While (x<xend) {
X++;
If (p<0)
P+=t_dy;
else {
Y++;
P+=t_dydx;
}
puttpixel (x,y,4);
30
}}
• Circle Generating Algorithms:
Circle frequently used component in picture and graph, a
procedure for generating either full circle or circular arc in
most graphics packages
Properties of circle :
A circle is defined as the set of point that are at a given
distance r from a center point (xc,yc)
(x-xc)2+(y-yc)2=r2
We would use this equation to calculate the position of point
on a circle circumference by stepping along the x-axis in
unit steps from xc-r to xc+r and calculating the
corresponding y values at each position as
Y=yc±sqrt(r2-xc-x)2
But this is not a best method for generating circle
33
• Another way to calculate point along circular points
along circular boundary using polar coordinate r
and θ.Expressing the circle equation in parametric
polar form the pair of equation
x=xc + rcos θ and y =yc + rsin θ
When display is generated with these equation using
a fixed angular step size a circle is plotted with
equally spaced points along the circumference.
The step size chosen for θ depend on the application
and display devices
34
Considering symmetry about x-axis
y
(Y,x)
(-y,x)
(X,y)
(-x,y)
90
x
(x,-y)
(-x,-y)
(-y,-x) (Y-x)
35
• Midpoint circle Algorithms:
To apply midpoint method we define a circle
function:
fcircle(x,y)=x2+y2-r2
Any point (x,y) on the boundary of the circle
with radius r satisfies the equation fcircle(x,y)=0.
If the point is in the interior of the circle, the
circle function is negative &
If the point is outside of the circle, the circle
function is positive
36
• fcircle(x,y) < 0 if (x,y) is inside the circle boundary
= 0 if (x,y) is on the circle boundary
> 0 if (x,y) is outside the circle boundary
Assuming we have just plotted the pixel position at (xk,yk),
we next need to determine whether the pixel at position
(xk+1,yk) or at the position (xk+1,yk-1) is closer to the
circle. Our decision parameter is the circle function
evaluated at the midpoint between these two pixel
Pk= fcircle(xk+1,yk-1/2)
= (xk+1)2+(yk-1/2)2-r2
37
• The initial decision parameter is obtained by
evaluating the circle function at the start position
(x0,y0)=(0,r)
P0=fcircle(1,r- ½)
= 1+(r- ½ )2 –r2
= 1+ (r2-2r.½+¼) - r2
= 5/4 – r
If radius r is specified as an integer,we can simlpy
round p0 to
P0=1-r (for r an integer) since all increment are
integer
38
Midpoint circle algorithms (Bresenhaum circle algo)
1.Input radius (r) and circle center (xc,yc) and obtain
the first point on the circumference of a circle
centered on the origin as (x0,y0)=(0,r)
2.Calculate the initial value of the decision parameter
as p0=5/4 - r.
3 At each xk position, starting at k=0,perform the
following test: if pk<0,the next point along the circle
centered on (0,0) is (xk+1,yk) and
Pk+1=pk+2xk+1+1 -
39
Otherwise, the next point along the circle is
(xk+1,yk-1) and
Pk+1=pk+2xk+1+1- 2yk+1 +
41
Pk+1=pk+2xk+1+1- 2yk+1
=6+2.4+1-2.9 =-3 pixel (5,9)
Pk+1=pk+2xk+1+1
=-3+2.5+1=8 pixel (6,8)
Pk+1=pk+2xk+1+1- 2yk+1
=8 +2.6+1-2.8=5 pixel(7,7)
42
• Void circlemid(int xc, int yc, int radius)
{
Int x=0;
Int y=radius;
Int p=1-radius;
Void circleplotpoints(int, int, int, int);
/*plot first set points*/
Circleplotpoints (xc,yc,x,y);
While (x<y) {
X++;
If (p<0)
P+=2*x+1;
else {
y--;
P+=2*(x-y)+1;
}
cont………..
43
Circleplotpoints(xc, yc, x, y);
}
}
Void circleplotpoints(int xc,int yc,int x,int y)
{
putpixel (xc+x,yc+y, intensity_Color);
putpixel (xc-x, yc+y, intensity_Color);
putpixel (xc+x, yc-y, intensity_Color);
putpixel (xc-x, yc-y, intensity_Color);
putpixel (xc+y, yc+x, intensity_Color);
putpixel (xc-y, yc+x, intensity_Color);
putpixel (xc+y, yc-x, intensity_Color);
putpixel (xc-y, yc-x, intensity_Color);
}
44
• Ellipse Generating Algorithms:
An ellipse is defined as the set of points such
that the sum of the distances from two fixed
positions (foci) is the same for all points. If the
distance to the two foci from any point p=(x,y)
on the ellipse are labeled d1 and d2,then the
general equation of an ellipse can be stated as
d1+d2=constant expressing distances d1 and
d2 in terms of the focal coordinate F1=(x1,y1)
and F2=(x2,y2) we have
sqrt(x-x1)2+(y-y1)2+ sqrt (x-x2)2+(y-y2)2=const
45
• The equation of ellipse can be written in terms
of the ellipse center coordinate and parameters
rx and ry as
(x-xc/rx)2+(y-yc/ry)2=1
Using polar coordinate r and θ,we can also
describe the ellipse in standard position with
the parametric equation:
x=xc+rxcos θ
y=yc+ry sin θ
46
Our approach here is similar to that used in
displaying a raster circle.Given parameters rx,
ry and (xc, yc), we determine points(x,y) for an
ellipse in standard positions centered on the
origin and then we shift the points so the ellipse
is centered at (xc,yc). If we wish also to display
the ellipse in nonstandard position, we could
rotate the ellipse about its center coordinate to
re orient the major and minor axes
General method for transforming object
orientation and position.
47
F ellipse (x,y)=ry2x2+rx2y2-rx2ry2
rx
48
• P1k= F ellipse (xk+1,yk-1/2)
=ry2(xk+1)2+rx2(yk-1/2)2-rx2ry2
If p1k<0 the midpoint is inside the ellipse and the
pixel on scan line or on the ellipse boundary
and we select the pixel on scan line yk-1
At the next sampling position (xk+1+1=xk+2) the
decision parameter for region evaluated as
• P1k+1= F ellipse (xk+1+1,yk+1-1/2)
49
• In region1 the initial value of the decision
parameter is obtained by evaluating the ellipse
function at the start position (x0,y0)=(0,ry):
P10= F ellipse (xk+1,yk-1/2) x0 y ry
P10= F ellipse (1,ry-1/2)
=ry2x2+rx2y2-rx2ry2
=
ry2 + rx2 (ry-1/2)2 - rx2ry2
P10=ry2-r2xry+1/4r2x
50
• over region2 we sample at unit step in negative y
direction, and the midpoint is now taken between
horizontal pixels at each step.For this region,the
decision parameter is evaluated as
P2k= F ellipse (xk+1/2,yk-1)
=ry2(xk+1/2)2+rx2(yk-1)2- rx2ry2
when you enter region two ,the initial position
(x0,y0) is taken as the last position selected as
region as and the initial decision parameter in
region 2 is then
P20= F ellipse (xk+1/2,yk-1)
=ry2(x0+1/2)2+rx2(y0-1)2- rx2ry2
51
• Midpoint ellipse algorithms:
1.Input rx,ry and ellipse center (xc,yc), and obtain the
first point on an ellipse centered on the origon as
(x0,y0)=(0,ry)
2.Calculate the initial value of the decision parameter
in region 1 as
P10=ry2-r2xry+1/4.r2x
3.At each xk position in region at k=0,perform the
following test:If p1k<0,the next point along the
ellipse centered on (0,0) is (xk+1,yk) and
P1k+1=p1k+2ry2xk+1+ry2 _
cont…………..
52
Otherwise, the next point along the circle is (xk+1,yk-1)
and
p1K+1=p1k+2r2yxk+1-2r2xyk+1+r2y +
And continue until 2r2yx≥2r2xy.
54
• Example: Given input ellipse parameter rx (Major Axis)=8 and ry
(Minor Axis)=6,we illustrate the steps in the midpoint ellipse algo
by determining raster position along the ellipse path in the first
quadrant.
=36-64.6+1/4.64=-332 (1,6)
P1k+1=p1k+2ry2xk+1+ry2
=-332+2.36.1+36=-224 (2,6)
=-224+2*36*2+36=-44 (3,6)
=-44+2*36*3+36=+208 (4,5)
cont……..
55
p1K+1=p1k+2r2yxk+1-2r2xyk+1+r2y
=208+2*36*4-2*64*5+36=-108 (5,5)
P1k+1=p1k+2ry2xk+1+ry2
=-108+2*36*5+36=288 (6,4)
=288+2*36*6-2*64*4+36=244 (7,3)
cont………..
56
• For region 2: the initial point (x0,y0)=(7,3)
And the initial decision parameters is
P20=ry2(x0+1/2)2+rx2(y0-1)2- rx2ry2
=36(7+1/2)2+0(3-1)2-0*36
= -151 (8,2)
=233 (8,1)
=745 (8,0)
57
Program :midpoint ellipse
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
Void main()
{
long d1,d2;
Int l,gd,gm,x,y;
long rx,ry,rxsq,rysq,tworxsq,tworysq,dx,dy;
printf(“Enter the x & y coordinate”);
scanf(%ld%ld”,&rx,&ry);
detectgraph(&gd,&gm);
Initgraph(&gd,&gm,c:\\tc\\bgi”);
rxsq=rx*rx;rysq=ry*ry;tworxsq=2*rxsq; tworysq=2*rxsq;
x=0;
y=ry;
d1=rysq-rxsq*ry+(0.25*rxsq);
dx=tworysq*x;
dy=tworxsq*y;
do
{
Putpixel(200+x,200+y,20);
Putpixel(200-x,200-y,20);
Putpixel(200+x,200-y,20);
Putpixel(200+x,200+y,20);
58
If(d1<0)
{
x=x+1;
Y=y;
dx=dx+ tworysq;
d1=d1+dx+rysq;
}
else
{x=x+1;
Y=y-1
dx= dx+ tworysq;
dy= dy- tworxsq;
d1=d1+dx-dy+rysq;
}
delay(100)
}
While (dx<dy);
d2=rysq*(x+0.5)*(x+0.5)+rxsq*(y-1)*(y-1)-rxsq*rysq;
do
{
putpixel(200+x,200+y,20);
putpixel(200-x,200-y,20);
putpixel(200+x,200-y,20);
putpixel(200-x,200+y,20);
59
If(d2>0)
{
x=x; y=y-1;
dy= dy-tworxsq;
dx= dx+tworysq;
d2=d2+dx-dy+rxsq;
}
delay(100);
}
while(y>0);
getch();
closegraph();
}
End of program.
60
1.Boundary fill Algorithms:(Seed fill)
Filling is to start at a point inside a region and paint the interior
outward towards the boundary. If the boundary is specified in
a single color the fill algorithm proceeds outward pixel until
the boundary color is uncounted. This method is called b fill.
*Boundary Fill Algorithms, is particularly useful in interactive
painting package, where interior point are easily selected.
p p
63
• The following procedure illustrate a recursive method for filling a 4
connected area with an intensity specified in parameter fill up to a
boundary color specified with parameter boundary.
• Void boundaryFill4(int x, int y, int fill, int boundary)
{
int current;
Current=get pixel (x,y);
If ((current!=boundary) &&(current!=fill)) {
Set color (fill);
Setpixel(x,y);
Boundaryfill4(x+1,y,fill,boundary)
Boundaryfill4(x-1,y,fill,boundary)
Boundaryfill4(x,y+1,fill,boundary)
Boundaryfill4(x,y-1,fill,boundary)
}
}
Recursive boundary-fill algorithms may not fill regions correctly if some if some
interior pixel are already displayed in the fill color. This occurs because the algo
checks next pixel both for boundary color and for fill color
64
• Flood fill algorithms:(Forest fire Algorithm)
• sometimes you want to fill in (or recolor) an area
that is not defined within a single color boundary.
We can paint such area by replacing a specified
interior color instead of searching for a boundary
color value. This approach is called Flood-fill
algorithms.
• We start from a specified interior point (x,y) and
reassign all pixel values that are currently set to a
given interior color with the desired fill color. If the
area we want to paint has more than one interior
color, we can first reassign pixel values so that all
interior points have the same color. Using either a 4
connected or 8 connected approach.
65
• The following procedure flood fills a 4 connected
region recursively, starting from the input position.
• Void floodfill4(int x, int y, int fillcolor,int old color)
{ if (getpixel(x,y)==old color) {
Setcolor(fillcolor);
Setpixel(x,y);
floodfill4(x+1,y,fillcolor,oldcolor)
floodfill4(x-1,y,fillcolor,oldcolor)
floodfill4(x,y+1,fillcolor,oldcolor)
floodfill4(x,y-1,fillcolor,oldcolor)
}
}
66
Program :Boundary fill
void bfill(int,int,int,int);
void main()
{
Int gd,gm,x,y;
Initgraph(&gd,&gm,”c:\\TURBOC3//BGI”);
printf(“Enter the seed point within 50,50,100,100)\n”);
Scanf(“%d%d” ,&x,&y);
rectangle(50,50,100,100);
bfill(55,55,4,15);
getch();
closegraph();
}
cont………..67
Void bfill(intx,inty,intfill,int bound)
{
Int current;
current=getpixel(x,y);
If ((current!=bound) &&(current!fill))
{
Putpixel(x,y,fill);
delay(1)
bfill(x+1,y,fill,bound);
bfill(x-1,y,fill,bound);
bfill(x,y+1,fill,bound);
bfill(x,y-1,fill,bound);
}} End Bfill algo.
68
Program :Flood fill
Void ffill(int,int,int,int);
Void main()
{
Int gd,gm,x,y;
Initgraph(&gd,&gm,”c:\\”);
printf(“Enter the seed point within 50,50,100,100)\n”);
Scanf(“%d%d”,&x,&y);
rectangle(50,50,100,100);
Flood(55,55,4,15)
cont……….
69
getch()
closegraph();
}
flood(s_x,s_y,fgroud_col,bk_col)
{
If(getpixel(x,y)!=bk_col&&getpixel(x,y)!=fgroud_col)
{
Putpixel(s_x,s_y ,fgroud,bk_col);
Flood(x+1,y,fgroud_col,bbk_col);
Flood(x-1,y,fgroud_col,bbk_col);
Flood(x,y+1,fgroud_col,bbk_col);
Flood(x,y-1,fgroud_col,bbk_col);
}
return(0)
} End
floodfill.
70
• Output of Boundary fill algorithm
POLYGON FILLING
• Polygon is an ordered list of vertices as shown
in figure. For filling polygons with particular
colors, you need to determine the pixels
falling on the border of the polygon and those
which fall inside the polygon.
• In this algorithm, we will see how we can fill
polygons using different techniques.
Scan Line Algorithm
• This algorithm works by intersecting scan line
with polygon edges and fills the polygon
between pairs of intersections. The following
steps depict how this algorithm works.
• Step 1: Find out the Ymin and Ymax from the
given polygon.
• Step 2: ScanLine intersects with each edge of
the polygon from Ymin to Ymax. Name each
intersection point of the polygon. As per the
figure shown above, they are named as p0,
p1, p2, p3.
• Step 3: Sort the intersection point in the
increasing order of X coordinate i.e. (p0, p1),
(p1, p2), and (p2, p3).
• Step 4: Fill all those pair of coordinates that
are inside polygons and ignore the alternate
pairs.
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 is
one methods by which we can identify
whether particular point is inside an object or
outside.
• Odd-Even 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 point; 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 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.
End chapter.
Question ?