Professional Documents
Culture Documents
CS 6504-Computer Graphics
CS 6504-Computer Graphics
CS 6504-Computer Graphics
Graphics
Prepared by,
Mr.R.Karthiban M.E.,
Assistant Professor,
Computer Science and Engineering,
Sri Shakthi Institute of Engineering and Technology,
Coimbatore-641 062.
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; widow-to-viewport coordinate transformation, Two dimensional
viewing functions; clipping operations – point, line, and polygon clipping
algorithms.
Transformation
Transformation that alter the coordinate description of object
Changes in orientation, size and shape
Three basic transformations
1. Translation
2. Rotation
1. About origin
i. Clockwise
ii. Counter clockwise
2. About pivot
i. Clockwise
ii. Counter clockwise
3. Scaling
1. About origin
2. About pivot
Translation- Transformation
Transformation that move an object from one position to another
position along straight line path
x’=x+tx
y’=y+ty
Where tx and ty- Translation factors
x,y – given original coordinate values
x’,y’- New coordinate values
Translation- Transformation
•Matrix representation
P’=P + T
P’= P= T=
= +
Rotation- Transformation
Transformation that reposition an object along a
circular path in the xy plane
Key terms needed for rotation
Rotation angle Ɵ
Positive angle- Counter clock wise
Negative angle- Clock wise
Position (xr,yr) of the rotation point(or pivot point)
Rotation about origin
Key terms
Rotation angle Ɵ
Pivot as origin
The transformation equation Positive angle(Counter clock wise rotation)
x’=x cos Ɵ – y sin Ɵ
y’ = x sin Ɵ + y cos Ɵ
The transformation equation Negative angle(Clock wise rotation)
x’=x cos Ɵ + y sin Ɵ
Where Ɵ rotating angle
y’ = - x sin Ɵ + y cos Ɵ x,y – given original coordinate values
x’,y’- New coordinate values
Rotation about origin
• Matrix representation
P’=R.P
Positive angle (Counter Clock wise rotation)
P’= P= R=
= .
Negative angle (Clock wise rotation)
P’= P= R=
= .
Rotation about Pivot
Key terms
Rotation angle Ɵ
Pivot as (xr,yr)
The transformation equation Positive angle(Counter clock wise rotation)
x’=xr + (x-xr) cos Ɵ – (y-yr) sin Ɵ
y’ =yr + (x-xr) sin Ɵ + (y-yr) cos Ɵ
The transformation equation Negative angle(Clock wise rotation)
x’=xr + (x-xr) cos Ɵ + (y-yr) sin Ɵ
y’ =yr - (x-xr) sin Ɵ + (y-yr) cos Ɵ Where Ɵ rotating angle
xr,yr – Pivot coordinate values
x,y – given original coordinate values
x’,y’- New coordinate values
Scaling- Transformation
Transformation that alters the size of an object
Changes in the size
Key terms needed for scaling
sx, sy – Scaling factor
< 1 – Size reduced
> 1 – Size enlarged
sx= sy – Uniform scaling
sx != sy- Differential scaling
Position (x ,y ) of the fixed point( Pivot coordinate value)
Scaling Transformation equation
Key terms
Scaling factor sx and sy
Fixed point ( pivot Point) - Origin
x’= x . sx
y’= y . sy
= .
Example
Perform the following transformations
Triangle with the coordinate A(50,50), B(100,50) and C(75,75)
1. Translation distance of (50,50)
2. Rotating angle 600
3. Scaling twice
Answer 1 = A’= (100,100) B’=(150,100) C’=(125,125)
2 = A’=(-27.75,-102.45) B’= ? C’=?
3 = A’=(100,100) B’ = (200,100) C’= (150,150)
Home work
1. Rotate a triangle [ (4,6),(2,2),(6,2)] about the vertex (4,6) by 600 and find
the new vertices.
Answer: A’=(4,6) B’=(6.5,2.26) C’=(8.5,5.7)
2. A polygon has four vertices located at A(20,10) B(60,10) C(60,30)
D(20,30). Calculate the vertices after applying a transformation matrix
to thrice the size of polygon with point A located on the same place.
Answer: A’=(20,10) B’=(140,130) c’=(140,70) D’=(20,70)
3. Rotate a triangle [ (4,6),(2,2),(6,2)] about the vertex (4,6) by CW 600 and
find the new vertices.
Answer: A’ =(4,6) B’=? C’=?
Matrix representation
General matrix form to represent all the basic transformation ( translation,
Rotation, Scaling) in a single equation.
P’=M1.P+M2
P and P’- Column vector
Matrix M1
A 2 by 2 array containing multiplicative factor
Identify matrix for translation
Matrix M2
A two – element column matrix containing translational terms
Translation terms associated with the pivot point or scaling fixed point for rotation and scaling
Homogeneous coordinates
•To represent all geometric transformation equations as matrix
multiplication
Represent each Cartesian coordinate position (x,y) with the homogeneous
coordinate triple (xh,yh,h)
x= xh/h , y= yh/h
Translation
P’= T . P
=.
•Rotation
Counter Clock Wise
P’= R . P
=.
Clock Wise
=.
Scaling
P’ = S . P
=.
Composite transformation
•Forming products of transformation matrices is referred as a
concatenation or composition of matrices.
Translation
Two successive translation vector (tx1,ty1) and (tx2,ty2) are applied to a coordinate
position p, the final transformed location p’ is calculated as,
P = T(tx2,ty2).{T(tx1,ty1).P} = {T(tx2,ty2).T(tx1,ty1)}.P
.=
(or)
.=
Or
S(sx2,sy2).S(sx1,sy1)=S(sx1.sx2,sy1.sy2)
Successive scaling are multiplicative
Triple the size twice in succession , final size nine times that of the original.
General pivot- point rotation
Sequence of operations
Translate => Rotate => translate
Steps:
1. Translate the object so that the pivot- point is moved to the coordinate
origin
2. Rotate the object about the coordinate origin
3. Translate the object so that the pivot - point is returned to its original
position.
General pivot- point rotation(cond.,)
•Composite transformation matrix
.
T(xr,yr).R().T(-xr,-yr)=R(xr,yr, )
General fixed point scaling
Sequence of operations
Translate => Scale => translate
Steps:
1. Translate the object so that the pivot- point is moved to the coordinate
origin
2. Scale the object about the coordinate origin
3. Translate the object so that the pivot - point is returned to its original
position.
General fixed point scaling(cond.,)
•Composite transformation matrix
.
T(xf,yf).S().T(-xf,-yf)=R(xf,yf, sx,sy)
Other Transformations
Additional two transformations are,
1. Reflection
2. Shearing
Reflection
A reflection is a transformation that produces mirror image of an object.
Generated relative to an axis of reflection by rotating the object 1800 about
the reflection axis.
Shearing
Transformation that distorts the shape of an object.
Reflection
•Reflection about x axis
Reflection about the line y=0
Sy=
Window to viewport coordinate
transformation(cnted.,)
Transformation steps
1. Perform a scaling transformation using fixed point position of
(xwmin,ywmin) that scale the window area to the size of viewport.
2. Translate the scaled window area to the position of the viewport.
Two dimensional viewing functions
Definition about a viewing reference system
evaluateViewOrientationMatrix(x0,y0,xv,yv,error,viewMatrix)
X0 and y0 coordinates of the viewing origin
Xv and yv world coordinate positions for the view up vector
Storing combinations of viewing and window-viewport mapping for various work stations
in viewing table
evaluateViewMappingMatrix(xwmin,xwmax,ywmin,ywmax,xvmin,xvmax,yvmin,yvmax,error,viewMa
ppingMatrix)
Two dimensional viewing
functions(conted.,)
Selection of a particular set of options from the viewing table
setViewIndex(viewIndex)
Selection of a workstation window – viewport pair
setWorkstationWindow(ws,xwsVportmin,xwsWindmax,ywsWindmin,ywsWin
dmax)
setWindowstationViewport(ws,xwsVPortmin,xwsVportmax,ywsVportmin,yws
Vportmax)
Ws- Workstation number.
Clipping operations
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.
The region against which an object is to clipped is called a clip window.
Types of clipping
1. Point clipping
2. Line clipping(straight- line segments)
3. Area clipping(polygon)
4. Curve clipping
5. Text clipping
1. Point clipping
Assuming that the clip window is a rectangle in standard position.
Save a point p = (x,y) for display if the following inequalities are
satisfied,
Xwmin<=x<=xwmax
Ywmin<=y<=ywmax
Wmin, wmax either world coordinate or view coordinate
If any one of these four inequalities not satisfied the point is clipped.
2. Line Clipping
Completely inside – Accept - no need to clip
Completely outside – Reject – Ignore the line
Completely inside or completely outside- perform “inside-outside”
test – calculate the intersection point.
2.1 Cohen-Sutherland Line Clipping
Region codes or outcodes – Identify the location of the point relative to
the boundaries of clipping rectangle.
Above Below Right Lef
Procedure:
1. Perform logical AND between two end points , if resultant equal to 0 the line
inside. Or otherwise discard the line
1. Perform logical OR on the two end points if the resultant equal to 0 no need to clip the
line otherwise find intersection points on the boundary.
2. Left : xl,y=m(xl-x1)+ y1
Right : xr,y=m(xr-x1) + y1
Top : yt,x=x1+(1/m)(yt-y1)
Bottom : yb,x=x1+(1/m)(yb-y1)
2.2 Liang Barsky Line Clipping
Algorithm
Advantages
Intersection calculations are reduced
Updating of parameters requires only one division
2.3 Nicholl-Lee-Nicholl (NLN) line
clipping algorithm
The Nicholl-Lee-Nicholl (NLN) line clipping algorithm creates more
regions around the clipping window to avoid multiple line-intersection
calculations, thus using fewer comparisons and divisions.
The tradeoff is that Cohen-Sutherland and Liang- Barsky can be easily
extended to three dimensions, but NLN cannot.
We assume most of the computational overhead is associated with
finding intersections, so it follows that irrelevant intersection
computations should be avoided.
NLN - Initial Testing
Initially, a line’s endpoints are assigned region codes as before.
Then the “easy” tests to determine if the line is entirely inside or
outside the clipping region are performed. Obviously we’re done (as
before) if one of the easy tests succeeds.
Otherwise NLN proceeds to set up additional clipping regions.
Endpoint Positions to Consider
One of the endpoints (called P0) of each line considered by NLN must be in
one of three positions. That is, P0 must have one of three region codes:
0000 = inside the clipping region
0001 = immediately left of the clipping region
1001 = left and above the clipping region
If P0 isn’t in one of those regions, but Pend is, then P0 and Pend can be
swapped.
The other endpoint positions can be mapped into one of the three positions
by rotating the line and the clipping region by 90, 180, or 270 degrees.
90 or 180 or 270 CCW Rotation
• A 90, 180 or 270 counterclockwise rotation of a point (x,y) about the
origin can be easily accomplished:
• For a 90 rotation, replace (x,y) with (-y,x)
• For a 180 rotation, replace (x,y) with (-x,y)
• For a 270 rotation, replace (x,y) with (-y,-x)
• So, for NLN, rotating the clipping region and the line segment (from
P0 to Pend) requires 12 assignments and some negations. We’ll also
need to do the reverse rotation after clipping is completed.