You are on page 1of 24

CS 8092 COMPUTER GRAPHICS III YR VI SEM 2017 REGULATION

UNIT II TWO-DIMENSIONAL GRAPHICS 

Two dimensional geometric transformations – Matrix representations and


homogeneous coordinates, composite transformations; Two dimensional viewing –
viewing pipeline, viewing coordinate reference frame; window-to-viewport
coordinate transformation, Two dimensional viewing functions; clipping
operations – point, line, and polygon clipping algorithms.

Transformation means changing some graphics into something else by applying


rules. We can have various types of transformations such as translation, scaling up
or down, rotation, shearing, etc. When a transformation takes place on a 2D plane,
it is called 2D transformation.
Transformations play an important role in computer graphics to reposition the
graphics on the screen and change their size or orientation.
Homogenous Coordinates
To perform a sequence of transformation such as translation followed by rotation
and scaling, we need to follow a sequential process −

 Translate the coordinates,


 Rotate the translated coordinates, and then
 Scale the rotated coordinates to complete the composite transformation.
To shorten this process, we have to use 3×3 transformation matrix instead of 2×2
transformation matrix. To convert a 2×2 matrix to 3×3 matrix, we have to add an
extra dummy coordinate W.
In this way, we can represent the point by 3 numbers instead of 2 numbers, which
is called Homogenous Coordinate system. In this system, we can represent all
the transformation equations in matrix multiplication. Any Cartesian point
PX,YX,Y can be converted to homogenous coordinates by P’ (Xh, Yh, h).
Translation
A translation moves an object to a different position on the screen. You can
translate a point in 2D by adding translation coordinate (t x, ty) to the original
coordinate X,YX,Y to get the new coordinate X′,Y′X′,Y′.

VEC/IT
CS 8092 COMPUTER GRAPHICS III YR VI SEM 2017 REGULATION

From the above figure, you can write that −


X’ = X + tx
Y’ = Y + ty
The pair (tx, ty) is called the translation vector or shift vector. The above equations
can also be represented using the column vectors.
P=[X]/[Y] p' = [X′]/[Y′]T = [tx]/[ty]
We can write it as −
P’ = P + T
Rotation
In rotation, we rotate the object at particular angle θ thetatheta from its origin.
From the following figure, we can see that the point PX,YX,Y is located at angle
φ from the horizontal X coordinate with distance r from the origin.
Let us suppose you want to rotate it at the angle θ. After rotating it to a new
location, you will get a new point P’ X′,Y′X′,Y′.

VEC/IT
CS 8092 COMPUTER GRAPHICS III YR VI SEM 2017 REGULATION

Using standard trigonometric the original coordinate of point PX,YX,Y can be


represented as −
X=rcosϕ......(1)X=rcosϕ......(1)
Y=rsinϕ......(2)Y=rsinϕ......(2)
Same way we can represent the point P’ X′,Y′X′,Y′ as −
x′=rcos(ϕ+θ)=rcosϕcosθ−rsinϕsinθ.......(3)x′=rcos(ϕ+θ)=rcosϕcosθ−rsinϕsinθ.......
(3)
y′=rsin(ϕ+θ)=rcosϕsinθ+rsinϕcosθ.......(4)y′=rsin(ϕ+θ)=rcosϕsinθ+rsinϕcosθ.......
(4)
Substituting equation 11 & 22 in 33 & 44 respectively, we will get
x′=xcosθ−ysinθx′=xcosθ−ysinθ
y′=xsinθ+ycosθy′=xsinθ+ycosθ

VEC/IT
CS 8092 COMPUTER GRAPHICS III YR VI SEM 2017 REGULATION

VEC/IT
CS 8092 COMPUTER GRAPHICS III YR VI SEM 2017 REGULATION

Scaling
To change the size of an object, scaling transformation is used. In the scaling
process, you either expand or compress the dimensions of the object. Scaling can
be achieved by multiplying the original coordinates of the object with the scaling
factor to get the desired result.
Let us assume that the original coordinates are X,YX,Y, the scaling factors are
(SX, SY), and the produced coordinates are X′,Y′X′,Y′. This can be mathematically
represented as shown below −
X' = X . SX and Y' = Y . SY
The scaling factor SX, SY scales the object in X and Y direction respectively. The
above equations can also be represented in matrix form as below −

OR
P’ = P . S
Where S is the scaling matrix. The scaling process is shown in the following
figure.

