Вы находитесь на странице: 1из 6

Computer Graphics

Chapter-2
Graphics primitives

Coordinate System:
Most graphics packages are designed to use Cartesian coordinate system. More
than one Cartesian system may be referenced by a single graphics package. Since,
different o/p device required different co-ordinate system. The coordinate referenced
by a user are called “World-coordinate system” or “User co-ordinate system”.
A Coordinate system used by a particular o/p device is called “device co-
ordinate” or “Screen co-ordinate” in the case of video monitor.
World-coordinate definition allows the user to set any convenient dimension.
Architect layout might be specified in fraction of foot, surveyor is specified in terms of
kilometer.

Picture Picture Picture D


definition in definition in definition in E
Word Normalized Device V
Co-ordinates Co-ordinates Co-ordinates I
C
E

The world coordinate definition are given to the graphics system, it converts
these coordinate values into appropriated device coordinate system. A typical
procedure used in graphical package is to convert user coordinate definition into
normalized coordinate system and then converted to device coordinate system. This
makes the system flexible enough to accommodate the number of o/p devices. The
normalized x-y coordinate are assigned the values in the interval (0, 1). This
normalized coordinate are transferred to the device coordinate in the range (0,0) to
(xmax,ymax).

SSCW Tumkur
Computer Graphics

Point-plating Technique:
Line drawing algorithm:
Straight lines are used in computer generated pictures. They occur in
black diagrams, bar-charts and graphs, civil and mechanical engineering drawings
etc., the criteria required to generate straight lines are:
1. Lines should appear straight:
The techniques used in computers are suited to the generation of lines
which are parallel or at an angle of 45o to the x & y axis other lines crated problems.
A line segment through it starts and finishes at the required points appears as
thought they have passed through many intermediate points. In such cases it is
necessary for us to approximate the line by choosing those points which should fall on
the actual path.

2. Lines should terminate accurately: When lines are not plotted accurately, they
may terminate in the wrong place. The effect is seen as a small gap between the end
point of one line and the starting point of the next or as a cumulative error.

3. Lines should have constant density: To maintain constant density, dot should
be equally spaced. This can be achieved only in lines parallel or at 45o to the axes. In
other case, the spacing must be as even as possible; otherwise bunching of dots will
be visible.

4. Line density should be independent of line length and angle: Before plotting
a line its exact length must be determined which involves computing a square root.
Also the number of dots to be plotted in terms of distance must be calculated. This
can not be done easily. The best way is to compute an approximate line length and
use the line generation algorithm that keeps line density constant within that
estimate.

5. Line should be drawn rapidly: In an interactive system it is necessary for us to


perform our actions rapidly. To achieve these criteria a minimum number of
computations should be done and the hardware should be special purpose and
compatible.

SSCW Tumkur
Computer Graphics

DDA line drawing algorithm [Digital Differential Analyzer]

Algorithm: [(x1, y1) & (x2, y2) are the two end points, dx is the interval in x-axis,
dy is the interval in dy-axis, step is the number of repetition, x & y are the plotting
points and I is the loop control variable]
1. start

2. read (x1,y1) & (x2,y2)

3. [calculate]

dx  x2-x1 and dyy2-y1

4. [to find the step value]

if (dx >dy) then

step  dx

else

step  dy

endif

5. [to calculate x and y increment]

xinc dx/step and yinc  dy/step

6. [assign the value of first end point to x and y and print]

x x1 and yy1

plotpoint(x,y,red)

7. [loop]

for i 1 to step do

begin

x  x+ xinc

y  y + yinc

plotpoint(x,y,red)

end for

8. stop

SSCW Tumkur
Computer Graphics

Bresenham’s Line algorithm


