CH 4
CH 4
If |dx| is greater than |dy|, then |dx| will be step and otherwise |dy| will be step.
if |dx|>|dy| then
step = |dx|
else
step = |dy|
Prepared by Desta L. Computer graphics
Now it is very simple to say that step is the total number of pixel
Next a loop is required that will run step times. In the loop drawPixel
and addPrepared
xIncrement
by Desta L.
in x1 and yIncrement in y1.
DDA_Line (Point p1, Point p2)
dx = p2.x – p1. x yIncrement = dy/step
dy = p2.y – p1. y for counter = 1 to step
x1=p1.x drawPixel (x1, y1)
y1=p1.y x1 = x1 + xIncrement
if |dx|>|dy| then y1 = y1 + yIncrement
step = |dx|
else
step = |dy|
xIncrement = dx/step
Advantage
Does not calculate coordinates based on the complete equation (uses
offset method)
Disadvantage
Round-off errors are accumulated, thus line diverges more and
more from straight line
Round-off operations take time
Perform integer arithmetic by storing float as integers in numerator and
denominator and performing integer artithmetic.
Coordinate changes along the line occur in unit steps in either the x or y directions.
Prepared by Desta L.
Let us define a decision function p, to determine which distance is closer to
the true point. By taking the difference between the distances, the decision
function will be positive if d1 is larger, and negative otherwise.
A positive scaling factor is added to ensure that no division is necessary
and only integer math need be used.
pi = dx (d1-d2)
pi = dx (2m * (xi+1) + 2b – 2yi-1 )
pi = 2 dy (xi+1) –2 dx yi + dx (2b-1 ) ------------------ (i)
pi = 2 dy xi – 2 dx yi + k ------------------ (ii)
where k=2 dy + dx (2b-1)
Prepared by Desta L.
Then we can calculate pi+1 in terms of pi without any xi , yi or k .
pi+1 = 2 dy xi+1 – 2 dx yi+1 + k
pi+1 = 2 dy (xi + 1) - 2 dx yi+1 + k since xi+1= xi + 1
pi+1 = 2 dy xi + 2 dy- 2 dx yi+1 + k ------------- (iii)
Now subtracting (ii) from (iii), we get
pi+1 - pi = 2 dy - 2 dx (yi+1 - yi )
pi+1 = pi + 2 dy - 2 dx (yi+1 - yi )
If the next point is: (xi+1,yi) then
d1<d2 => d1-d2<0
=> pi<0
=> pi+1= pi + 2 dy
If the next point is: (xi+1,yi+1) then
d1>d2 => d1-d2>0
=> pi>0
=> pi+1= pi + 2 dy - 2 dx
Prepared by Desta L. Computer graphics
The pi is our decision variable, and calculated using integer arithmetic from
pre-computed constants and its previous value.
Now a question is remaining how to calculate initial value of pi. For that use
equation (i) and put values (x1, y1)
pi = 2 dy (x1+1) – 2 dx yi + dx (2b-1 )
where b = y – m x implies that
pi = 2dyx1+2dy–2dxyi+dx(2(y1–mx1)-1)
pi = 2dyx1+2dy–2dxyi+2dxy1–2dyx1-dx
pi = 2dyx1+2dy–2dxyi+2dxy1–2dyx1-dx
There are certain figures that will cancel each other shown in same different
colour
pi = 2 dy - dx
Prepared by Desta L.
Thus, Bresenham's line drawing algorithm is as follows:
Again, this algorithm can be easily generalized to other arrangements of the end points
of the line segment, and for different ranges of the slope of the line.
Ex: draw a line using bresenham’s line drawing algorithm b/n P1(5,3) and P2(10,6).
Fixed point representation allows a method for performing calculations using only
integer arithmetic, but still obtaining the accuracy of floating-point values.
In fixed point, the fraction part of a value is stored separately, in another integer:
M = Mint.Mfrac
Mint = Int(M)
Mfrac = Frac(M)× MaxInt
Prepared by Desta L.
Addition in fixed point representation occurs by adding fractional and integer
components separately, and only transferring any carry-over from the fractional result
to the integer result. The sequence could be implemented using the following two
integer additions:
ADD Yfrac,Mfrac ;
ADC Yint,Mint
Improved versions of these algorithms exist. For example, the following variations
exist on Bresenham's original algorithm:
Symmetry (forward and backward simultaneously)
Segmentation (divide into smaller identical segments - GCD(D x,D y) )
Double step, triple step, n step
Prepared by Desta L.
Line style and Thickness
setlinestyle() function-Sets the current line style and width or pattern
setlinestyle sets the style for all lines drawn by line, lineto, rectangle, drawpoly.
setlinestyle (linestyle, upattern, thickness); Line Styles
U pattern, thickness
U pattern- User can define its own pattern.
0 should be used if using predefined pattern,
other wise any integer number representing user
pattern
Thickness- Thickness of the line in pixels
Prepared by Desta L.
Introduction to Pixel
A pixel is the smallest unit of a digital image or graphic that can be displayed and
represented on a digital display device.
A pixel is the basic logical unit in digital graphics. Pixels are combined to form a
complete image, video, text or any visible thing on a computer display.
A pixel is also known as a picture element.
A pixel is represented by a dot or square on a computer monitor display screen.
Pixels are the basic building blocks of a digital image or display and are created
using geometric coordinates.
Prepared by Desta L. Computer graphics
Depending on the graphics card and display monitor, the quantity, size and
color combination of pixels varies and is measured in terms of the display
resolution.
For example, a computer with a display resolution of 1280 x 768 will produce a
maximum of 98,3040 pixels on a display screen.
Each pixel has a unique logical address, a size of eight bits or more and, in
most high-end display devices, the ability to project millions of different colors.
The pixel resolution spread also determines the quality of display; more pixels
per inch of monitor screen yields better image results.
Prepared by Desta L.
Setting a Pixel
Initial Task:
Turning on a pixel (loading the frame buffer/bit-map).
Assume the simplest case, i.e., an 8-bit, non-interlaced graphics system.
Then each byte in the frame buffer corresponds to a pixel in the output display
This technique uses the equation for a circle on radius r centered at (0, 0) given as:
Prepared by Desta L.
Subtracting (1) from (2), we get
Otherwise, if Pk > 0 or positive then yk+1 will be yk-1 and the formula to calculate
Pk+1 will be:
Prepared by Desta L. Computer graphics
Now a similar case that we observe in line algorithm is that how would be starting
Pk be evaluated.
For this at the start pixel position will be (0, r).
Therefore, putting this value in equation, we get
If radius r is specified as an integer, we can simply round p0 to:
Since all increments are integer. Finally sum up all in the algorithm:
Prepared by Desta L. Computer graphics
Now let us consider an example to calculate first octant of the circle using above
algorithm; while one quarter is displayed where you can observe that exact circle
is passing between the points calculated in a raster circle.
Prepared by Desta L. Computer graphics
Prepared by Desta L. Computer graphics
Filling Primitives: Rectangles, Polygons & Circles
Prepared by Desta L.
Complex: Complex polygons are just what their name suggests: complex. Complex
polygons are basically concave polygons that may have self-intersecting edges.
The complexity arises from distinguishing which side is inside the polygon when filling it.
In order to determine which pixels are inside the polygon, the odd-parity rule is
used within the scan-line polygon fill algorithm.
Prepared by Desta L.
Parity
Parity is a concept used to determine which pixels lie within a polygon, i.e. which
pixels should be filled for a given polygon.
The Underlying Principle: Conceptually, the odd parity test entails drawing a
line segment from any point that lies outside the polygon to a point P, which we
wish to determine whether it is inside or outside of the polygon.
Count the number of edges that the line crosses.
If the number of polygon edges crossed is odd, then P lies within the polygon.
Similarly, if the number of edges is even, then P lies outside of the polygon.
Prepared by Desta L. Computer graphics
Scan Line Algorithm
In order to fill a polygon, we do not want to have to determine the type of polygon
that we are filling.
The easiest way to avoid this situation is to use an algorithm that works for all three
types of polygons.
Since both convex and concave polygons are subsets of the complex type, using an
algorithm that will work for complex polygon filling should be sufficient for all three
types.
The scan-line polygon fill algorithm, which employs the odd/even parity concept
works for complex polygon filling.
Prepared by Desta L.
Reminder: The basic concept of the scan-line algorithm is to draw points from
edges of odd parity to even parity on each scan-line.
A scan-line is a line of constant y value, i.e., y = c, where c lies within our
drawing region, e.g., the window on our computer screen.
The following steps show how this algorithm works.
Step 1 − Find out the Ymin and Ymax from the given polygon.
Prepared by Desta L.
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.
Prepared by Desta L. Computer graphics
Flood Fill Algorithm
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
Prepared by Desta L. Computer graphics
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.
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. Prepared by Desta L. Computer graphics
The 4-Connected Polygon
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 seed point (seedx, seedy), fcolor and dcol.
Step 2 − Define the boundary values of the polygon.
Prepared by Desta L. Computer graphics
Step 3 − Check if the current seed 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 seed point.
setPixel(seedx, seedy, fcol)
Step 5 − Recursively follow the procedure with four neighborhood points.
FloodFill (seedx – 1, seedy, fcol, dcol)
FloodFill (seedx + 1, seedy, fcol, dcol)
FloodFill (seedx, seedy - 1, fcol, dcol)
FloodFill (seedx, seedy + 1, fcol, dcol)
Step 6 − Exit
Prepared by Desta L.
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.
Prepared by Desta L.
The 8-Connected Polygon
Step 3 − Check if the current seed point is of default color then repeat the steps 4 and 5 till the
boundary pixels reached.
Step 4 − Change the default color with the fill color at the seed point.
Prepared by Desta L.
FloodFill (seedx – 1, seedy, fcol, dcol)
Step 6 − Exit
Prepared by Desta L.
!!!
OU
K Y
A N
T H
Prepared by Desta L. Computer graphics