Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
11 views

Computer Graphics Unit 2

The document discusses algorithms for computer graphics including line drawing, circle drawing, and polygon filling. It describes the Digital Differential Analyzer (DDA) algorithm for line drawing and the midpoint circle algorithm. It also covers topics like convex and concave polygons, inside-outside testing, and winding number algorithms.

Uploaded by

Tejas
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Computer Graphics Unit 2

The document discusses algorithms for computer graphics including line drawing, circle drawing, and polygon filling. It describes the Digital Differential Analyzer (DDA) algorithm for line drawing and the midpoint circle algorithm. It also covers topics like convex and concave polygons, inside-outside testing, and winding number algorithms.

Uploaded by

Tejas
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

BCA414: Computer Graphics 1

Unit II

Algorithms
Line drawing algorithms
1. DDA Algorithm
2. Bresenham’s Line Algorithm

Circle drawing algorithm


1. Midpoint Circle Algorithm

Polygon fill algorithms:


1. Boundary fill Algorithm
2. Flood fill Algorithm

Line Drawing Algorithm

The Line drawing algorithm is a graphical algorithm which is used to


represent the line segment.A line contains two points. The point is an
important element of a line.

Equation of the straight line We can define a straight line with the help
of the following equation.
y= mx + a
Where, (x, y) = axis of the line.
m = Slope of the line.
a = Interception point

TS
BCA414: Computer Graphics 2

Unit II
DDA Algorithm

The Digital Differential Analyzer helps us to interpolate the


variables on an interval from one point to another point. We can use
the digital Differential Analyzer algorithm to perform rasterization
on polygons, lines, and triangles.

Digital Differential Analyzer algorithm is also known as an


incremental method of scan conversion. In this algorithm, we can
perform the calculation in a step by step manner.

We use the previous step result in the next step. As we know the
general equation of the straight line is: y = mx + c

Here, m is the slope of (x1, y1) and (x2, y2). m = (y2 – y1)/ (x2 –
x1)

Now, we consider one point (xk, yk) and (xk+1, yk+1) as the next
point. Then the slope m = (yk+1 – yk)/ (xk+1 – xk) Now, we have to
find the slope between the starting point and ending point. There can
be following three cases to discuss

Case 1:
If m < 1 Then x coordinate tends to the Unit interval. xk+1 = xk + 1
yk+1 = yk + m

Case 2:
If m > 1 Then y coordinate tends to the Unit interval. yk+1 = yk + 1
xk+1 = xk + 1/m

Case 3:
If m = 1 Then x and y coordinate tend to the Unit interval. xk+1 =
xk + 1 yk+1 = yk + 1

We can calculate all intermediate points with the help of above three
discussed cases.

Algorithm of Digital Differential Analyzer (DDA) Line Drawing

Step 1: Start.

TS
BCA414: Computer Graphics 3

Unit II
Step 2: We consider Starting point as (x1, y1), and endingpoint (x2,
y2).
Step 3: Now, we have to calculate ▲x and ▲y. ▲x = x2-x1 ▲y = y2-
y1 m = ▲y/▲x
Step 4: Now, we calculate three cases. If m < 1 Then x change in Unit
Interval y moves with deviation (xk+1, yk+1) = (xk+1, yk+1) If m > 1
Then x moves with deviation y change in Unit Interval (xk+1, yk+1) =
(xk+1/m, yk+1/m) If m = 1 Then x moves in Unit Interval y moves in
Unit Interval (xk+1, yk+1) = (xk+1, yk+1)
Step 5: We will repeat step 4 until we find the ending point of the
line.
Step 6: Stop

TS
BCA414: Computer Graphics 4

Unit II

TS
BCA414: Computer Graphics 5

Unit II

TS
BCA414: Computer Graphics 6

Unit II

TS
BCA414: Computer Graphics 7

Unit II

TS
BCA414: Computer Graphics 8

Unit II

TS
BCA414: Computer Graphics 9

Unit II

Circle generating algorithm - Mid Point


Circle Algorithm
Drawing a circle on the screen is a little complex than drawing a
line.

In the mid-point circle algorithm we use eight-way symmetry so only


ever calculate the points for the top right eighth of a circle, and
then use symmetry to get the rest of the points.

TS
BCA414: Computer Graphics 10

Unit II

Assume that we have just plotted point (xk, yk) The next point is a
choice between (xk+1, yk) and (xk+1, yk-1)

TS
BCA414: Computer Graphics 11

Unit II

TS
BCA414: Computer Graphics 12

Unit II

