Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Module 2

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

Computer Graphics Module II

POINTS AND LINES


A line connects two points. It is a basic element in graphics. To
draw a line, you need two points between which you can draw a line. In
the following three algorithms, we refer the one point of line as X1,Y1
and the second point of line as X2,Y2.
The Cartesian slope intercept equation
y=mx+b……………………..(1)
Where m representing the slope of the line and b as the y intercept.
Line with endpoint (x1,y1) and (x2,y2)
m=(y2-y1) /(x2-x1)……………….(2)
b=y-mx………………………...(3)
for any given x interval ▲x along a line, we can compute the corresponding y
interval ▲y from equation (2)as
▲y=m.▲x………………………….(4)
Similarly we can obtain the x interval ▲x corresponding to a
specified ▲y as ▲x=▲y/m …………………………(5)

DDA Algorithm
DDA stands for Digital Differential Analyzer. It is an incremental method of
scan conversion of line. In this method calculation is performed at each step

1
Computer Graphics Module II

but by using results of previous steps.


There are four possible cases
1. Left to right m<=1
2. Left to right m >1
3. Right to left m<=1
4. Right to left m>1
We first consider a line with positive slope less than or equal to 1 then ▲x=1
The line of equation will be
yk+1 = yk +m......................(6)
Consider a line with positive slope greater than 1 then ▲y=1
xk+1=xk+(1/m) …………………….(7)
We first consider a line with negative slope less than or equal to 1
then ▲x=1 yk+1 = yk -m......................(8)
Consider a line with negative slope greater than 1 then ▲y=1
xk+1=xk-(1/m) …………………….(9)
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

2
Computer Graphics Module II

software. But it is less suited for hardware implementation.


