Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
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
Initial Calculation
x1 = 0 y1 = 0 x2 = 5 y2 = 5
Length = 5
Δx = 1 Δy = 1
x = 0.5 y = 0.5
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.
Step2: x=x1
y=y1
Step3: Δx=x2-x1
Δy=y2-y1
Step4: m=Δy/Δx
Step5: e=m-0.5
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
Step1: Start
Step2: x=x1
y=y1
Step3: Δx = x2 – x1
Δy = y2 – y1
Step4: e = 2 * Δy – Δx
e = e – 2 * Δx
end while
x=x+1
e = e + 2 * Δy
next i
Step6: Finish
Step2: x = x + 1
y=y+1
Step6: e = 2 * Δy – Δx
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:
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.
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
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
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
x1 = -x y1 = y
y y
x x
y y
x x
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
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