2.raster Scan Algo

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 83

2.

COMPUTER GRAPHICS
Output primitives
TEXT BOOK :
 COMPUTER GRAPHICS

DONALD HEARN & M.PAULINE BAKER


Raster scan Algorithms

LINE GENERATION ALGORITHM


1.DDA SCAN LINE
ALGORITHMS
( Digital Differential Analyzer)
Basic Math Review
Cartesian Coordinate System
Slope-Intercept Formula For A Line
Given a third point on the line: 6
P = (x,y)
P = (x,y) 5

Slope = (y - y1)/(x - x1) 4 P2 = (x2,y2)


= (y2 - y1)/(x2 - x1) 3
Solving For y
2
y = [(y2-y1)/(x2-x1)]x
1
+ [-(y2-y1)/(x2-x1)]x1 + y1 P1 = (x1,y1)

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. The unit steps are always along
the coordinate of greatest change, e.g. if dx
= 10 and dy = 5, then we would take unit
steps along x and compute the steps along
y.
A line connects two points. It is a basic element in
graphics.
To draw a line, you need two points between
which you can draw a line.

In DDA algorithms, we refer the one point of line as X0,


Y0 and the second point of line as X1, Y1.
DDA Algorithm
Digital Differential Analyzer (DDA)
algorithm is the simple line generation
algorithm which is explained step by
step here.
Step 1: Get the input of two end points
(X0, Y0) and (X1, Y1).
Step 2: Calculate the difference between two end
points.
dx = X1 - X0
dy = Y1 - Y0
Step 3: Based on the calculated difference in step-
2, you need to identify the number
of steps to put pixel. If dx > dy, then you need more
steps in x coordinate; otherwise
in y coordinate.
if (dx > dy)
Steps = absolute(dx);
else
Steps = absolute(dy);

Step 4: Calculate the increment in x coordinate


and y coordinate.
Xincrement = dx / (float) steps;
Yincrement = dy / (float) steps;
Step 5: Put the pixel by successfully incrementing x
and y coordinates accordingly and complete the
drawing of the line.

for(int v=0; v < Steps; v++)


{
x = x + Xincrement;
y = y + Increment;
put pixel(x,y,intensity);
}
EXAMPLE:1
Consider the line from (0,0) to (4,6). Use the simple DDA algorithm
to rasterize this line.

Sol. Evaluating steps 1 to 5 in the DDA algorithm we have


 
Xl = 0                          Y1 = 0
X2 = 4                          Y2 = 6
Length = |Y2-Y1l = 6
                             ∆X = |X2- Xl | / Length
                                 =       4
                                           6
                             ∆Y = |Y2- Yl | / Length
                                   = 6/6=1
Initial value for
                               X = 0 + 0.5 * Sign ( 4 ) = 0.5
                                                                 6
                               Y = 0 + 0.5 * Sign (1) = 0.5
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.

Sol. Evaluating steps 1 to 5 in the DDA algorithm we have


 
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,


// C program for DDA line generation
#include<stdio.h>
#include<graphics.h>
 
//Function for finding absolute value
int abs (int n)
{
    return ( (n>0) ? n : ( n * (-1)));
}
 
//DDA Function for line generation
void DDA(int X0, int Y0, int X1, int Y1)
{
    // calculate dx & dy
    int dx = X1 - X0;
    int dy = Y1 - Y0;
 
    // calculate steps required for generating pixels
    int steps = abs(dx) > abs(dy) ? abs(dx) : abs(dy);
 
    // calculate increment in x & y for each steps
    float Xinc = dx / (float) steps;
    float Yinc = dy / (float) steps;
 // Put pixel for each step
    float X = X0;
    float Y = Y0;
    for (int i = 0; i <= steps; i++)
    {
        putpixel (X,Y,RED);  // put pixel at (X,Y)
        X += Xinc;           // increment in x at each step
        Y += Yinc;           // increment in y at each step
        delay(100);          // for visualization of line-
                             // generation step by step
    }
}
/ Driver program
int main()
{
    int gd = DETECT, gm;
 
    // Initialize graphics function
    initgraph (&gd, &gm, “ ");  
 
    int X0 = 2, Y0 = 2, X1 = 14, Y1 = 16;
    DDA(2, 2, 14, 16);
    return 0;
}
Output
Advantages of DDA Algorithm

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 v direction to find the pixel positions
along the line path.

Disadvantages of DDA Algorithm

1.Floating point arithmetic in DDA algorithm is still time-


consuming.
2. The algorithm is orientation dependent. Hence end point accuracy is
poor.
Bresenham’s Line Generation
The Bresenham algorithm is another incremental
scan conversion algorithm. The big advantage of
this algorithm is that, it uses only integer
calculations.

Moving across the x axis in unit intervals and at


step choose between two different y coordinates.
For example, as shown in the following
illustration, from position (2, 3) you need to
choose between
(3, 3) and (3, 4). You would like the point that is
closer to the original line.
At sample position xk+1, the vertical separations
from the mathematical line are
labeled as dupper and dlower.

From the above illustration, the y coordinate on the


mathematical line at xk+1 is: 𝑌 = 𝑚(𝑋𝑘 + 1) + 𝑏
So, dupper and dlower are given as follows:
𝑑𝑙𝑜𝑤𝑒𝑟 = 𝑦 − 𝑦𝑘
= 𝑚(𝑋𝑘 + 1) + 𝑏 − 𝑌𝑘
and
𝑑𝑢𝑝𝑝𝑒𝑟 = (𝑦𝑘 + 1) − 𝑦
= 𝑌𝑘 + 1 − 𝑚(𝑋𝑘 + 1) − 𝑏

You can use these to make a simple decision about


which pixel is closer to the mathematical line. This
simple decision is based on the difference between
the two pixel positions.

𝑑𝑙𝑜𝑤𝑒𝑟 − 𝑑𝑢𝑝𝑝𝑒𝑟 = 2𝑚(𝑥𝑘 + 1) − 2𝑦𝑘 + 2𝑏 − 1


Let us substitute m with dy/dx where dx and dy are the
differences between the endpoints.
𝑑𝑥(𝑑𝑙𝑜𝑤𝑒𝑟 − 𝑑𝑢𝑝𝑝𝑒𝑟 ) = 𝑑𝑥(2𝑑𝑦/𝑑𝑥(𝑥𝑘 + 1) − 2𝑦𝑘 + 2𝑏 − 1)
= 2𝑑𝑦 ∙ 𝑥𝑘 − 2𝑑𝑥 ∙ 𝑦𝑘 + 2𝑑𝑦 + 𝑑𝑥(2𝑏 − 1)
= 2𝑑𝑦 ∙ 𝑥𝑘 − 2𝑑𝑥 ∙ 𝑦𝑘 + 𝐶
So, a decision parameter pk for the k th step along a line is
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 (negative value)
Otherwise, the next point to plot is (xk+1,yk+1) &
Pk+1=pk+2∆y - 2∆x (positive value)
5.Display the point put pixel(x,y,Intensity)

6.Repet step 4 ∆x times

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 (e)
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 Bresenham’s algorithm, The result is…
po=0 1 pixel plot (0,0)
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;
}
settpixel (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)
45
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 -(Negative)

