Sei sulla pagina 1di 9

Computer Graphics

Raster Scan Graphics


 Line Drawing Algorithms:-
A cathode ray tube (CRT) raster display is considered as matrix of discrete finite area cells
(pixels). Each of which can be made bright. It is not possible to directly draw a straight line from
one point to another. The process of determining which pixels provide the best approximation to
the desired line is properly known as rasterization. Following criteria is applied to computer
generated lines.
1. Lines should appear straight.
2. Lines should terminate accurately.
3. Lines should have constant density.
4. Line density should be independent of line length and angle.
5. Lines should be drawn rapidly.

 Digital Differential Analyzer (DDA):-


One technique for drawing straight line is to solve the differential equation for a straight
line, i.e.
dy/dx = constant or Δx/Δy = (y2 – y1) / (x2 – x1)
Digital differential analyzer algorithm is as follows…...
Step1: Start

Step2: if abs(x2 – x1) >= abs(y2 – y1) then


Length = abs(x2 – x1)
else
Length = abs(y2 – y1)
end if

Step3: Select the larger of Δx or Δy to be one raster unit.


Δx = (x2 – x1) / Length
Δy = (y2 – y1) / Length

Step4: Round the values rather than truncate, so that center pixel addressing is handled correctly.
x = x1 + 0.5
y = y1 + 0.5

Step5: i = 1

Step6: Begin main loop


while (i <= Length)
setpixel(Integer(x), Integer(y))
x = x + Δx
y = y + Δy
i=i+1
end while
Step7: Finish
Example:-

YCIT SYBCS Page 1


Computer Graphics

Initial Calculation
x1 = 0 y1 = 0 x2 = 5 y2 = 5
Length = 5
Δx = 1 Δy = 1
x = 0.5 y = 0.5

Incrementing through the main loop yields…

i setpixel x y

0.5 0.5
1 (0, 0)
1.5 1.5
2 (1, 1)
2.5 2.5
3 (2, 2)
3.5 3.5
4 (3, 3)
4.5 4.5
5 (4, 4)
5.5 5.5

 Bresenham’s Algorithm:-
The Bresenham line algorithm is an algorithm which determines which points in an n-
dimensional raster should be plotted in order to form a close approximation to a straight line
between two given points. It is commonly used to draw lines on a computer screen, as it uses only
integer addition, subtraction and bit shifting, all of which are very simple operations in standard
computer architectures. It is one of the earliest algorithms developed in the field of computer
graphics.
The algorithms seek to select the optimum raster locations that represent a straight line.
To accomplish this, the algorithm always increments by one unit either in x or y, depending on the
slope of the line. The increment in the other variable is determined by examining the distance
between the actual line and the nearest grid locations. This distance is called the error.
The algorithm is cleverly constructed so that only the sign of the error term need to be
examined. Bresenham’s line rasterization algorithm is as follows.

Bresenham’s line rasterization algorithm for the first octant


Step1: Start

Step2: x=x1
y=y1
Step3: Δx=x2-x1
Δy=y2-y1

YCIT SYBCS Page 2


Computer Graphics

Step4: m=Δy/Δx

Step5: e=m-0.5

Step6: begin the main loop


for i=1 to Δx
SetPixel(x,y)
while(e>0)
y=y+1
e=e-1
end while
x=x+1
e=e+m
next i

Step7: Finish
Example:- Consider the line from (0,0) to (5,5). Rasterizing the line with the Bresenham algorithm
yields.

x=0 y=0
Δx=5 Δy=5
M=1 e=1-0.5=0.5

 Integer Bresenham’s Algorithm:-


Bresenham’s line rasterization algorithm requires the use of floating point arithmetic to
calculate the slope of the line and to evaluate the error term. The speed of algorithm is increased
by using integer arithmetic.
The sign of the error term is important, the simple transformation e=2eΔx of the error
term in the line rasterization algorithm yields an integer algorithm. Integer algorithm for the first
octant is as follows.

Step1: Start

Step2: x=x1
y=y1

Step3: Δx = x2 – x1
Δy = y2 – y1

Step4: e = 2 * Δy – Δx

Step5: begin the main loop


for i = 1 to Δx
setpixel(x,y)
while(e > 0)
y=y+1

YCIT SYBCS Page 3


Computer Graphics

e = e – 2 * Δx
end while
x=x+1
e = e + 2 * Δy
next i

Step6: Finish

 General Bresenham’s Algorithm:-


A full implementation of Bresenham’s algorithm requires modification for lines lying in the
other octants. These can easily be developed by considering the quadrant in which the line lies, and
the line’s slope. When the absolute magnitude of the slope of the line is greater than 1, y is
incremented by 1 and Bresenham’s error criterion is used to determine when to increment x.
Whether x or y is incremented by 1 depends on the quadrant. The general Bresenham’s algorithm
for all quadrants is as follows.
Step1: Start

Step2: x = x + 1
y=y+1

Step3: Δx = abs(x2 – x1)