TS
BCA414: Computer Graphics 13

Unit II

Polygon
A polygon is a two-dimensional shape with straight sides. It is a closed figure formed by connecting
line segments, where each line segment intersects exactly two other line segments at its endpoints.
Polygons are classified based on the number of sides they have. Some common polygons include
triangles (3 sides), quadrilaterals (4 sides), pentagons (5 sides), hexagons (6 sides), heptagons (7 sides),
octagons (8 sides), and so on. Polygons play a significant role in geometry and are studied extensively
in mathematics. They have various properties and formulas related to their angles, side lengths, area,
and perimeter.

Types of Polygon:

1. Convex Polygon:
- In a convex polygon, any line segment drawn between two points inside the polygon lies entirely
within the polygon.
- All interior angles of a convex polygon are less than 180 degrees.
- The sides do not intersect each other, and the polygon does not have any "dents" or inward
curvatures.
- Examples of convex polygons include triangles, rectangles, regular pentagons, and regular
hexagons.
- Imagine a regular triangle. All its interior angles are less than 180 degrees, and any line segment
drawn between two points inside the triangle lies completely within the triangle. Thus, it is a convex
polygon.

2. Concave Polygon:
- In a concave polygon, at least one line segment drawn between two points inside the polygon lies
partially or entirely outside the polygon.
- At least one interior angle of a concave polygon is greater than 180 degrees.
- Concave polygons have at least one "dent" or inward curvature.
- Examples of concave polygons include a crescent shape or irregular shapes with at least one
"notch."
- Consider a shape that resembles a letter 'C'. If you draw a line segment between two points inside
the 'C', there will be instances where the line segment extends outside the shape. This demonstrates
that it's concave because it has at least one interior angle greater than 180 degrees and at least one
"dent" or inward curvature.

In summary, convex polygons have no inward curvatures and all interior angles are less than 180
degrees, while concave polygons have at least one inward curvature and at least one interior angle
greater than 180 degrees.

TS
BCA414: Computer Graphics 14

Unit II
Inside-Outside test

ODD - EVEN RULE:

Inside-outside tests are methods used to determine whether a point lies inside or
outside a polygon. Constructing a line segment between the point (P) to be examined
and a known point outside the polygon is the one way to determine a point lies inside
a polygon or not. The number of times the line segment intersects the polygon
boundary is then counted. The point (P) is an internal point if the number of polygon
edges intersected by this line is odd; otherwise, the point is an external point.

In the figure, the line segment from ‘A’ crosses single edge & hence point A is inside
the polygon. The point B is also inside the polygon as the line segment from B crosses
three (odd) edges. But point C is outside the polygon it as the line segment from C
crosses two (even) edges.

But this even-odd test fails when the intersection point is a vertex.
To handle this case, we have to make few modifications.
We must look at the other end points of the two segments of a polygon which meet at
this vertex. If these points lies on the same side of the constructed line A’P’, then the
intersection point counts as an even number of intersection. But if they lie on the

TS
BCA414: Computer Graphics 15

Unit II
opposite side of constructed line AP, then the intersection points counts as a single
intersection.

Winding Number Algorithm:

1. This algorithm considers the winding number around the point.


2. It counts how many times the polygon winds around the point as it's traversed in a
particular direction (usually counterclockwise).
3. If the winding number is nonzero, the point is inside the polygon. If it's zero, the
point is outside.

Polygon

As we can see the line segment A’P’ intersects at M which is a vertex and L & Z are
the other end points of the two segments meeting at M. L & Z lie on same side of the
line segment A’P’, so the count is taken as even.

Winding Number Method :


There is another alternative method for defining a polygons’ interior point is called
the winding number method. Conceptually, one can stretch a piece of elastic between
the point (P) to be checked and the point on the polygon boundary.
Treat that, elastic is tied to point (P) to be checked firmly and the other end of the
elastic is sliding along the boundary of the polygon until it has made one complete
circuit. Then we check that how many times the elastic has been wound around the

TS
BCA414: Computer Graphics 16

Unit II
point of intersection. If it is wound at-least once, point is inside. If there is no net
winding then point is outside.

In this method, instead of just counting the intersections, we give each boundary line
crossed a direction number, and we sum these directions numbers. The direction
number indicates the direction of the polygon edge was drawn relative to the line
segment we constructed for the test.