DDA Algorithm:
Step1: Start Algorithm
Step2: Declare x1,y1,x2,y2,dx,dy,x,y as integer variables.
Step3: Enter value of x1,y1,x2,y2.
Step4: Calculate dx = x2-x1
Step5: Calculate dy = y2-y1
Step6: If ABS (dx) > ABS (dy)
Then step = abs (dx)
Else
step = abs (dy)
Step7: xinc=dx/step
yinc=dy/step
assign x = x1
assign y = y1
Step8: Set pixel (x, y)
Step9: x = x + xinc
y = y + yinc
Set pixels (Round (x), Round (y))
Step10: Repeat step 9 until x = x2
Step11: End Algorithm
PROGRAM
#include<device.h>
#define ROUND(a) ((int)(a+0.5))
void lineDDA(int x1,int y1,int x2,int y2) )
{
int dx,dy,step,k;

3
Computer Graphics Module II

float x_inc,y_inc,x,y;
x=x1; y=y1;
dx=abs(x2-x1); dy=abs(y2-y1);
if(dx>=dy)
step=dx;
else
step=dy;
x_inc=dx/step;
y_inc=dy/step;
setPixel(ROUND(x),ROUND(y))
for(k=0;k<steps;k++)
{
x=x+x_inc;
y=y+y_inc;
setPixel(ROUND(x),ROUND(y))
}
Example: A line has a starting point (9,18) and ending point (14,22). Apply
the DDA algorithm to plot a line.

Solution: P1 (9,18) P2 (14,22)


x1=9 dy = 22-18=4
y1=18 step=5
x2= 14 x_inc=5/5=1
y2=22 y_inc=4/5=0.8
dx = 14-9=5

4
Computer Graphics Module II

K xk yk ploat
9 18 (9,18)
1 10 18.8 (10,19)
2 11 19.6 (11,20)
3 12 20.4 (12,20)
4 13 21.2 (13,21)
5 14 22 (14,22)

Bresenham’s Line Algorithm


An accurate, efficient raster line drawing algorithm developed by “Jack
Elton Bresenham” in 1962, scan converts lines using only incremental integer
calculations that can be adapted to display circles and other curves. Keeping in
mind the symmetry property of lines, lets derive a more efficient way of drawing a
line. Starting from the left end point (x0,y0) of a given line , we step to each
successive column (x position) and plot the pixel whose scan-line y value closest
to the line path. Assuming we have determined that the pixel at (xk,yk) is to be
displayed, we next need to decide which pixel to plot in column xk+1.
Choices are (xk +1, yk) and (xk+1, yk+1)

d1 = y – yk = [m(xk + 1) + b ]– yk
d2 = (yk + 1) – y = yk + 1 -[m(xk + 1) – b]
The difference between these 2 separations is
d1-d2 = 2m(xk + 1) – 2 yk + 2b – 1

5
Computer Graphics Module II

A decision parameter pk for the kth step in the line algorithm can be
obtained by rearranging above equation so that it involves only integer
calculations Define
Pk = ▲x ( d1-d2) = 2 ▲y.xk -2▲ x.yk + c…………………..(1)
The sign of Pk is the same as the sign of d1-d2, since x > 0.
Parameter c is a constant and has the value 2▲ y + ▲x(2b-1)
If pixel at yk is closer to line-path than pixel at yk +1 (i.e, if d1 < d2) then pk is
negative. We plot lower pixel in such a case. Otherwise, upper pixel will be
plotted.
At step k + 1, the decision parameter can be evaluated as,
pk+1 = 2▲ y.xk+1 - 2 ▲x.yk+1 + c…………………………(2)
Taking the difference of pk+ 1 and pk we get the following.
pk+1 – pk = 2 ▲y(xk+1- xk) - 2▲x(yk+1 – yk)
But, xk+1 = xk +1, so that
pk+1 = pk + 2▲ y - 2 ▲ x(yk+1 – yk)
Where the term yk+1-yk is either 0 or 1, depending on the sign of parameter pk
The first parameter p0 is directly computed
p0 = 2▲ y - ▲x

ALGORITHM
1. Input the two end points and store the left end point in (x0,y0)
2. Load (x0,y0) into the frame buffer (plot the first point)

6
Computer Graphics Module II

3. Calculate the constants ▲ x, ▲ y, 2▲ y and 2▲ y- ▲x and obtain the


starting value for the decision parameter as
p0 = 2 ▲y- ▲ x
4. At each xk along the line, starting at k=0, perform the following test:
If pk < 0 , the next point is (xk+1, yk) and
pk+1 = pk + 2▲ y
Otherwise Point to plot is (xk+1, yk+1)
pk+1 = pk + 2▲ y - 2 ▲x
5. Repeat step 4 (above step) x times
Example
A line has a starting point (9,18) and ending point (14,22). Apply the
Bresenham‟s Line Drawing algorithm to plot a line.
Solution: We have two coordinates,
Starting Point = (x1, y1) = (9,18)
Ending Point = (x2, y2) = (14,22)
Step 1: First, we calculate ▲x, ▲y.
▲x = x2 – x1 = 14-9 = 5
▲y = y2 – y1 = 22-18 = 4
Step 2: Now, we are going to calculate the decision parameter (pk)
pk = 2▲y-▲x
=2x4–5=3
The value of pk = 3
Step 3: Now, we will check both the cases.
If
pk >= 0
Then
Case 2 is satisfied. Thus
pk+1 = pk +2▲y-2▲x =3+ (2 x 4) – (2 x 5) = 1

7
Computer Graphics Module II

xk+1 =xk +1 = 9 + 1 = 10
yk+1 =yk +1 = 18 +1 = 19
Step 4: Now move to next step. We will calculate the coordinates until
we reach the end point of the line.
▲x -1 = 5 – 1 = 4
Step 5: Stop.
pk pk+1 xk+1 yk+1
9 18
3 1 10 19
1 -1 11 20
-1 7 12 20
7 5 13 21
5 3 14 22

The Coordinates of drawn lines are


P1 = (9, 18) P4 = (12, 20)
P2 = (10, 19) P5 = (13, 21)
P3 = (11, 20) P6 = (14, 22)

8
Computer Graphics Module II

#include “device.h”
void drawline(int xa, int ya, int xb, int yb)
{ }
int dx, dy, p, x, y,xEnd; setPixel(x,y);
dx=abs(xb-xa);
dy=abs(yb-ya);
int twoDy=2*dy; while(x<xEnd)
int twoDyDx=2*(dy-dx); {
if(xa>xb) x++;
{ if(p<0)
x=xa; p=p+twoDy;
y=ya; else
xEnd=xa; {
} x++
else p=p+twoDyDx}
{ }
x=xb; setPixel(x,y);
y=yb; }
xEnd=xb; }