Procedure: A more efficient line algorithm to determine pixel position
developed by Bresenham’s. Finds the closest integer co-ordinates to the actual line
path using only integer arithmetic.
Algorithm: [(x1,y1) &(x2,y2) are the two end points, dx & dy are interval in x & y-
axis respectively, p is the slope xend is the terminated point]
1. start
2. Read (x1,y1) and (x2,y2)
3. [Calculate]
Dx  x2-x1 and dy  y2-y1
4. [To find the line path is + ve or –ve slope]
If (x1 <x2) then
X  x1
Y  y1
Xend  x2
Else
X  x2
Y  y2
Xend  x1
Endif
5. plotpoint(x,y,RED)
6. [Calculate first point slope ] p 2 *(dy-dx)
7. [loop]
While (x <= xend) do
a. [increment] xx +1
b. [check the previous slope]
If (p<0) then
[calculate] p p + 2 *dy
Else
[increment] yy +1
[calculate] p p+ 2 * (dy-dx)
End if
c. plot point(x,y,RED)
end while
8. stop

SSCW Tumkur
Computer Graphics

Comparison between DDA and Bresenham’s Line algorithm.

DDA Bresenham’s
Xinc and yinc are calculated based on the The increment value of x and y is always
formale. unity.
There is no decision parameter There is a decision parameter used.
Both x and y values are incremented at Depending on the decision parameter the
every step. value of y is either incremented or left as
it is. However x value is incremented ata
every step.
No special cases Several cases exists, such as slope <0
and slope >0

General Circle Algorithm

DDA Algorithm: [ (xc, yc ) is the center of the circle, r is the radius, x and y are the
plotting points , θ is the angle of rotation’ ]
1. start
2. [Read (xc, yc ) and radius r]
3. [loop]
For θ  to 360 do
Begin
a. [calculate x and y values]
X  xc +r.cos θ
Y  yc + r. sin θ
b. [plot point]
putpixel(x,y)
end for
4. stop

Bresenham’s Circle algorithm:

In circle generating algorithm, integer position along circular path can be


obtained by determining which of two pixel nearer to the circle path at each step.
To simplify the algorithmic statements, First consider the circle center ate co-

ordinate origin (xc=0, yc=0), we also calculate the points for ⅛th circle segments. Unit
step is taken in x-coordinate, starting from (xc - r) to (xc + r). The starting value for
plotting the pixel position is x=0 and y = r. The simulation at some arbitrary steps in
an algorithm is as shown in the above diagram. We assume that the position(xi, yi) as
been determined to be closer to the specified circle path. The next pixel position is
either (xi+1,yi) or (xi+1, yi-1). Before selecting the next pixel position to calculate the
first parameter as p =3-2*r

SSCW Tumkur
Computer Graphics

If p<0, then next pixel position is at (xi+1,yi) and calculate the next parameter
as p = p+2*x +1

If p>=0, then next pixel position is at (xi+1, yi+1) and calculate the next
parameter as p = p+2(x-y) +1
Repeat the above procedure until the x-coordinate is reaches to r or
x<=r.

Algorithm: [(xc, yc) is the centre of the circle, r is the radius of the circle, x and y
are the plotting points, p is the slope or constant parameter.]
1. start
2. [Read (xc ,yc) and radius r]
3. [assign]
X  0 and y r
4. [Calculate first parameter]
P  3 – 2*r
5. [Loop]
While(x<=r) do
Begin
a. [plot 8 pixels]
Plotpoint(xc + x , yc + y , 1)
Plotpoint(xc + x , yc - y , 1)
Plotpoint(xc - x , yc + y , 1)
Plotpoint(xc - x , yc - y , 1)
Plotpoint(xc + y , yc + x , 1)
Plotpoint(xc + y , yc - x , 1)
Plotpoint(xc - y , yc + x , 1)
Plotpoint(xc - y , yc - x , 1)
b. [ Check the parameter]
If p<0 then
P <- p+2*x+1
Otherwise
Y<- y -1
P <- p +2*(x-y) +1
Endif
c. [Increment x value]
X <- x+1
End while
5. stop.

 Write a program to pie chart depiction of the results of an election between 4


parties.
 Program to draw a vertical bar chart / Histogram of ABC Car company for car
production of 4 years.

SSCW Tumkur

Вам также может понравиться