Example : To test a point (xi, yi), let us consider a horizontal line segment y = yi
which runs from outside the polygon to (xi, yi). We find all the sides which crossed
this line segment.
Now there are 2 ways for side to cross, the side could be drawn starting below end,
cross it and end above the line. In this case we can give direction numbers – 1, to the
side or the edge could start above the line & finish below it in this case, given a
direction 1. The sum of the direction numbers for the sides that cross the constructed
horizontal line segment yield the “Winding Number” for the point.

If the winding number is non-zero , the point is interior to polygon, else, exterior to
polygon.

Polygon

In the above figure, the line segment crosses 4 edges having different direction
numbers : 1, -1, 1& -1 respectively, then :
Winding Number = 1 + (-1) + 1 + (-1) = 0
So the point P is outside the Polygon. The edge has direction Number -1 because it
starts below the line segment & finishes above. Similarly, edge has direction Number
+1 because it starts from above the line segment & finishes below the line segment
(See the directions in the figure).

Polygon fill algorithm

Boundary-Fill Algorithm

The boundary fill algorithm works as its name. This algorithm picks a point inside an
object and starts to fill until it hits the boundary of the object. The color of the
boundary and the color that we fill should be different for this algorithm to work.

TS
BCA414: Computer Graphics 17

Unit II
In this algorithm, we assume that color of the boundary is same for the entire object.
The boundary fill algorithm can be implemented by 4-connected pixels or 8-
connected pixels.

TS
BCA414: Computer Graphics 18

Unit II
Sometimes we come across an object where we want to fill the area and its boundary
with different colors. We can paint such objects with a specified interior color instead
of searching for particular boundary color as in boundary filling algorithm.

Instead of relying on the boundary of the object, it relies on the fill color. In other
words, it replaces the interior color of the object with the fill color. When no more
pixels of the original interior color exist, the algorithm is completed.

Once again, this algorithm relies on the Four-connect or Eight-connect method of


filling in the pixels. But instead of looking for the boundary color, it is looking for all
adjacent pixels that are a part of the interior.

4-Connected Pixels
In this technique 4-connected pixels are used as shown in the figure. We are putting
the pixels above, below, to the right, and to the left side of the current pixels and this
process will continue until we find a boundary with different color.

Algorithm

Step 1 − Initialize the value of point x,y

, fcolor and dcol.

Step 2 − Define the boundary values of the polygon.

Step 3 − Check if the current point is of default color, then repeat the steps 4 and 5
till the boundary pixels reached.

If getpixel(x, y) = dcol then repeat step 4 and 5

Step 4 − Change the default color with the fill color at the point.

setPixel(x, y, fcol)

TS
BCA414: Computer Graphics 19

Unit II
Step 5 − Recursively follow the procedure with four neighborhood points.

FloodFill (x – 1, y, fcol, dcol)


FloodFill (x + 1, y, fcol, dcol)
FloodFill (x, y - 1, fcol, dcol)
FloodFill (x – 1, y + 1, fcol, dcol)

Step 6 − Exit

There is a problem with this technique. Consider the case as shown below where we
tried to fill the entire region. Here, the image is filled only partially. In such cases, 4-
connected pixels technique cannot be used.

8-Connected Pixels
In this technique 8-connected pixels are used as shown in the figure. We are putting
pixels above, below, right and left side of the current pixels as we were doing in 4-
connected technique.

In addition to this, we are also putting pixels in diagonals so that entire area of the
current pixel is covered. This process will continue until we find a boundary with
different color.

TS
BCA414: Computer Graphics 20

Unit II

Algorithm

Step 1 − Initialize the value of point x,y

, fcolor and dcol.

Step 2 − Define the boundary values of the polygon.

Step 3 − Check if the current point is of default color then repeat the steps 4 and 5 till
the boundary pixels reached

If getpixel(x,y) = dcol then repeat step 4 and 5

Step 4 − Change the default color with the fill color at the point.

setPixel(x, y, fcol)

Step 5 − Recursively follow the procedure with four neighbourhood points

FloodFill (x – 1, y, fcol, dcol)


FloodFill (x + 1, y, fcol, dcol)
FloodFill (x, y - 1, fcol, dcol)
FloodFill (x, y + 1, fcol, dcol)
FloodFill (x – 1, y + 1, fcol, dcol)
FloodFill (x + 1, y + 1, fcol, dcol)
FloodFill (x + 1, y - 1, fcol, dcol)
FloodFill (x – 1, y - 1, fcol, dcol)

TS
BCA414: Computer Graphics 21

Unit II
Step 6 − Exit

The 4-connected pixel technique failed to fill the area as marked in the following
figure which won’t happen with the 8-connected technique.

Inside-outside Test

TS

You might also like