Advantages
● It is simple to implement because it only contains integers.
● It is quick and incremental
● It is fast to apply but not faster than the Digital Differential Analyzer (DDA)
algorithm.
● The pointing accuracy is higher than the DDA algorithm.
Disadvantages
● The Bresenham’s Line drawing algorithm only helps to draw the basic line.
● The resulted draw line is not smooth.
Midpoint Circle Algorithm
The midpoint circle drawing algorithm helps us to calculate the
complete perimeter points of a circle for the first octant. We can quickly

9
Computer Graphics Module II

find and calculate the points of other octants with the help of the first
octant points. The remaining points are the mirror reflection of the first
octant points.
This algorithm is used in computer graphics to define the coordinates
needed for rasterizing the circle. The midpoint circle drawing algorithm
helps us to perform the generalization of conic sections. Bresenham’s
circle drawing algorithm is also extracted from the midpoint circle
drawing algorithm. In the algorithm, we will use the 8-way symmetry
property.
In this algorithm, we define the unit interval and consider the nearest
point of the circle boundary in each step.
Let us assume we have a point a (p, q) on the boundary of the
circle and with r radius satisfying the equation fc (p, q) = 0

Circle function around the origin is given by


fcircle(x,y) = x2+ y2– r2
Any point (x,y) on the boundary of the circle satisfies the equation and circle
function is zero

Midpoint Circle Algorithm


For a point in the interior of the circle, the circle function is negative and for
a point outside the circle, the function is positive

10
Computer Graphics Module II

Thus,
fcircle(x,y) < 0 if (x,y) is inside the circle boundary
fcircle(x,y) = 0 if (x,y) is on the circle boundary
fcircle(x,y) > 0 if (x,y) is outside the circle boundary

Assuming we have just plotted the pixel at (xk,yk) , we next need to


determine whether the pixel at position (xk + 1, yk-1) is closer to the circle
Our decision parameter is the circle function evaluated at the midpoint
between these two pixels
pk = fcircle (xk +1, yk -1/2)
= (xk +1)2 + (yk -1/2)2– r2
If pk < 0 , this midpoint is inside the circle and the pixel on the scan line yk is
closer to the circle boundary. Otherwise, the mid position is outside or on the
circle boundary, and we select the pixel on the scan line yk-1
Successive decision parameters are obtained using incremental
calculations Pk+1 = fcircle(xk+1+1, yk+1-1/2)
= [(xk+1)+1]2+ (yk+1 -1/2)2–r2
OR
Pk+1 = Pk+2(xk+1) + (y2k+1 – y2k) – (yk+1- yk)+1
Where yk+1 is either yk or yk-1, depending on the sign of pk
2xk+1 = 2xk +2
2 yk+1 = 2yk – 2
At the start position (0,r) , these two terms have the values 2 and 2r-2

11
Computer Graphics Module II

respectively Initial decision parameter is obtained by evaluating the circle function


