Computer Graphics: Geometry and Line Generation

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

GEOMETRY AND LINE GENERATION

LINE DRAWING ALGORITHMS


CHAPTER IV  In order to display any object on a computer screen, the most important information
is which pixel have to be highlighted for accomplishing it. Most human operators
generally think in terms of more complex graphic objects such as cubes, cones,
Computer Graphics circles and ellipses. The process of representing continuous graphics objects as a
collection of discrete pixels is called scan conversion.
 Several line drawing algorithms are developed. Their basic objective is to
enable visually satisfactory images in least possible time. This is achieved by
reducing the calculations to a minimum. This is by using integer arithmetic rather
than floating point arithmetic. This way of minimizing even a single arithmetic
operation is important. This is because every drawing or image generated will have a
large number of line segments in it and every line segment will have many pixels.
So saving of one computation per pixel will save number of computations in
generating an object. This in turn minimizes the time required to generate the whole
image on the screen. The computer screen is divided into rows and columns. The
intersection area of these rows and columns is known as pixel (in short pel).
1 2

 In order to draw a line on the screen, first we have to determine m = (y2-y1)/(x2-x1) (2)
c = y1 - (m.x1) (3)
which pixels have to switched ON or are to be highlighted. The
process of determining which combination of pixels provides the Algorithms for displaying straight lines are based on the equations (1), (2) and (3).
best approximation to the desired line is known as rasterization.
For any given x interval ∆x along a line, we can compute the corresponding y interval
General requirements for drawing a line from equation (2) as
Lines must appear to be straight
• Lines should start and end accurately
∆y=m. ∆x (4)
• Lines should have constant brightness along their length

• Lines should be drawn rapidly Similarly, we can obtain the x interval ∆x corresponding to specified ∆y as
 The Cartesian slope-intercept equation for a straight line is
y=m.x+c (1) ∆x= ∆y/m (5)
 Where ‘ m’ is the slope of the line and c is the y- intercept. (x, y) is
We know that the general equation of a line is y = mx + c. Where m is the slope of the
any point on the line.
line and c is the y- intercept. (x, y) is any point on the line. We also know that the slope
 Suppose we are given the 2 end points of a line. of the above line can be expressed as:-
 (xstart, ystart) and (xend, yend). m = (yend – ystart)/( xend - xstart)
3 4
DDA (Digital Differential Analyzer) algorithm Example: Consider a line from (2,3) to (6,6). Use DDA algorithm to rasterize this line.

DDA is a scan-conversion line algorithm based on either ∆y or ∆x, using equation (4) Solutions: Initializations:
or (5). DDA line drawing algorithm can be written as:- (xa, ya) = (2,3)
1. Input the two line end points (xa, ya) and (xb, yb ) (xb, yb ) = (6,6)

2. Calculate dx = xb - xa dy = yb - ya dx= xb-xa=6-2=4

3. If abs(dx)>=abs(dy) then Length= abs(dx) else Length= abs(dy) dx=4

∆x=dx/length dy= yb-ya=6-3=3


dy=3
∆y=dy/length x=xa
Since abs(dx)>=abs(dy), length=abs(dx)=4
y=ya putpixel(ceil(x),ceil(y)) i=1
length=4
4. while (i<=length)
∆x=dx/length=4/4=1
x=x+∆x y=y+∆y
∆x=1
putpixel(ceil(x),ceil(y))
∆y=dy/length=3/4=0.75
i=i+1 end while ∆y=0.75
5. Finish 5 x=xa=2 x=2 y=ya=3 y=3 Continue………….. 6

First pixel will be plotted at (2,3) Plotting begins: Bresenham’s Line Generation
For i=1 x=x+∆x=2+1=3 y=y+∆y=3+0.75=3.75 The Bresenham algorithm is another incremental scan conversion
algorithm. The big advantage of this algorithm is that, it uses only integer
First pixel will be plotted at (3,3.75)
calculations. Moving across the ‘x’ axis in unit intervals and at each step
For i=2 x=x+∆x=3+1=4 choose between two different ‘y’ coordinates.
y=y+∆y=3.75+0.75=4.5 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
First pixel will be plotted at (4,4.5)
is closer to the original line.
For i=3 x=x+∆x=4+1=5
y=y+∆y=4.5+0.75=5.25
First pixel will be plotted at (5,5.25)
For i=4 x=x+∆x=5+1=6 y=y+∆y=5.25+0.75=6
First pixel will be plotted at (6,6)

7
At sample position Xk+1, the vertical separations from the mathematical So, d upper and d lower are given as follows
line are labelled as dupper and dlower
d lower = y − yk = m(Xk+1)+b−Yk
and
d upper = (yk+1) − y = Yk+1−m(Xk+1) − b

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.

d lower − d upper = 2m(xk+1)−2yk+2b−1


From the above illustration, the y coordinate on the mathematical line
at xk+1 is Let us substitute m with dy/dx where dx and dy are the differences
between the end-points.
Y = m(Xk+1) + b dx(d lower − d upper) = dx(2dydx(xk+1)−2yk+2b−1)
=2dy.xk−2dx.yk+2dy+2dx(2b−1)
Continue………… =2dy.xk−2dx.yk+C
Continue…….. 10

 So, a decision parameter Pk for the kth step along a line is given by − Now, keeping in mind all the above points and calculations, here is the