VEC/IT
CS 8092 COMPUTER GRAPHICS III YR VI SEM 2017 REGULATION

If we provide values less than 1 to the scaling factor S, then we can reduce the
size of the object. If we provide values greater than 1, then we can increase the
size of the object.
Reflection
Reflection is the mirror image of original object. In other words, we can say that it
is a rotation operation with 180°. In reflection transformation, the size of the
object does not change.
The following figures show reflections with respect to X and Y axes, and about
the origin respectively.

VEC/IT
CS 8092 COMPUTER GRAPHICS III YR VI SEM 2017 REGULATION

Shear
A transformation that slants the shape of an object is called the shear
transformation. There are two shear transformations X-Shear and Y-Shear. One
shifts X coordinates values and other shifts Y coordinate values. However; in both
the cases only one coordinate changes its coordinates and other preserves its
values. Shearing is also termed as Skewing.

X-Shear

VEC/IT
CS 8092 COMPUTER GRAPHICS III YR VI SEM 2017 REGULATION

The X-Shear preserves the Y coordinate and changes are made to X coordinates,
which causes the vertical lines to tilt right or left as shown in below figure.

The transformation matrix for X-Shear can be represented as −

Y-Shear

The Y-Shear preserves the X coordinates and changes the Y coordinates which
causes the horizontal lines to transform into lines which slopes up or down as
shown in the following figure.

VEC/IT
CS 8092 COMPUTER GRAPHICS III YR VI SEM 2017 REGULATION

The Y-Shear can be represented in matrix from as −

Composite Transformation
If a transformation of the plane T1 is followed by a second plane transformation
T2, then the result itself may be represented by a single transformation T which is
the composition of T1 and T2 taken in that order. This is written as T = T1∙T2.
Composite transformation can be achieved by concatenation of transformation
matrices to obtain a combined transformation matrix.
A combined matrix −
[T][X] = [X] [T1] [T2] [T3] [T4] …. [Tn]
Where [Ti] is any combination of

VEC/IT
CS 8092 COMPUTER GRAPHICS III YR VI SEM 2017 REGULATION

 Translation
 Scaling
 Shearing
 Rotation
 Reflection
The change in the order of transformation would lead to different results, as in
general matrix multiplication is not cumulative, that is [A] . [B] ≠ [B] . [A] and
the order of multiplication. The basic purpose of composing transformations is to
gain efficiency by applying a single composed transformation to a point, rather
than applying a series of transformation, one after another.
For example, to rotate an object about an arbitrary point (X p, Yp), we have to carry
out three steps −

 Translate point (Xp, Yp) to the origin.


 Rotate it about the origin.
 Finally, translate the center of rotation back where it belonged.

Viewing Pipeline

 
Window – world coordinate area for display
Viewport – area on display device to which window is mapped
 
Window – what is viewed
Viewport – where it is displayed
 

VEC/IT
CS 8092 COMPUTER GRAPHICS III YR VI SEM 2017 REGULATION

Viewing Transformations – mapping part of a world coordinate scene to device


coordinates
aka – Window-Viewport Mapping
 

2D Viewing-Transformation Pipeline

Viewing Coordinate Reference Frame


Procedure:
1. Set up viewing-coordinate origin at some world position Po(xo,yo)
2. Set up orientation of reference frame
e.g. could set up view-up vector
can compute component of u = (ux, uy) & v = (vx,vy)
3. obtain matrix for converting world coordinates to viewing coordinates
                                                    i.     translate viewing origin to world origin
                                                   ii.     rotate to align two coordiate reference frame

Window-to-Viewport Coordinate Transformation

VEC/IT
CS 8092 COMPUTER GRAPHICS III YR VI SEM 2017 REGULATION

 Device transformations using:


o Xwl, Xwh, Ywl, Ywh
o Xvl, Xvh, Yvl, Yvh
 Use Transformation:
1.     Set up windowvl
2.     Translate window
3.     Scale to normalize
4.     Scale to viewport
5.     Translate to Viewport
 

VEC/IT
CS 8092 COMPUTER GRAPHICS III YR VI SEM 2017 REGULATION

What are the final formulas?

Xv = [(Xvh - Xvl)/(Xwh - Xwl)] (X - Xwl) + Xvl

Yv = [(Yvh - Yvl) / (Ywh - Ywl)] (Yw - Ywl) + Yvl

Window-Viewport Mapping      
             
Ywh-
Xwh Xwl Ywh Ywl Xwh-Xwl Ywl  
7.5 2.5 9 2.5 5 6.5  
             
Xvh Xvl Yvh Yvl Xvh-Xvl Yvh-Yvl  
900 400 700 400 500 300  
             
Y-
No. x y X-Viewport Viewport    
1 1.0 4.0 250.000 469.231   
2 4.0 10.0 550.000 746.154   
3 8.0 8.0 950.000 653.846   
4 8.0 4.0 950.000 469.231   
1 1.0 4.0 250.000 469.231   
             
Window
Coordinates Viewport Coordinates    
x y   x y    
Window/Viewpo
2.5 2.5  400 400rt

VEC/IT
CS 8092 COMPUTER GRAPHICS III YR VI SEM 2017 REGULATION

2.5 9  400 700Outline  


7.5 9  900 700   
7.5 2.5  900 400   
2.5 2.5  400 400   
             
1.0 4.0  250 469Data Points
4.0 10.0  550 746   
8.0 8.0  950 654   
8.0 4.0  950 469   
1.0 4.0  250 469   
 

          

Clipping Operations
Clipping Algorithms -> procedure that identifies portions of picture either inside
or outside specified regions

Clip Window -> region against which an object is clipped

VEC/IT
CS 8092 COMPUTER GRAPHICS III YR VI SEM 2017 REGULATION

Applications of Clipping:

 Extracting part of defined scene for viewing


 Identifying visible surfaces in 3D Views
 Anti-aliasing line segments or object boundaries
 Creating objects using solid modelling procedures
 Drawing and painting operations
 

·       Clipping operations eliminate everything outside window

·       Two Approaches:

1. Clip word coordinates -> only contents of window interior mapped to device
coordinates
2. Map all world coordinates to device coordinates -> then clip against
viewport boundaries
·       Raster systems -> clipping part of scan conversion

Point Clipping
Rectangular clipping window…

Point P(x,y) saved for display IF

Xwl <= x <= Xwh

Ywl <= y <= Ywh

Line Clipping
 

VEC/IT
CS 8092 COMPUTER GRAPHICS III YR VI SEM 2017 REGULATION

Line Clipping Procedure:

1.     Test line segment to determine if it resides completely inside clipping window

2.     Test to see if it is completely outside

3.     It we are unable to tell, perform intersection tests

Inside-Outside Test
·       Check both endpoints -> if both P1 and P2 are within boundaries – line is
saved

·       If both endpoints outside – drop line segment

·       Remaining may require calculation of multiple intersections

Sutherland-Cohen Line Clipping


·       Every line endpoint is assigned a 4 bit Region code.

·       The bit is set depending on the location of the endpoint with respect to that
window component as shown below:

Bit 1: Left of window  


Bit 2: Right of window

VEC/IT
CS 8092 COMPUTER GRAPHICS III YR VI SEM 2017 REGULATION

Bit 3: Below window


Bit 4: Above window

Example:

P1 -> 0001, P 2 -> 1000

P3 -> 0001, P4 -> 0100

P5 -> 0000, P6 -> 0010

P7 -> 0001, P8 -> 0001


Determine the bit code by testing the endpoints with window as follows:
o      If x is less than Xwl then set bit 1
o      If x is greater than Xwh then set bit 2
o      If y is less than Ywl then set bit 3
o      If y is greater than Ywh then set bit 4
 
§       Possible Algorithm
o      Calculate difference between endpoint coordinates and clipping
boundary
o      Use sign bit of difference to set region code
x – Xwl         bit 1
Xwh – x        bit 2
y – Ywl         bit 3
Ywh – y        bit 4
o      Determine Visibility
·       Any line whose endpoints have region codes of 0000 is visible
o      Any lines that have 1 at same bit position for each endpoint are outside

VEC/IT
CS 8092 COMPUTER GRAPHICS III YR VI SEM 2017 REGULATION

·       Use AND operation as test


1001
0101
0001
·       If result is not zero – line is completely outside
o      All other lines must be tested against boundaries -> one at a time
 

§       Procedure:

o      Begin with P1 and lower boundary and check against left, right, and
bottom boundaries
o      Find intersection point P1’ with bottom boundary and replace P1 with
P1’
o      Do same with P2 but we find two intersections p2’ and p2’’
o      P2’ is above window but P2’’ is on window boundary
 

VEC/IT
CS 8092 COMPUTER GRAPHICS III YR VI SEM 2017 REGULATION

Computing line intersections


§       Need to fined (x,y) position alone a line segment

§       We know …

y = mx + b

m = ( y2 – y1) / ( x2 – x1 )

 
b = y1 – m x1

so…
y = m x + y1 – m x1

y  = y1 + m ( x – x1)

§       substitute for x  => Xwh or Xwl to calculate y coordinate of intersection


 

also

x = x1 + ( y – y1) / m

 
§       substitute for y  => Ywh or Ywl to calculate x coordinate of intersection
 

§       Example:

P1 : (1, 4 ) and P2 : ( 4, 10 )


Xwh Xwl Ywh Ywl
7.5 2.5 9 2.5
Answer:

VEC/IT
CS 8092 COMPUTER GRAPHICS III YR VI SEM 2017 REGULATION

Intersection with Ywh == x = 3.5


Intersection with Xwl == y = 7
So…
P1’ = ( Xwl, 7) = ( 2.5, 7 )
P2’ = ( 3.5, Ywh ) = ( 3.5, 9 )
 
§       Splitting Concave Polygons
§       Identify concave polygons by calculating cross products of successive
edge vectors
§       If z – value of some cross products is positive while others are negative,
concave polygon exits.
(assume no three successive vertices are collinear => gives 0 crossproduct)
 
Example:
z-component processing
 

 
(E1 X E2) > 0
(E2 X E3) > 0
(E3 X E4) < 0
(E4 X E5) > 0
(E5 X E6) > 0
(E6 X E7) > 0
 
§       Cross product:
V1 X V2 = u |V1||V2| sinq
VEC/IT
CS 8092 COMPUTER GRAPHICS III YR VI SEM 2017 REGULATION

where u is unit vector perpendicular to V1 and V2


 
V1 X V2 = ( V1y V2z – V1z V2y, V1z V2x – V1x V2z, V1x V2y – V1y V2z )
 

 
E1 = (1, 0, 0)
E2 = (1, 1, 0)
E3 = (1, -1, 0)
E4 = (0, 2, 0)
E5 = (-3, 0, 0)
E6 = (1, -2, 0)
 
(E1 X E2) = (0, 0, 1)
(E2 X E3) = (0, 0, -2)
(E3 X E4) = (0, 0, 2)
(E4 X E5) = (0, 0, 6)
(E5 X E6) = (0, 0, 6)
(E6 X E7) = (0, 0, 2)
 
Since E2 X E3 z-value < 0 – must split polygon along line E2
Must determine intersection of line with edge E4
 
Use slope-intercept form of line:
y = max + ba            y = mbx + bb
max + ba  = mbx + bb
x = ( bb  - ba ) / (ma - mb )
and
x = ( y - ba ) / ma      x = ( y – bb ) / mb
( y - ba ) / ma = ( y – bb ) / mb

VEC/IT
CS 8092 COMPUTER GRAPHICS III YR VI SEM 2017 REGULATION

y = (ma bb – mb ba ) / (ma - mb )


 
§       Alternative method for splitting concave polygons
1.     Rotational Method:
2.     Proceed counterclockwise
3.     Translate each polygon vertex Vk in sequence to origin
4.     Rotate cloackwise so next vertex is on x axis
5.     If next vector Vk+2 is below axis, polygon is concave
 
e.g.

§       can fined, since (x, y=0), substitute into


x = x1 + ( y – y1 ) / m
so…
x = x1 - y1  / m
 

Polygon Clipping
§       Polygon boundary processed with a line clipper appears as series of
unconnected line segments

e.g.

VEC/IT
CS 8092 COMPUTER GRAPHICS III YR VI SEM 2017 REGULATION

Need to display a bounded area after clipping


 

Need algorithm that will generate one or more closed areas that can be scan
converted

Sutherland  Hodgeman Polygon Clipping


§       Clip polygon against all four edges

§       Each successive clipped polygon sent to next edge

4 Vertex Processing Cases

1. 1st outside, 2nd inside
intersection points and inside points added to output vertex list

2. 1st inside, 2nd inside
2nd vertex added to output vertex list

3. 1st inside, 2nd outside
intersection added to output vertex list

4. 1st outside, 2nd outside
nothing added to output vertex list

VEC/IT
CS 8092 COMPUTER GRAPHICS III YR VI SEM 2017 REGULATION

e.g.

VEC/IT

You might also like