at the start position
(x0,y0) = (0,r)
p0 = fcircle(1, r-1/2) = 1+ (r-1/2)2-r2
OR
P0 = 5/4 -r
If radius r is specified as an integer, we can round p0 to p0 = 1-r
The actual algorithm
1: Input radius r and circle center (xc,yc) and obtain the first point on the
circumference of the circle centered on the origin as
(x0,y0) = (0,r)
2: Calculate the initial value of the decision parameter as
P0 = 5/4 - r
3: At each xk position starting at k = 0 , perform the following test:
If pk < 0 , the next point along the circle cantered on (0,0) is
(xk+1, yk) and pk+1 = pk + 2xk+1 + 1
Otherwise the next point along the circle is (xk+1, yk-1) and
pk+1 = pk + 2xk+1 +1 -2yk+1
Where 2xk+1 = 2xk+2 and 2yk+1 = 2yk-2
4: Determine symmetry points in the other seven octants
5: Move each calculated pixel position (x,y) onto the circular path centered on
(xc,yc) and plot the coordinate values
x = x+ xc , y= y+ yc
6: Repeat steps 3 through 5 until x >= y

12
Computer Graphics Module II

Program
class circlemidpoint(int xcenter, int ycenter,int radius)
{
int x=0;
int y=radius;
int p=1-radius;
void circleplot(int,int,int,int);
circleplot(xcenter, ycenter, x,y)
while(x<y)
{
x++;
if(p<0)
p=p+2*x+1;
else
{
y--;
p=p+2*(x-y)+1;
}
circleplot(xcenter, ycenter, x,y)

}
}
void circleplot(int xcenter, int ycenter,int x,int y)
{
setPixel(xcenter+x, ycenter+y);
setPixel(xcenter-x, ycenter+y);
setPixel(xcenter+x, ycenter-y);

13
Computer Graphics Module II

setPixel(xcenter-x, ycenter-y);
setPixel(xcenter+y, ycenter+x);
setPixel(xcenter-y, ycenter+x);
setPixel(xcenter+y, ycenter-x);
setPixel(xcenter-y, ycenter-x);
}
Example
1. Draw a circle with radius 10
P0=1-r
1-10= -9
2x0=0
2y0=20

14
Computer Graphics Module II

Character Generation

1) STROKE METHOD

Stroke method is based on natural method of text written by human being. In


this method graph is drawing in the form of line by line.
Line drawing algorithm DDA follows this method for line drawing. This method uses
small line segments to generate a character. The small series of line segments are
drawn like a stroke of pen to form a character. We can build our own stroke method
character generator by calls to the line drawing algorithm. Here it is necessary to
decide which line segments are needed for each character and then drawing these
segments using line drawing algorithm.

2)BITMAP METHOD

Bitmap method is a called dot-matrix method as the name suggests this method
use array of bits for generating a character. These dots are the points for array whose
size is fixed. In bit matrix method when the dots is stored in the form of array the
value 1 in array represent the characters i.e. where the dots appear we represent that
position with numerical value 1 and the value where dots are not present is
represented by 0 in array. It is also called dot matrix because in this method characters
are represented by an array of dots in the matrix form. It is a two dimensional array
having columns and rows.

A 5x7 array is commonly used to represent characters. However 7x9 and 9x13 arrays
are also used. Higher resolution devices such as inkjet printer or laser printer may use
character arrays that are over 100x100.

15
Computer Graphics Module II

3) STARBUST METHOD:

In this method a fix pattern of line segments are used to generate characters. Out of
these 24 line segments, segments required to display for particular
character are highlighted. This method of character generation is called starbust
method because of its characteristic appearance
The starbust patterns for characters A and M. the patterns for particular characters are
stored in the form of 24 bit code, each bit representing one line segment. The bit is set
to one to highlight the line segment; otherwise it is set to zero. For example, 24-bit
code for Character A is 0011 0000
0011 1100 1110 0001 and for character M is 0000 0011 0000 1100 1111 0011.
This method of character generation has some disadvantages. They are
1. The 24-bits are required to represent a character. Hence more memory is required.
2. Requires code conversion software to display character from its 24- bitcode.
3. Character quality is poor. It is worst for curve shaped characters.

Character A : 0011 0000 0011 1100 11100001


Character M: 0000 0011 0000 1100 11110011

16

You might also like