Δy = abs(y2 – y1)

Step4: s1 = Sign((x2 – x1)


s2 = Sign(y2 – y1)

Step5: Interchange Δx and Δy, depending on the slope of the line


if Δy > Δx then
Temp = Δx
Δx = Δy
Δy = Temp
Interchange = 1
else
Interchange = 0
end if

Step6: e = 2 * Δy – Δx

Step7: main loop


for i = 1 to Δx
setpixel(x,y)
while(e > 0)
if Inerchange = 1 then
x = x + s1
else
y = y + s2

YCIT SYBCS Page 4


Computer Graphics

end if
e = e – 2 * Δx
end while
if Interchange = 1 then
y = y + s2
else
x = x + s1
end if
e = e + 2 * Δy
next i

Step8: Finish

Transformation
Two Dimensional Transformation:-
A graphical system should allow the programmer to define pictures that include a variety of
transformations.
For example:

YCIT SYBCS Page 5


Computer Graphics

1. He should be able to magnify a picture so that detail appears more clearly, or reduce it so
that more of the picture is visible.
2. He should be able to apply transformations to symbols.
3. It is also useful to be able to change the scale of a symbol and to rotate it through some
angle.

Two aspects of the formulation of transformations should be emphasized.


a. A transformation is a single mathematical entity and as such can be denoted by a single
name or symbol.
b. Two transformations can be combined, or concatenated, to yield a single transformation
with the same effect as the sequential application of the original two.

Basically transformation has only three basic transformation i.e. translation, scaling and
rotation. But generally we use five basic transformations.
i. Translation
ii. Scaling
iii. Rotation
iv. Reflection
v. Shearing

Translation:-
Moving the image or object is called as translation. It moves the object without
deformation. It means that every point of the object will move by same amount.
We can translate a 2D point by translation distance tx and ty to original coordinates x and
y. This addition of translated distance moves the point from (x, y) to (x1, y1). The translation is as
follows.
x1 = x + tx
y1 = y + ty

. P (x , y )
1 1 1

. P(x, y)
Fig:- Translation of point P with P1 vector

The translation distance (tx, ty) is called as translation vector.

Scaling:-
The scaling transformations can be used for a variety of purposes. If the picture is to be
enlarged to twice its original size we might choose Sx = Sy = 2. Scaling means magnifying or reducing
the size of an object. The scaling is as follows.
x1 = xSx y1 = ySy

YCIT SYBCS Page 6


Computer Graphics

Rotation:-
The rotation is used for rotating a point (x, y) through a clockwise angle about the origin of
the coordinate system. The two dimensional rotation is applied along a circular path. If rotation is
positive then the object will move clockwise and if rotation angle is negative then the object will
move anticlockwise. The rotation can also be applied along the origin of coordinates system through
which the object can be rotated.

Reflection:-
The reflection is the transformation that produces a mirror image of an original object.
These implements for 2D coordinate system. There are following types of reflection.
1. Reflection about x-axis:-
This reflection produces object with one dimension get changed i.e. y-axis value.
This is shown in following figure.
x1 = x y1 = -y

y y

x x

Original Image Reflection about x-axis

2. Reflection about y-axis:-


In this reflection the image is reflected about y-axis and gets change x-axis value
as shown in following figure.

x1 = -x y1 = y
y y

x x

YCIT SYBCS Page 7


Computer Graphics

Original Image Reflection about y-axis

3. Reflection about origin:-


The reflection of original image in 2D coordinate system about the origin is as shown
in following figure.
x1 = -x y1 = -sy

y y

x x

Original Image Reflection about origin

Shearing:-
A transformation that distorts (changing angles, deformation) the shape of original object
such that the new shape appear. The shear transformation causes image deformation which makes
change in the angles of value in both coordinates x & y. There are two types of shearing i.e. x-
shearing and y-shearing.
x-shearing maintains y coordinate value but the change is made in x value. The y-shearing
maintains all the x coordinates value as it is but shifts y-axis coordinate value.

Matrix Representation:-
Two dimensional transformations can be represented in a uniform way by 3 X 3 matrix. The
transformation of a point (x, y) to a new point (x1, y1) by means of any sequence of translation,
rotations, and scaling is then represented as

[x’ y’ 1] = [x y 1] a d 0
b e 0
c f 1

YCIT SYBCS Page 8


Computer Graphics

Where the 3 X 3 matrix completely specifies the transformation. The matrix represents the
transformation. The parameters of the 3 X 3 transformation matrix can be arranged to make the
matrix represent the simple transformations of translation, rotation, and scaling.

Translation:-
[x’ y’ 1] = [x y 1] 1 0 0
0 1 0
Tx Ty 1

Rotation:-
[x’ y’ 1] = [x y 1] cos -sinθ 0
sinθ cosθ 0
0 0 1

Scaling:-
[x’ y’ 1] = [x y 1] Sx 0 0
0 Sy 0
0 0 1

YCIT SYBCS Page 9

Potrebbero piacerti anche