CG Unit 2
CG Unit 2
dimensional shape.
DDA Algorithm:
Step1: Start 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))
Example: If a line is drawn from (2, 3) to (6, 15) with use of DDA. How many points will
needed to generate such line?
x1=2
y1=3
x2= 6
y2=15
dx = 6 - 2 = 4
dy = 15 - 3 = 12
m=
Advantage:
1. It is a faster method than method of using direct use of line equation.
2. This method does not use multiplication theorem.
3. It allows us to detect the change in the value of x and y ,so plotting of same point
twice is not possible.
4. This method gives overflow indication when a point is repositioned.
5. It is an easy method because each step involves just two additions.
Disadvantage:
1. It involves floating point additions rounding off is done. Accumulations of round off
error cause accumulation of error.
2. Rounding off operations and floating point operations consumes a lot of time.
3. It is more suitable for generating line using the software. But it is less suited for
hardware implementation.
Step5: Consider (x, y) as starting point and xendas maximum possible value of x.
If dx < 0
Then x = x2
y = y2
xend=x1
If dx > 0
Then x = x1
y = y1
xend=x2
Step9: Increment x = x + 1
Step11: Go to step 7
Example: Starting and Ending position of the line are (1, 1) and (8, 5). Find
intermediate points.
Solution: x1=1
y1=1
x2=8
y2=5
dx= x2-x1=8-1=7
dy=y2-y1=5-1=4
I1=2* ∆y=2*4=8
I2=2*(∆y-∆x)=2*(4-7)=-6
d = I1-∆x=8-7=1
x y d=d+I1 or I2
1 1 d+I2=1+(-6)=-5
2 2 d+I1=-5+8=3
3 2 d+I2=3+(-6)=-3
4 3 d+I1=-3+8=5
5 3 d+I2=5+(-6)=-1
6 4 d+I1=-1+8=7
7 4 d+I2=7+(-6)=1
8 5
Output:
5.DDA Algorithm can draw circle and 5. Bresenham's Line Algorithm can
curves but are not accurate as draw circle and curves with more
Bresenham's Line Algorithm accurate than DDA Algorithm.
Defining a Circle:
Circle is an eight-way symmetric figure. The shape of circle is the same in all quadrants.
In each quadrant, there are two octants. If the calculation of the point of one octant is
done, then the other seven points can be calculated easily by using the concept of
eight-way symmetry.
For drawing, circle considers it at the origin. If a point is P 1(x, y), then the other seven
points will be
So we will calculate only 45°arc. From which the whole circle can be determined easily.
If we want to display circle on screen then the putpixel function is used for eight points
as shown below:
Example: Let we determine a point (2, 7) of the circle then other points will be (2, -7),
(-2, -7), (-2, 7), (7, 2), (-7, 2), (-7, -2), (7, -2)
These seven points are calculated by using the property of reflection. The reflection is
accomplished in the following way:
There are two standards methods of mathematically defining a circle centered at the
origin.
In this article, we will discuss about Mid Point Circle Drawing Algorithm.
The points for other octants are generated using the eight symmetry property.
Procedure-
Given-
Centre point of Circle = (X0, Y0)
Radius of Circle = R
The points generation using Mid Point Circle Drawing Algorithm involves the following steps-
Step-01:
Assign the starting point coordinates (X0, Y0) as-
X0 = 0
Y0 = R
Step-02:
Calculate the value of initial decision parameter P0 as-
P0 = 1 – R
Step-03:
Suppose the current point is (Xk, Yk) and the next point is (Xk+1, Yk+1).
Find the next point of the first octant depending on the value of decision parameter Pk.
Follow the below two cases-
Step-04:
If the given centre point (X0, Y0) is not (0, 0), then do the following and plot the point-
Xplot = Xc + X0
Yplot = Yc + Y0
Step-05:
Keep repeating Step-03 and Step-04 until Xplot >= Yplot.
Step-06:
Step-05 generates all the points for one octant.
To find the points for other seven octants, follow the eight symmetry property of circle.
This is depicted by the following figure-
Problem-01:
Given the centre point coordinates (0, 0) and radius as 10, generate all the points to form a circle.
Solution-
Given-
Centre Coordinates of Circle (X0, Y0) = (0, 0)
Radius of Circle = 10
Step-01:
Assign the starting point coordinates (X0, Y0) as-
X0 = 0
Y0 = R = 10
Step-02:
Calculate the value of initial decision parameter P0 as-
P0 = 1 – R
P0 = 1 – 10
P0 = -9
Step-03:
As Pinitial < 0, so case-01 is satisfied.
Thus,
Xk+1 = Xk + 1 = 0 + 1 = 1
Yk+1 = Yk = 10
Pk+1 = Pk + 2 x Xk+1 + 1 = -9 + (2 x 1) + 1 = -6
Step-04:
This step is not applicable here as the given centre point coordinates is (0, 0).
Step-05:
Step-03 is executed similarly until Xk+1 >= Yk+1 as follows-
(0, 10)
-9 -6 (1, 10)
-6 -1 (2, 10)
-1 6 (3, 10)
6 -3 (4, 9)
-3 8 (5, 9)
8 5 (6, 8)
Algorithm Terminates
printf("ENTER r");
scanf("%d",&r);
printf("%d",r);
p= -r;
printf("%d",p);
initgraph (&gdriver, &gmode, NULL);
x=0;
y=r;
p=(1-r);
printf("%d",p);
while (x<=y)
{
if (p<0)
{
p= p+(2*x)+1;printf("%d",p);
}
else
{
p+=(2*(x-y))+1;
y--;
}
x++;
putpixel(x+xc,y+yc,RED);
putpixel(x+xc,-y+yc,YELLOW);
putpixel(-x+xc,-y+yc,GREEN);
putpixel(-x+xc,y+yc,YELLOW);
putpixel(y+xc,x+yc,12);
putpixel(y+xc,-x+yc,14);
putpixel(-y+xc,-x+yc,15);
putpixel(-y+xc,x+yc,6);
}
getch();
return 0;
}
Output:
Step4: Calculate d = 3 - 2r
Step7: Plot eight points by using concepts of eight-way symmetry. The center is at (p,
q). Current active pixel is (x, y).
putpixel (x+p, y+q)
putpixel (y+p, x+q)
putpixel (-y+p, x+q)
putpixel (-x+p, y+q)
putpixel (-x+p, -y+q)
putpixel (-y+p, -x+q)
putpixel (y+p, -x+q)
putpixel (x+p, -y-q)
Step9: Go to step 6
Example: Plot 6 points of circle using Bresenham Algorithm. When radius of circle is 10
units. The circle has centre (50, 50).
So P1 (0,0)⟹(50,50)
P2 (1,10)⟹(51,60)
P3 (2,10)⟹(52,60)
P4 (3,9)⟹(53,59)
P5 (4,9)⟹(54,59)
P6 (5,8)⟹(55,58)
while(x<=y)
{
if(d<0)
{
d=d+(4*x)+6;
}
else
{
d=d+(4*x)-(4*y)+10;
y=y-1;
}
x=x+1;
EightWaySymmetricPlot(xc,yc,x,y);
}
}
int main()
{
i. Using filling algorithms such as Floodfill algorithm, Boundary fill algorithm and scanline polygon fill
algorithm, we can color the objects.
ii. Using inbuilt graphics functions such as floodfill(),setfillstyle() we can fill the object with color’s directly
without using any filling algorithm.
Types of filling
• Solid-fill
• Pattern-fill
The polygon can be displayed by drawing a line between (x1, y1), and (x2, y2), then a line between (x2, y2),
and (x3, y3), and so on until the end vertex. In order to close up the polygon, a line between (x n, yn), and
(x1, y1) must be drawn.
One problem with this representation is that if we wish to translate the polygon, it is necessary to apply
the translation transformation to each vertex in order to obtain the translated polygon.
Inside-Outside Tests
When filling polygons we should decide whether a particular point is interior or
exterior to a polygon.
A rule called the odd-parity (or the odd-even rule) is applied to test whether a
point is interior or not.
To apply this rule, we conceptually draw a line starting from the particular point
and extending to a distance point outside the coordinate extends of the object in
any direction such that no polygon vertex intersects with the line.
Inside-Outside Tests
They are:
Algorithm:
The following steps illustrate the idea of the recursive boundary-fill algorithm:
2. If the current pixel is not already filled and if it is not an edge point, then set the pixel
with the fill color, and store its neighboring pixels (4 or 8-connected). Store only
neighboring pixel that is not already filled and is not an edge point.
3. Select the next pixel from the stack, and continue with step 2.
{ delay(10);
putpixel(x, y, fcolor);
Advantages:
Disadvantages:
Sometimes we want to fill in (recolor) an area that is not defined within a single color
boundary. We paint such areas by replacing a specified interior color instead of searching
for a boundary color value. This approach is called a flood-fill algorithm.
1. We start from a specified interior pixel (x, y) and reassign all pixel values that are
currently set to a given interior color with the desired fill color.
2. If the area has more than one interior color, we can first reassign pixel values so
that all interior pixels have the same color.
2. First all the pixels should be reassigned to common color. here common color is
black.
3. Start with a point inside given object, check the following condition:
if(getpixel(x,y)==old_col)---old_col is common color
4. If above condition is satisfied, then following 4 steps are followed in filling the
object.
2. First all the pixels should be reassigned to common color. here common color is
black.
3. Start with a point inside given object, check the following condition:
if(getpixel(x,y)==old_col)---old_col is common color
4. If above condition is satisfied, then following 8 steps are followed in filling the
object.
//Recursive 4-way floodfill.
Advantages:
Disadvantages:
⚫ Find the intersections of the scan line with all edges of the polygon
⚫ Fill in all pixels between pairs of intersections that lie interior to the
polygon
• the horizontal scanning of the polygon from its lowermost to its topmost vertex,
• and finally drawing the interior horizontal lines with the specified fill color.
process.
Dealing with vertices:
• When the endpoint y coordinates of the two edges are increasing, the y value of
the upper endpoint for the current edge is decreased by one (a)
• When the endpoint y values are decreasing, the y value of the next edge is
decreased by one (b)
xk+1 = xk + 1/m
Steps in algorithm:
Algorithm Steps:
1. the horizontal scanning of the polygon from its lowermost to its topmost vertex
a. Each entry in the table for a particular scan line contains the maximum y
value for that edge, the x-intercept value (at the lower vertex) for the edge,
and the inverse slope of the edge.
4. Determine whether any edges need to be splitted or not. If there is need to split,
split the edges.
Advantages:
Scan fill algorithm fills the polygon in the same order as rendering.
Disadvantages: