CG - Unit - Ii
CG - Unit - Ii
Basic Transformation
• Basic transformation includes three transformations Translation, Rotation, andScaling.
• These three transformations are known as basic transformation because with combination of these
three transformations we can obtain any transformation.
Translation
(𝒙′, 𝒚′)
𝒕𝒚
(𝒙, 𝒚)
𝒕𝒙
Page | 1
• Final coordinates after translation are [A’ (12, 11), B’ (17, 16), C’(22,11)].
Rotation
• It is a transformation that used to reposition the object along the circular path in the XY - plane.
• To generate a rotation we specify a rotation angle 𝜽 and the position of the Rotation Point (Pivot
Point) (𝒙𝒓,𝒚𝒓) about which the object is to berotated.
• Positive value of rotation angle defines counter clockwise rotation and negative value of rotation angle
defines clockwise rotation.
• We first find the equation of rotation when pivot point is at coordinate origin(𝟎,𝟎).
(𝒙′,𝒚′)
(𝒙, 𝒚)
𝜽
Page | 2
• Now replace 𝒓 𝐜𝐨𝐬 ∅ with 𝒙 and 𝒓 𝐬𝐢𝐧 ∅ with 𝒚 in above equation.
𝒙′ = 𝒙 𝐜𝐨𝐬 𝜽 − 𝒚 𝐬𝐢𝐧 𝜽
𝒚′ = 𝒙 𝐬𝐢𝐧 𝜽 + 𝒚 𝐜𝐨𝐬 𝜽
• We can write it in the form of column vector matrix equation as;
𝑷′ = 𝑹 ∙ 𝑷
(𝒙′, 𝒚′)
(𝒙, 𝒚)
𝜽
∅
(𝒙𝒓, 𝒚𝒓)
• Final coordinates after rotation are [A’ (4, -5), B’ (3, -8), C’ (8, -8)].
Page | 3
Scaling
Fixed Point
Page | 4
• Equation for scaling with fixed point position as (𝒙𝒇, 𝒚𝒇) is:
𝒙′ = 𝒙𝒇 + (𝒙− 𝒙𝒇)𝒔𝒙 𝒚′ = 𝒚𝒇 + (𝒚 −𝒚𝒇)𝒔𝒚
𝒙′ = 𝒙𝒇 + 𝒙𝒔𝒙 − 𝒙𝒇𝒔𝒙 𝒚′ = 𝒚𝒇 + 𝒚𝒔𝒚 − 𝒚𝒇𝒔𝒚
𝒙′ = 𝒙𝒔𝒙 + 𝒙𝒇(𝟏 − 𝒔𝒙) 𝒚′ = 𝒚𝒔𝒚 + 𝒚𝒇(𝟏 −𝒔𝒚)
• Matrix equation for the same will discuss in later section.
• Polygons are scaled by applying scaling at coordinates and redrawing while other body like circle and
ellipse will scale using its defining parameters. For example ellipse will scale using its semi major axis,
semi minor axis and center point scaling and redrawing at that position.
• Example: - Consider square with left-bottom corner at (2, 2) and right-top corner at (6, 6) apply the
transformation which makes its size half.
As we want size half so value of scale factor are 𝑠𝑥 = 0.5, 𝑠𝑦 = 0.5 and Coordinates of square are [A (2,
2), B (6, 2), C (6, 6), D (2,6)].
𝑃′ = 𝑆 ∙ 𝑃
• Final coordinate after scaling are [A’(1, 1), B’ (3, 1), C’ (3, 3), D’ (1,3)].
Page | 5
Translation
NOTE: - Inverse of translation matrix is obtain by putting −𝒕𝒙 & − 𝒕𝒚 instead of 𝒕𝒙 & 𝒕𝒚.
Rotation
Composite Transformation
• We can set up a matrix for any sequence of transformations as a composite transformation matrix by
calculating the matrix product of individual transformation.
• For column matrix representation of coordinate positions, we form composite transformations by
multiplying matrices in order from right to left.
Translations
• Two successive translations are performed as:
Here 𝑷′ and 𝑷 are column vector of final and initial point coordinate respectively.
• This concept can be extended for any number of successive translations.
Example: Obtain the final coordinates after two translations on point 𝑝(2,3) with translation vector
(4, 3) and (−1, 2) respectively.
Page | 6
Final Coordinates after translations are 𝑝,(5, 8).
Rotations
• Two successive Rotations are performed as:
Here 𝑷′ and 𝑷 are column vector of final and initial point coordinate respectively.
• This concept can be extended for any number of successive rotations.
Example: Obtain the final coordinates after two rotations on point 𝑝(6,9) with rotation angles are 30𝑜 and
60𝑜 respectively.
Scaling
• Two successive scaling are performed as:
Here 𝑷′ and 𝑷 are column vector of final and initial point coordinate respectively.
• This concept can be extended for any number of successive scaling.
Example: Obtain the final coordinates after two scaling on line 𝑝𝑞 [𝑝(2,2), 𝑞(8, 8)] with scaling factors are
(2, 2) and (3, 3) respectively.
Final Coordinates after rotations are 𝑝,(12, 12) and 𝑞,(48, 48).
Page | 7
General Pivot-Point Rotation
Here 𝑷′ and 𝑷 are column vector of final and initial point coordinate respectively and (𝒙𝒓, 𝒚𝒓) are the
coordinates of pivot-point.
• Example: - Locate the new position of the triangle [A (5, 4), B (8, 3), C (8, 8)] after its rotation by 90o
clockwise about the centroid.
Pivot point is centroid of the triangle so:
Page | 8
• Final coordinates after rotation are [A’ (11, 7), B’ (13, 4), C’(18,4)].
(c) (d)
(a) (b)
Scale Object with Translate Object so that
Original Position Translate Object so
Respect to Origin Fixed Point is Return to
of Object and that Fixed Point
Position (𝒙𝒇, 𝒚𝒇) .
Fixed Point (𝒙𝒇, 𝒚𝒇) is at Origin
Here 𝑷′ and 𝑷 are column vector of final and initial point coordinate respectively and (𝒙𝒇, 𝒚𝒇) are the
coordinates of fixed-point.
• Example: - Consider square with left-bottom corner at (2, 2) and right-top corner at (6, 6) apply the
transformation which makes its size half such that its center remains same.
Fixed point is center of square so:
As we want size half so value of scale factor are 𝑠𝑥 = 0.5, 𝑠𝑦 = 0.5 and Coordinates of square are [A (2,
2), B (6, 2), C (6, 6), D (2, 6)].
Page | 9
• Final coordinate after scaling are [A’(3, 3), B’ (5, 3), C’ (5, 5), D’ (3, 5)]
𝒔𝟏
Here 𝑷′ and 𝑷 are column vector of final and initial point coordinate respectively and 𝜽 is the angle
between actual scaling direction and our standard coordinate axes.
Other Transformation
• Some package provides few additional transformations which are useful in certain applications. Two
such transformations are reflection and shear.
Page | 10
Reflection
• A reflection is a transformation that produces a mirror image of anobject.
• The mirror image for a two –dimensional reflection is generated relative to an axis of reflection by
rotating the object 180o about the reflection axis.
• Reflection gives image based on position of axis of reflection. Transformation matrix for few positions
are discussed here.
y
1 Original
Position
2 3
x
2’ 3’
Reflected
Position
1’
y
1’ 1 Original
Reflected
Position Position
3’ 2’ 2 3
Page | 11
y
Original
3 Position
2
1
x
1’
3’
Reflected
Position 2’
y
Original x=y line
Position
3
2 1
1’
3’
Reflected
2’ Position
Page | 12
Transformation matrix for reflection about the line 𝒙 = −𝒚 .
x=-y line 3 y
1 2
Original
1’ Position
3’
2’
Reflected
Position
x
• Example: - Find the coordinates after reflection of the triangle [A (10, 10), B (15, 15), C (20, 10)] about x
axis.
• Final coordinate after reflection are [A’ (10, -10), B’ (15, -15), C’ (20,-10)]
Shear
• A transformation that distorts the shape of an object such that the transformed shape appears as if the
object were composed of internal layers that had been caused to slide over each other is called shear.
• Two common shearing transformations are those that shift coordinate x values and those that shift y
values.
Shear in 𝒙 − 𝒅𝒊𝒓𝒆𝒄𝒕𝒊𝒐𝒏 .
Before After
Y Shear Y Shear
X X
Page | 13
• Shear relative to 𝑥 − 𝑎𝑥𝑖𝑠 that is 𝑦 = 0 line can be produced by following equation:
𝒙′ = 𝒙 + 𝒔𝒉𝒙∙𝒚 , 𝒚′ = 𝒚
• Transformation matrix for that is:
Here 𝒔𝒉𝒙 is shear parameter. We can assign any real value to 𝒔𝒉𝒙.
• We can generate 𝑥 − 𝑑𝑖𝑟𝑒𝑐𝑡𝑖𝑜𝑛 shear relative to other reference line 𝑦 = 𝑦𝑟𝑒𝑓 with following equation:
𝒙′ = 𝒙 + 𝒔𝒉𝒙 ∙ (𝒚 − 𝒚𝒓𝒆𝒇) , 𝒚′ = 𝒚
• Transformation matrix for that is:
• Example: - Shear the unit square in x direction with shear parameter ½ relative to line 𝑦 = −1.
Here 𝑦𝑟𝑒𝑓 = −1 and 𝑠ℎ𝑥 = 0.5
Coordinates of unit square are [A (0, 0), B (1, 0), C (1, 1), D (0, 1)].
• Final coordinate after shear are [A’ (0.5, 0), B’ (1.5, 0), C’ (2, 1), D’ (1, 1)]
• Shear in 𝒚 − 𝒅𝒊𝒓𝒆𝒄𝒕𝒊𝒐𝒏 .
Before
Y Shear After
Y
Shear
X X
Here 𝒔𝒉𝒚 is shear parameter. We can assign any real value to 𝒔𝒉𝒚.
Page | 14
• We can generate 𝑦 − 𝑑𝑖𝑟𝑒𝑐𝑡𝑖𝑜𝑛 shear relative to other reference line 𝑥 = 𝑥𝑟𝑒𝑓 with followingequation:
𝒙′ = 𝒙, 𝒚′ = 𝒚 + 𝒔𝒉𝒚 ∙ (𝒙 −𝒙𝒓𝒆𝒇)
• Transformation matrix for that is:
• Example: - Shear the unit square in y direction with shear parameter ½ relative to line 𝑥 = −1.
Here 𝑥𝑟𝑒𝑓 = −1 and 𝑠ℎ𝑦 = 0.5
Coordinates of unit square are [A (0, 0), B (1, 0), C (1, 1), D (0, 1)].
• Final coordinate after shear are [A’(0, 0.5), B’ (1, 1), C’ (1, 2), D’ (0,1.5)]
Fig. 3.1: - A viewing transformation using standard rectangles for the window and viewport.
• Now we see steps involved in viewing pipeline.
Page | 15
Construct World- Map Viewing
Convert Map
Coordinate Scene Coordinate to
MC WC World- VC NVC Normalized DC
Using Modeling- Normalized Viewing
Coordinate to Viewport to
Coordinate Coordinates using
Viewing Device
Transformations Window-Viewport
Coordinates Coordinates
Specifications
Fig. 3.3: - A viewing-coordinate frame is moved into coincidence with the world frame in two steps: (a)
translate the viewing origin to the world origin, and then (b) rotate to align the axes of the two systems.
• We can obtain reference frame in any direction and at any position.
• For handling such condition first of all we translate reference frame origin to standard reference frame
origin and then we rotate it to align it to standardaxis.
• In this way we can adjust window in any reference frame.
• this is illustrate by following transformation matrix:
Page | 16
𝐌𝐰𝐜,𝐯𝐜 = 𝐑𝐓
• Where T is translation matrix and R is rotation matrix.
Window-To-Viewport Coordinate Transformation
• Mapping of window coordinate to viewport is called window to viewporttransformation.
• We do this using transformation that maintains relative position of window coordinate intoviewport.
• That means center coordinates in window must be remains at center position inviewport.
• We find relative position by equation as follow:
𝐱𝐯 − 𝐱𝐯𝐦𝐢𝐧 𝐱𝐰 − 𝐱 𝐰𝐦𝐢𝐧
=
𝐱𝐯𝐦𝐚𝐱 − 𝐱𝐰𝐦𝐚𝐱 − 𝐱𝐰𝐦𝐢𝐧
𝐱𝐯𝐦𝐢𝐧
𝐲𝐯 − 𝐲𝐯𝐦𝐢𝐧 𝐲𝐰 − 𝐲𝐰𝐦𝐢𝐧
=
𝐲𝐯𝐦𝐚𝐱 − 𝐲𝐰𝐦𝐚𝐱 − 𝐲𝐰𝐦𝐢𝐧
𝐲𝐯𝐦𝐢𝐧
• Solving by making viewport position as subject weobtain:
𝐱𝐯 = 𝐱𝐯𝐦𝐢𝐧 + (𝐱𝐰 −𝐱𝐰𝐦𝐢𝐧)𝐬𝐱
𝐲𝐯 = 𝐲𝐯𝐦𝐢𝐧 + (𝐲𝐰 −𝐲𝐰𝐦𝐢𝐧)𝐬𝐲
• Where scaling factor are :
𝐱𝐯𝐦𝐚𝐱 − 𝐱𝐯𝐦𝐢𝐧
𝐬𝐱 = 𝐱𝐰𝐦𝐚𝐱 − 𝐱𝐰𝐦𝐢𝐧
𝐲𝐯𝐦𝐚𝐱 − 𝐲𝐯𝐦𝐢𝐧
𝐬𝐲 = 𝐲𝐰𝐦𝐚𝐱 − 𝐲𝐰𝐦𝐢𝐧
• We can also map window to viewport with the set of transformation, which include following sequence
of transformations:
1. Perform a scaling transformation using a fixed-point position of (xWmin,ywmin) that scales the window
area to the size of the viewport.
2. Translate the scaled window area to the position of the viewport.
• For maintaining relative proportions we take (sx = sy). in case if both are not equal then we get stretched
or contracted in either the x or y direction when displayed on the outputdevice.
• Characters are handle in two different way one way is simply maintain relative position like other
primitive and other is to maintain standard character size even though viewport size is enlarged or
reduce.
• Number of display device can be used in application and for each we can use different window-to-
viewport transformation. This mapping is called the workstationtransformation.
Page | 17
Fig. 3.4: - workstation transformation.
Page | 18
• As shown in figure two different displays devices are used and we map different window-to-viewport on
each one.
Clipping Operations
• Generally, any procedure that identifies those portions of a picture that are either inside or outside of a
specified region of space is referred to as a clipping algorithm, or simply clipping. The region against
which an object is to clip is called a clipwindow.
• Clip window can be general polygon or it can be curved boundary.
Application of Clipping
• It can be used for displaying particular part of the picture on display screen.
• Identifying visible surface in 3D views.
• Antialiasing.
• Creating objects using solid-modeling procedures.
• Displaying multiple windows on same screen.
• Drawing and painting.
Point Clipping
• In point clipping we eliminate those points which are outside the clipping window and draw points which
are inside the clipping window.
• Here we consider clipping window is rectangular boundary with edge (xwmin,xwmax,ywmin,ywmax).
• So for finding wether given point is inside or outside the clipping window we use following inequality:
𝒙𝒘𝒎𝒊𝒏 ≤ 𝒙 ≤ 𝒙𝒘𝒂𝒎𝒙
𝒚𝒘𝒎𝒊𝒏 ≤ 𝒚 ≤𝒚𝒘𝒂𝒎𝒙
• If above both inequality is satisfied then the point is inside otherwise the point is outside the clipping
window.
Line Clipping
• Line clipping involves several possible cases.
1. Completely inside the clipping window.
2. Completely outside the clipping window.
3. Partially inside and partially outside the clipping window.
P9
P2 P2
P8
P1 P1
P5 P8
P3 P5 P6 P6
P7
P7
Page | 19
• Line which is completely inside is display completely. Line which is completely outside is eliminated from
display. And for partially inside line we need to calculate intersection with window boundary and find
which part is inside the clipping boundary and which part iseliminated.
• For line clipping several scientists tried different methods to solve this clipping procedure. Some of them
are discuss below.
Cohen-Sutherland Line Clipping
• This is one of the oldest and most popular line-clippingprocedures.
Algorithm
Step-1:
Assign region code to both endpoint of a line depending on the position where the line endpoint is located.
Step-2:
If both endpoint have code ‘0000’
Then line is completely inside.
Otherwise
Perform logical ending between this two codes.
Page | 20
Step-3:
Draw line segment which are completely inside and eliminate other line segment which found completely
outside.
Algorithm
1. Read two end points of line 𝑃1(𝑥1, 𝑦1) and 𝑃2(𝑥2,𝑦2)
2. Read two corner vertices, left top and right bottom of window: (𝑥𝑤𝑚𝑖𝑛, 𝑦𝑤𝑚𝑎𝑥) and (𝑥𝑤𝑚𝑎𝑥, 𝑦𝑤𝑚𝑖𝑛)
3. Calculate values of parameters 𝑝𝑘 and 𝑞𝑘 for 𝑘 = 1, 2, 3, 4 such that,
𝑝1 = −∆𝑥, 𝑞1 = 𝑥1 − 𝑥𝑤𝑚𝑖𝑛
𝑝2 = ∆𝑥, 𝑞2 = 𝑥𝑤𝑚𝑎𝑥 −𝑥1
𝑝3 = −∆𝑦, 𝑞3 = 𝑦1 − 𝑦𝑤𝑚𝑖𝑛
𝑝4 = ∆𝑦, 𝑞4 = 𝑦𝑤𝑚𝑎𝑥 − 𝑦1
4. If 𝑝𝑘 = 0 for any value of 𝑘 = 1, 2, 3, 4 then,
Line is parallel to 𝑘𝑡ℎ boundary.
Page | 21
Use boundary coordinates to draw line. And go to Step 8.
5. For 𝑘 = 1,2,3,4 calculate 𝑟𝑘 for nonzero values of 𝑝𝑘 and 𝑞𝑘 as follows:
𝑞𝑘
𝑟 = , 𝑓𝑜𝑟 𝑘 = 1,2,3,4
𝑘 𝑝𝑘
6. Find 𝑢1 𝑎𝑛𝑑 𝑢2 as given below:
𝑢1 = max{0, 𝑟𝑘|𝑤ℎ𝑒𝑟𝑒 𝑘 𝑡𝑎𝑘𝑒𝑠 𝑎𝑙𝑙 𝑣𝑎𝑙𝑢𝑒𝑠 𝑓𝑜𝑟 𝑤ℎ𝑖𝑐ℎ 𝑝𝑘 <0}
𝑢2 = min{1, 𝑟𝑘|𝑤ℎ𝑒𝑟𝑒 𝑘 𝑡𝑎𝑘𝑒𝑠 𝑎𝑙𝑙 𝑣𝑎𝑙𝑢𝑒𝑠 𝑓𝑜𝑟 𝑤ℎ𝑖𝑐ℎ 𝑝𝑘 >0}
7. If 𝑢1 ≤ 𝑢2 then
Calculate endpoints of clipped line:
𝑥 ′ = 𝑥 + 𝑢 ∆𝑥
1 1 1
𝑦1′ = 𝑦1 + 𝑢1∆𝑦
𝑥 ′ = 𝑥 + 𝑢 ∆𝑥
2 1 2
𝑦2′ = 𝑦 +
1 𝑢 ∆𝑦
2
Draw line (𝑥 ′1, 𝑦 ′,1𝑥 ′, 𝑦
2 );2
′
8. Stop.
Advantages
1. More efficient.
2. Only requires one division to update 𝑢1 and 𝑢2.
3. Window intersections of line are calculated just once.
P1
P1 P1
Fig. 3.7: - Three possible position for a line endpoint p1 in the NLN line-clipping algorithm.
• We can also extend this procedure for all nine regions.
• Now for p1 is inside the window we divide whole area in following region:
Page | 22
Fig. 3.8: - Clipping region when p1 is inside the window.
• Now for p1 is in edge region we divide whole area in followingregion:
Fig. 3.10: - Two possible sets of clipping region when p1 is in corner region.
• Regions are name in such a way that name in which region p2 falls is gives the window edge which
intersects the line.
• For example region LT says that line need to clip at left and top boundary.
• For finding that in which region line 𝒑𝟏𝒑𝟐 falls we compare the slope of the line to the slope of the
boundaries:
𝒔𝒍𝒐𝒑𝒆 𝒑𝟏𝒑𝑩𝟏 < 𝒔𝒍𝒐𝒑𝒆 𝒑𝟏𝒑𝟐 < 𝑠𝑙𝑜𝑝𝑒 𝒑𝟏𝒑𝑩𝟐
Where 𝒑𝟏𝒑𝑩𝟏 and 𝒑𝟏𝒑𝑩𝟐 are boundary lines.
• For example p1 is in edge region and for checking whether p2 is in region LT we use following equation.
Page | 23
• Otherwise calculate intersection.
• After finding region we calculate intersection point using parametric equation whichare:
• 𝒙 = 𝒙𝟏 + (𝒙𝟐 − 𝒙𝟏)𝒖
• 𝒚 = 𝒚𝟏 + (𝒚𝟐 − 𝒚𝟏)𝒖
• For left or right boundary 𝑥 = 𝑥𝑙 𝑜𝑟 𝑥𝑟 respectively, with 𝑢 = (𝑥𝑙/𝑟 – 𝑥1)/ (𝑥2 – 𝑥1), so that 𝑦 can be
obtain from parametric equation as below:
−𝒚𝟏
𝒚𝟐
• 𝒚 = 𝒚𝟏 + 𝒙 −𝒙 (𝒙 − 𝒙𝟏)
𝟐 𝟏
• Keep the portion which is inside and clip therest.
Polygon Clipping
• For polygon clipping we need to modify the line clipping procedure because in line clipping we need to
consider about only line segment while in polygon clipping we need to consider the area and the new
boundary of the polygon after clipping.
Sutherland-Hodgeman Polygon Clipping
• For correctly clip a polygon we process the polygon boundary as a whole against each window edge.
• This is done by whole polygon vertices against each clip rectangle boundary one by one.
• Beginning with the initial set of polygon vertices we first clip against the left boundary and produce new
sequence of vertices.
• Then that new set of vertices is clipped against the right boundary clipper, a bottom boundary clipper
and a top boundary clipper, as shown in figure below.
Fig. 3.12: - Processing the vertices of the polygon through boundary clipper.
• There are four possible cases when processing vertices in sequence around the perimeter of a polygon.
Page | 24
Fig. 3.13: - Clipping a polygon against successive window boundaries.
• As shown in case 1: if both vertices are inside the window we add only second vertices to outputlist.
• In case 2: if first vertices is inside the boundary and second vertices is outside the boundary only the
edge intersection with the window boundary is added to the output vertexlist.
• In case 3: if both vertices are outside the window boundary nothing is added to window boundary.
• In case 4: first vertex is outside and second vertex is inside the boundary, then adds both intersection
point with window boundary, and second vertex to the output list.
• When polygon clipping is done against one boundary then we clip against next window boundary.
• We illustrate this method by simple example.
Window
3
2’
2 1’
1 3’ 4
6
5’
4’
5
Page | 25
V2’ V2
(resume) V1’
V3’ V3
V4’
V1
V4
(stop)
V5’
(resume) V7’
V5
V6 V6’
(a) (b)
Fig. 3.14: - Clipping a concave polygon (a) with the Weiler-Atherton algorithm generates the two se
• As shown in figure we start from v1 and move clockwise towards v2 and add intersection point and next
point to output list by following polygon boundary, then from v2 to v3 we add v3 to outputlist.
• From v3 to v4 we calculate intersection point and add to output list and follow window boundary.
• Similarly from v4 to v5 we add intersection point and next point and follow the polygon boundary, next
we move v5 to v6 and add intersection point and follow the window boundary, and finally v6 to v1 is
outside so no need to add anything.
• This way we get two separate polygon section after clipping.
Page | 26