39
Otherwise, the next point along the circle is
(xk+1,yk-1) and
Pk+1=pk+2xk+1+1- 2yk+1 -(Positive)

4.Determine symmetry point in the other seven


octant.
5.Move each calculated pixel position(x,y) onto the
circular path centered on (xc,yc) and plot the
coordinate values:
X=x+ xc , y=y+ yc
6.Repet steps 3 through 5 until x≥y.
40
• Example : given a circle radius r=10,we demonstrate
the midpoint circle algorithms by determining position
along the circle octant in the first quadrant from x=10
to x=y. The initial value of the decision parameter.
P0=1-r = 1-10=-9  pixel(1,10)
For the circle centered on the coordinate origin, the initial
point is (x0,y0)=(0,10)
Pk+1=pk+2xk+1+1
=-9+2.1+1=-6  pixel(2,10)
=-6+2.2+1=-1  pixel(3,10)
=-1+2.3+1=6  pixel(4,9)
cont…..

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)

A plot is generated pixel position in the first


quadrant

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);
putpixel (xc-x, yc+y, intensity);
putpixel (xc+x, yc-y, intensity);
putpixel (xc-x, yc-y, intensity);
putpixel (xc+y, yc+x, intensity);
putpixel (xc-y, yc+x, intensity);
putpixel (xc+y, yc-x, intensity);
putpixel (xc-y, yc-x, intensity);
}
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

F ellipse (x,y) < 0 if (x,y) is inside the ellipse boundary


= 0 if (x,y) is on the ellipse boundary
> 0 if (x,y) is outside the ellipse boundary
Slope =-1
ry Reg
1
Reg-2

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) x0 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.

4.Calculate the initial value of the decision parameter


in region 2 using the last point (x0,y0) calculated in
region 1 as
P20=ry2(x0+1/2)2+rx2(y0-1)2- rx2 . ry2

5.At each yk position in region 2,starting at k=0


perform the following test:If p2k>0 the next point
along the ellipse centered on (0,0) is (xk,yk-1) and
p2k+1=p2k-2r2xyk+1+r2x
cont…………….
53
• Otherwise, the next point along the ellipse is
(xk+1,yk-1) and
P2k+1=p2k+2r2yxk+1-2r2xyk+1+r2x
Using the same increment calculation for x and y
as region 1.
6.Determine symmetry points in the other three
quadrant
7.Move each calculated pixel position(x,y) onto
the ellipical path centered on(xc,yc) and plot
coordinate values x=x+xc,y=y+yc
8.Repeat the steps for region 1 until 2r2yx≥2r2xy

54
• Example: Given input ellipse parameter rx=8 and ry=6,we
illustrate the steps in the midpoint ellipse algo by determining
raster position along the ellipse path in the first quadrant.

• For region 1: the initial point on the origin is (x0,y0)=(0,6)


And initial decision parameter value is
P10=ry2-r2xry+1/4r2 x

=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)

Until 2r2yxk+1≥ 2r2xyk+1


2*36*7≥2*64*3 = 504 ≥384

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:
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.

*A boundary fill procedure accepts as input the coordinate of an


interior point (x,y),a fill color and a boundary color. Starting
from(x,y),the procedure tests neighboring positions to
determine whether they are of boundary color. If not they are
painted with fill color and their neighbors are tested. This
process continuous until all pixel up to the boundary color the
area have been tested.
4 connected method 8 connected method

p p

Fig shows two methods for proceeding to neighboring pixel


from the current test position. Four neighboring points are
tested. These are the pixel position that are
right,left,above,and below the current pixel. Areas filled by
this methods are called 4 connected method. The 2
method shown in fig is used to fill more complex area. Here
the set of neighboring position to be tested includes the
four diagonal pixel. Fill methods using this approch are
called 8 connected method.

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)
• 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:\\”);
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 ?

You might also like