Computer Graphics: Geometry and Line Generation
Computer Graphics: Geometry and Line Generation
Computer Graphics: Geometry and Line Generation
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)
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.
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
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.