Pk = dx(d lower − d upper) = 2dy.xk−2dx.yk+C Bresenham algorithm for slope m < 1
 The sign of the decision parameter Pk is the same as that of • Step 1 − Input the two end-points of line, storing the left end-point in
d lower − d upper (x0,y0)
 If pk is negative, then choose the lower pixel, otherwise choose the • Step 2 − Plot the point (x0,y0)
upper pixel. • Step 3 − Calculate the constants dx, dy, 2dy, and 2dy–2dx
 Remember, the coordinate changes occur along the x axis in unit and get the first value for the decision parameter as p0=2dy−dx
steps, so you can do everything with integer calculations. At step k+1, • Step 4 − At each Xk along the line, starting at k = 0, perform the
the decision parameter is given as following test −
pk+1=2dy.xk+1−2dx.yk+1+C If pk< 0, the next point to plot is (xk+1,yk) and pk+1=pk+2dy
 Subtracting pk from this we get Otherwise, (xk,yk+1)
pk+1−pk=2dy(xk+1−xk)−2dx(yk+1−yk) pk+1=pk+2dy−2dx
 But, xk+1 is the same as (xk)+1 So • Step 5 − Repeat step 4 dx–1 times.
pk+1 = pk+2dy − 2dx(yk+1−yk) • For m > 1, find out whether you need to increment x while
Where, Yk+1–Yk is either 0 or 1 depending on the sign of Pk incrementing y each time.
 The first decision parameter p0 is evaluated at (x0,y0)is given as • After solving, the equation for decision parameter Pk will be very
p0=2dy−dx Continue…….. similar, just the x and y in the equation gets interchanged.
12
Drawing a circle on the screen is a little complex than drawing a line. Bresenham’s Algorithm for generating circle
There are two popular algorithms for generating a circle − Bresenham’s  We cannot display a continuous arc on the raster display. Instead, we
Algorithm and Midpoint Circle Algorithm. These algorithms are based have to choose the nearest pixel position to complete the arc.
on the idea of determining the subsequent points required to draw the  From the following illustration, you can see that we have put the pixel
circle. Let us discuss the algorithms in detail − at X,Y location and now need to decide where to put the next pixel −
The equation of circle is x2 + y2 = r2 where r is radius. at N (X+1,Y )or at S (X+1,Y−1).
This can be decided by the
decision parameter d.
If d <= 0, then N(X+1,Y)
is to be chosen as next pixel.
If d > 0, then S (X+1,Y−1)
is to be chosen as the next
pixel.

13 14

Algorithm Draw Circle Method(X, Y, P, Q).


Step 1 − Get the coordinates of the center of the circle and radius, and Call Putpixel (X + P, Y + Q).
store them in x, y, and R respectively. Set P=0 and Q=R. Call Putpixel (X - P, Y + Q).
Call Putpixel (X + P, Y - Q).
Step 2 − Set decision parameter D = 3 – 2R. Call Putpixel (X - P, Y - Q).
Call Putpixel (X + Q, Y + P).
Step 3 − Repeat through step-8 while P ≤ Q. Call Putpixel (X - Q, Y + P).
Call Putpixel (X + Q, Y - P).
Step 4 − Call Draw Circle X,Y,P,Q Call Putpixel (X - Q, Y - P).
Step 5 − Increment the value of P.

Step 6 − If D < 0 then D = D + 4P + 6.

Step 7 − Else Set R = R - 1, D = D + 4P−Q + 10.

Step 8 − Call Draw Circle X,Y,P,Q Continue…… 15 16


Polygons: A polygon is any 2-dimensional shape formed with straight  A special class of polygon exists; it happens for polygons whose sides
lines. Triangles, quadrilaterals, pentagons, and hexagons are all are all the same length and whose angles are all the same. When this
examples of polygons. The name tells you how many sides the shape happens, the polygons are called regular polygons. A stop sign is an
has. For example, a triangle has three sides, and a quadrilateral has four example of a regular polygon with eight sides. All the sides are the
sides. So, any shape that can be drawn by connecting three straight lines same and no matter how you lay it down, it will look the same. You
is called a triangle, and any shape that can be drawn by connecting four wouldn't be able to tell which way was up because all the sides are the
straight lines is called a quadrilateral. same and all the angles are the same.

 When a triangle has all the sides and angles the same, we know it as
All of these shapes are polygons. Notice how all the shapes are drawn an equilateral triangle, or a regular triangle. A quadrilateral with all
with only straight lines? This is what makes a polygon. If the shape had sides and angles the same is known as a square, or regular
curves or didn't fully connect, then it can't be called a polygon. The quadrilateral. A pentagon with all sides and angles the same is called
shape with arrow like is still a polygon even if it looks like it has an a regular pentagon. An n-gon with sides and angles the same is called
arrow. All the sides are straight, and they all connect. The shape with
17
a regular n-gon. 18
arrow like has 11 sides.

Angles of Regular Polygons


 Regular polygons also have two different angles related to them. The
first is called the exterior angle and it is the measurement between
the shape and each line segment when you stretch it out past the
shape.

 However many sides a polygon has is the same number of exterior


angles it has. So, a pentagon with five sides has five exterior angles. A
hexagon will have six exterior angles and so on. For regular polygons,
we can figure out the measurement of the exterior angle, but for
polygons that aren't regular, we can't. Here is the formula for regular
polygons:
19

You might also like