UNIT 3 Two Dimensional Algorithm
UNIT 3 Two Dimensional Algorithm
com
Two Dimensional
1
https://genuinenotes.com
2
https://genuinenotes.com
2D Coordinate System
1. Modeling Coordinates
• Modeling coordinates are used to construct shape of individual parts
(objects or structures) of a 2D scene. For example, generating a circle
at the origin with a “radius” of 2 units.
• Here, origin (0, 0), and radius 2 units are modeling coordinates.
2D Coordinate System..
2. World Coordinates
• World coordinates are used to organize the individual parts
into a scene.
• World coordinates units define overall scene to be modeled.
4
https://genuinenotes.com
2D Coordinate System..
3. Viewing Coordinates
• Viewing coordinates are used to define particular view of the user.
Viewer‟s position and view angle i.e. rotated/translated.
• Viewing coordinates specify the portion of the output device that is
to be used to present the view.
5
View Coordinates
https://genuinenotes.com
2D Coordinate System..
4. Device Coordinates or Screen Coordinates
• The display coordinate system is called device coordinate
system. Device coordinates are specific to output device.
• Device coordinates are integers within the range (0, 0) to
6
Device Coordinates
https://genuinenotes.com
2D Coordinate System..
7
https://genuinenotes.com
8
Window to Viewport transformation
https://genuinenotes.com
Window Viewport
=
=
Where 10
https://genuinenotes.com
13
https://genuinenotes.com
Clipping
• The process of identifying those portions of a picture that are
either inside or outside of the specified region of space is
called clipping.
Two possible ways to apply clipping in the viewing transformation:
1. Apply clipping in the world coordinate system: ignore objects that
15
https://genuinenotes.com
Clipping..
Why Clipping?
1. Excludes unwanted graphics from the screen.
2. Improves efficiency, as the computation dedicated to
objects that appear off screen can be significantly reduced.
16
https://genuinenotes.com
Point Clipping
• Let W denote a clip window with coordinates (xwmin, ywmin),
(Xwmin, Ywmax), (Xwmax, Ywmin), (Xwmax, Ywmax), then a
vertex (x, y) is displayed only if following “point clipping”
inequalities are satisfied:
Xwmin ≤ x ≤ Xwmax,
17
https://genuinenotes.com
Line Clipping
• Lines that do not intersect the clipping window are either
completely inside the window or completely outside the
window. On the other hand, a line that intersects the clipping
window is divided by the intersection point(s) into segments
that are either inside or outside the window.
19
https://genuinenotes.com
Line Clipping…
Ywmax
Ywmin 21
Xwmin Xwmax
https://genuinenotes.com
Step 1 : Establish Region code for all line end point
• First bit is 1, if x < Xwmin (Point lies to left of window) , else set it to 0
• Second bit is 1, if x > Xwmax (Point lies to right of window) , else set it to 0
• Third bit is 1, if y < Ywmin (Point lies to below window), else set it to 0
• Fourth bit is 1, if y > Ywmax (Point lies to above window) , else set it to 0
Fourth Bit
Third Bit
Xwmin Xwmax
https://genuinenotes.com
Step 2: Determine which lines are completely inside window
and which are not
a) If both end points of line has region codes ‘0000’ line is
completely inside window.
b) If logical AND operation of region codes of two end points is
NOT ‘0000’. The line is completely outside (some bit position
have 1’s)
C 1001
D 0001 D
AND
0001
Completely Outside 23
https://genuinenotes.com
Step 3 : If both tests fail then line is partially visible so we need to
find the intersection with boundaries of window .
• Yi= Y1 + m (X – X1)
Polygon Clipping
• A polygon can be defined as a geometric object "consisting of
a number of points (called vertices) and an equal number of
line segments (called sides or edges). Polygon clipping is
defined as the process of removing those parts of a polygon
that lie outside a clipping window. Consider a general polygon
Window Window
29
Before clipping After clipping
https://genuinenotes.com
Original Polygon Clip Left Clip Right Clip Bottom Clip Top
30
https://genuinenotes.com
31
https://genuinenotes.com
32
https://genuinenotes.com
Pixel Position
• Pixel position: referenced by scan line number and column
number
DDA Algorithm
Digital Differential Analyzer
Scan-conversion line – algorithm based on calculating either
35
https://genuinenotes.com
CASE 1
Positive Slope
(Y always increase when X increases and
Y always Decreases when X decrease)
Positive Slope
1. Slope less than 1 ( |M| < 1 ) and
moving Left to Right
M=1
37
X1 X2
CASE 1 https://genuinenotes.com
Positive Slope
2. Slope less than 1 ( |M| < 1 ) and
moving Right to Left
M=1
38
X1 X2
CASE 1 https://genuinenotes.com
Positive Slope
3. Slope greater than 1 ( |M| > 1 ) and
moving Left to Right
M=1
Y2
X1 X2 39
CASE 1 https://genuinenotes.com
Positive Slope
4. Slope Greater than 1 ( |M| > 1 ) and
moving Right to Left
M=1
Y2
X1 X2 40
https://genuinenotes.com
CASE 2
Negative Slope
1. Slope less than 1 ( |M| < 1 ) and
moving Left to Right
M=-1
X2 41
X1
https://genuinenotes.com
CASE 2
Negative Slope
2. Slope less than 1 ( |M| < 1 ) and
moving Right to Left
M=-1
X2 42
X1
https://genuinenotes.com
CASE 2
Negative Slope
3. Slope greater than 1 ( |M| > 1 ) and
moving Left to Right
M=-1
X1 X2 43
https://genuinenotes.com
CASE 2
Negative Slope
4. Slope Greater than 1 ( |M| > 1 ) and
moving Right to Left
M=-1
X1 X2 44
DDA
https://genuinenotes.com
Q.> Digitize a Line with end point A(3,2) and B(8,4) , using DDA
47
https://genuinenotes.com
DDA Final Exercise
1. Digitize the line from A(1,1) to B(10,8) using scan conversion line
algorithm.
2. Draw a line with two end point P(5,3) and Q(1,2) using DDA
3. Digitize the line with endpoints A(1,7) and B(6,3) using digital
differential analyzer line drawing algorithm. Show all necessary
steps. (TU 2013, 3 marks) .
+1 +1
+1 +1 50
https://genuinenotes.com
Let us assume that pixel (xk, yk) is already
plotted assuming that the sampling direction is
along X-axis i.e. (xk+1, yk) or (xk+1, yk+1). Thus, the
common equation of the line is
y = m(xk+1) + c
Now,
51
https://genuinenotes.com
Here,
Pk+1- Pk = 2 Δy Xk+1+2 Δy + 2 Δx c - 2 Δx yk+1 – Δx – {2 Δy Xk+2
Δy + 2 Δx c - 2 Δx yk – Δx }
53
Pk+1 = Pk +2 Δy(Xk+1 – Xk)- 2 Δx(Yk+1– Yk)
https://genuinenotes.com
Case 1
If Pk < 0 (i.e. d1-d2 is Negative )
then,
Xk+1 = Xk+1
Yk+1 = Yk
Pk = Pk + 2 Δy
Pk = Pk + 2 Δy -2Δx 54
https://genuinenotes.com
Initial Decision Parameter (P0)
y = m(xk+1) + c
Let, X0 = 0 , Y0 = 0 then C = 0
P0 = 2 Δy - Δx
55
https://genuinenotes.com
Example-1: Digitize the line with end points (20, 10) and (30, 18)
using BLA.
Solution :
Here, Starting point of line = (x1, y1) = (20, 10) and
Ending point of line = (x2, y2) = (30, 18)
Thus, slope of line, m = Δy / Δx = y2-y1 / x2-x1
= (18-10) / (30-20)
56
Thus, https://genuinenotes.com
The initial decision parameter (P0) = 2Δy - Δx = 2*8 – 10 = 6
Since, for the Bresenham’s Line drawing Algorithm of slope, |m| ≤ 1, we have
+1 +1
+1 +1
60
+1 +1
https://genuinenotes.com
d1 = X - Xk = {yk +1 – c}/m - xk
and
d2 = (Xk +1)- X = xk+1– {yk+1 – c}/m 61
https://genuinenotes.com
So,
d1 - d2 = [{yk +1 – c}/m - xk] -[xk+1–
{{yk+1 – c}/m }]
Or,
d1 - d2 = 2/m* (yk+1) - 2c/m – 2xk -1
Pk = Pk + 2 Δx
d2
d2
Y
Y
d1
Case 1
If Pk < 0 (i.e. d1-d2 is Negative )
then,
Xk+1 = Xk - 1
Yk+1 = Yk
Pk = Pk + 2 Δy
d1 d2 d1
d2
Pk = Pk + 2 Δx
if Pk < 0 If Pk ≥ 0 If Pk < 0 If Pk ≥ 0
Xk+1 = Xk+1 Xk+1 = Xk+1 Xk+1 = Xk Xk+1 = Xk+1
Yk+1 = Yk Yk+1 = Yk+1 Yk+1 = Yk+1 Yk+1 = Yk+1
Pk = Pk + 2 Δy Pk = Pk + 2 Δy -2Δx Pk = Pk + 2 Δx Pk = Pk + 2 Δx -2Δy
P0 = 2 Δy - Δx P0 = 2 Δx - Δy
If Pk < 0 If Pk ≥ 0 If Pk < 0 If Pk ≥ 0
Xk+1 = Xk - 1 Xk+1 = Xk - 1 Xk+1 = Xk Xk+1 = Xk - 1
Yk+1 = Yk Yk+1 = Yk +1 Yk+1 = Yk +1 Yk+1 = Yk +1
Pk = Pk + 2 Δy Pk = Pk + 2 Δy -2Δx Pk = Pk + 2 Δx Pk = Pk+ 2Δx - 2 Δy 85
P0 = 2 Δy - Δx P0 = 2 Δx - Δy
Circle Algorithm
https://genuinenotes.com
What is Circle ?
• Similarly to the case with lines, there is an incremental
algorithm for drawing circles – the mid-point circle algorithm
• In the mid-point circle algorithm we use eight-way symmetry
so only ever calculate the points for the top right eighth of a
circle, and then use symmetry to get the rest of the points
Pk<0
Pk≥0 (Xk+1,Yk-1/2)
Pk< 0 Pk ≥ 0
XK+1 = Xk+1 XK+1 = Xk+1
87
YK+1 = Yk YK+1 = Yk-1
https://genuinenotes.com
(Xk+1,Yk)
(Xk,Yk)
Pk<0
Case 1 : Pk < 0
Xk+1 = Xk +1
Yk+1 = Yk
Pk≥0 (Xk+1,Yk-1/2)
Pk+1 = Pk + 2(Xk+1) + 1
Pk+1 = Pk + 2Xk+1 + 1
Now, (1,r-1)
P0 = 12+(r-1/2 )2 – r2
= 1 + r2- r + ¼ - r2
= 5/4 – r
P0 ≈ 1-r
92
P0 ≈ 1-r
https://genuinenotes.com
Example : Digitize a circle with radius 10 at center (0,0)
Solution
Here, the initial decision parameter (P0) =1 – r = 1-10 = - 9
Since, for the Midpoint Circle Algorithm of initial point(0, r) &
center at origin (0, 0) rotating at clockwise direction, we have
Initial point (x0,y0) = (0,10)
P0=1-r = 1-10 = -9
98
https://genuinenotes.com
Pk<0
(Xk-1,Yk-1/2) Pk ≥ 0
Pk< 0 Pk ≥ 0
XK+1 = Xk-1 XK+1 = Xk-1
100
YK+1 = Yk YK+1 = Yk-1
Here, https://genuinenotes.com
Decision parameter(Pk) = Fcircle (Xk -1,Yk-1/2)
= (Xk -1)2 + (Yk-1/2)2 - r2
Then, K+1th term is,
Pk+1= (Xk+1 -1)2 + (Yk+1 - 1/2)2 - r2
Now,
Pk+1 – Pk = (Xk+1 -1)2 + (Yk+1 - 1/2)2 - r2 – {(Xk -1)2 + (Yk-1/2)2 - r2}
= -2(xk-1)+(Y2k+1-Y2k) – (Yk+1 – Yk) + 1
i.e.,
(x0,y0) = (0,r)
(-1,r-1/2)
Here ,
Now, (-1,r-1)
P0 = (-1)2+(r-1/2 )2 – r2
= 1 + r2- r + ¼ - r2
= 5/4 – r
P0 ≈ 1-r
102
P0 ≈ 1-r
https://genuinenotes.com
2D Geometric
103
https://genuinenotes.com
Introduction
Basic Transformations
The orientation, size, and shape of the output primitives
are accomplished with geometric transformations that alter the
coordinate descriptions of objects. The basic geometric
Introduction
Basic Transformations
1. Translation
HOMOGENOUS FORM
Expressing position in homogeneous coordinates allows
us to represent all geometric transformations equation as matrix
multiplications.
( x, y) -------------------- (xh, yh ,h)
(x,y)-------------------------(x, y, 1).
106
https://genuinenotes.com
1. Translation
A translation is applied to an object by repositioning it
along a straight-line path from one coordinate location to
another. We translate a two-dimensional point by adding
translation distances, tx, and ty, to the original coordinate
position (x, y) to move the point to a new position (x ' , y').
107
https://genuinenotes.com
1. Translation
We can write equation as a single matrix equation by using
column vectors to represent coordinate points and translation
vectors. Thus,
108
https://genuinenotes.com
2. ROTATION
A two-dimensional rotation is applied to an object by
repositioning it along a circular path in the xy plane. To generate
a rotation, we specify a rotation angle θ and the position (xr , yr)
of the rotation point (or pivot point) about which the object is to
be rotated.
109
https://genuinenotes.com
2. ROTATION
At Origin
2. ROTATION
In homogeneous co-ordinate
Anticlockwise direction
-
112
https://genuinenotes.com
3.Scaling
A scaling transformation alters the size of an object. This
operation can be carried out for polygons by multiplying the coordinate
values (x, y) of each vertex by scaling factors sx and sy to produce the
transformed coordinates (x', y').
• sx scales object in ‘x’ direction
• sy scales object in ‘y’ direction
3.Scaling
Thus, for equation form,
x' = x. sx and
y’ = y. sy
3.Scaling
P‘= S.P
4. Reflection
A reflection is a transformation that produces a mirror
image of an object. The mirror image for a 2D reflection is
generated relative to an axis of reflection by rotating the object
180" about the reflection axis. We can choose an axis of
reflection in the xy-plane or perpendicular to the xy plane.
116
https://genuinenotes.com
4. Reflection
(i) Reflection about x axis or about line y = 0
x
Keeps value same but flips Y value of
coordinate points
Homogeneous co-ordinate
117
https://genuinenotes.com
4. Reflection
(ii) Reflection about y axis or about line x = 0
Homogeneous co-ordinate
118
https://genuinenotes.com
4. Reflection
(iii) Reflection about origin
x’ = -x
Homogeneous co-ordinate
119
https://genuinenotes.com
4. Reflection
(iv) Reflection about line y = x
x’ = y
y’ = x
4. Reflection
(v) Reflection about line y = -x
x’ = -y
y’ = -x
5. Shearing
It distorts the shape of object in either ‘x’ or ‘y’ or both
direction. In case of single directional shearing (e.g. in ‘x’
direction can be viewed as an object made up of very thin layer
and slid over each other with the base remaining where it is).
Shearing is a non-rigid-body transformation that moves objects
122
https://genuinenotes.com
5. Shearing
5. Shearing
Homogenous Coordinates
The matrix representations for translation, scaling and rotation
are respectively:
• Translation: P’ = T + P (Addition)
• Scaling: P’ = S . P (Multiplication)
• Rotation: P’ = R . P (Multiplication)
Homogenous Coordinates
Homogenous Coordinates
Here, in case of homogenous coordinates we add a third
coordinate ‘h’ to a point (x, y) so that each point is represented
by (hx, hy, h). The ‘h’ is normally set to 1. If the value of ‘h’ is
more the one value then all the co-ordinate values are scaled by
this value.
Homogenous Coordinates
• For translation
• For rotation
Here, figure-a shows the Counter Clockwise (CCW) rotation & figure-b
shows the Clockwise (CW) rotation.
128
• For scaling https://genuinenotes.com
• For Reflection
• Reflection about x-axis
129
https://genuinenotes.com
Composite Transformation
With the matrix representation of transformation
equations it is possible to setup a matrix for any sequence of
transformations as a composite transformation matrix by
calculating the matrix product of individual transformation.
Forming products of transformation matrices is often referred to
131
https://genuinenotes.com
132
(Xf,Yf)
https://genuinenotes.com
(Xf,Yf) 133
https://genuinenotes.com
134
https://genuinenotes.com
(Xf,Yf)
(Xf,Yf) (Xf,Yf)
https://genuinenotes.com
0
0
0
Xf
Yf
1
.
Cos θ -Sin θ
Sin θ Cos θ
0 0 1
0
0
.
1
0
0
0
-Xf
-Yf
136
https://genuinenotes.com
Exercise 1
Q. N. > Rotate the triangle (5, 5), (7, 3), (3, 3) in counter
clockwise (CCW) by 90 degree.
A:
P’ = R. P
=
.
[ ]
-5 -3 -3
= 5 7 3 137
1 1 1
https://genuinenotes.com
Exercise 2
Q.>Rotate the triangle (5, 5), (7, 3), (3, 3) about fixed point (5, 4)
in counter clockwise (CCW) by 90 degree.
A : Solution
Here, the required steps are:
138
https://genuinenotes.com
Thus, the composite matrix is given by
Com = T(xf, yf) . Rθ .T(-xf, -yf)
P’ = Com . P
141
https://genuinenotes.com
142
(Xf,Yf)
https://genuinenotes.com
(Xf,Yf) 143
https://genuinenotes.com
144
https://genuinenotes.com
(Xf,Yf)
(Xf,Yf) (Xf,Yf)
https://genuinenotes.com
0
0
0
Xf
Yf
1
.
Sx
0
0
Sy
0 1
0
0
.
1
0
0
0
-Xf
-Yf
146
https://genuinenotes.com
Exercise 2
Q.N.1. > Find the coordinate of a triangle A(1,3) , B(2,5) and C(3,3)
after twice its original size about fixed point (2,3)
Q.N.2. > Find the coordinate of a triangle A(1,3) , B(2,5) and C(3,3)
after being rotated about fixed point p(2,4) by 45 in clockwise
Q.N.4 >Find the coordinate of a triangle (5, 5), (7, 3), (3, 3) after scaling
(2,3) and then rotate 45 CCW then translate (1,1) and then reflect y=0 147
line.
https://genuinenotes.com
Q.N.1 > Reflect an object (2, 3), (4, 3), (4, 5) about line y = x +1.
Solution
Here,
The given line is y = x +1.
Thus,
When x = 0, y=1
When x = 1, y=2
(0,1)
(C=1)
Also, (0,0)
The slope of the line (m) = 1 153
Thus, the rotation angle (θ) = Tan-1(m) = Tan-1(1) = 450
https://genuinenotes.com
Here, the required steps are:
• Translate the line to origin by decreasing the y-intercept with
one.
• Rotate the line by angle 450 in clockwise direction so that the
given line must overlap x-axis.
• Reflect the object about the x-axis.
• Reverse rotate the line by angle -450 in counter-clockwise
direction.
154
https://genuinenotes.com
Exercise
Q.N.> Reflect a triangle A(1,8) B(3,8) and C(1,6)
about line y= x+2
158
https://genuinenotes.com
Exercise (Imp)
Q.N> A mirror is placed such that it passes through (0,10),
(10, 0). Fin the mirror image of an object (6,7), (7, 6), (6, 9).
Solution
162
https://genuinenotes.com
Exercise
• Rotate a triangle A(5,6), B(6,2) and C(4,1) by 45 degree about
an arbitrary pivot point (3,3).
• Find the coordinate of a line A(1,2) ,B(3,3) after translate (2,2)
then rotate 35o angle clockwise and then again translate the
line on (-2,-2) and rotate Counter clockwise direction of 35o
163
https://genuinenotes.com
Unit 3 Finished