Sei sulla pagina 1di 54

CS 6504- Computer

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

Key terms Where sx,sy- Scaling factor


xf,yf – Pivot coordinate values
Scaling factor sx and sy
x,y – given original coordinate values
Fixed point ( pivot point) – (xf,yf) x’,y’- New coordinate values
x’ = x . sx + xf ( 1 – sx)
Y’ = y . sy + yf (1 – sy)
Scaling about origin
•Matrix representation
P’=S.P
P’= P= S=

= .
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)

T(tx2,ty2).T(tx1,ty1)= T(tx1 + tx2, ty1 + ty2)


Composite transformation(contend.,)
Rotation
Two successive rotations applied to a point P produce the transformed
position
P’ = R(Ɵ2).{R(Ɵ1).P} = {R(Ɵ2).R(Ɵ1)}.P

Two successive rotations are additive

R(Ɵ2).R(Ɵ1) = R(Ɵ1 + Ɵ2)

P’= R(Ɵ1 + Ɵ2) . P


Composite transformation(contend.,)
•Scaling
Two successive scaling produce the following transformation matrix

.=

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

Reflection about y axis


Reflection about the line x=0
•Reflection relative to the coordinate origin

Reflection about the diagonal line (y=x)

Reflection about the diagonal line (y=-x)


Shearing
•X- direction shear
Relative to the x axis
 Equation x’=x+shx . Y and y’=y
 Transformation matrix

Relative to the reference line


 Equation x’=x + shx(y-yref) and y’=y
 Transformation matrix
Shearing(cnted.,)
•Y- direction shear
Relative to the y axis
 Equation x’=x and y’=shy . X + y
 Transformation matrix

Relative to the reference line


 Equation x’=x and y’= shy(x-xref)+y
 Transformation matrix
Two Dimensional viewing
Window- A world – coordinate area selected for display.
Viewport- An area on a display device which a window is mapped.
Viewing transformation- The mapping of a part of a world- coordinate
scene to device coordinate (Window to view port transformation).
Two – dimensional viewing-
transformation pipeline
Construct World Map viewing
coordinate scene Convert World- coordinate to Map normalized
MC WC NVC DC
using Modeling coordinate to viewing VC normalized viewing viewport to device
coordinate coordinate coordinates using coordinates
transformations window- viewport
specifications
Viewports are defined within the unit square( Normalized coordinates).
Viewing transformation steps
1. Construct the scene in world coordinate using the output primitives
and attributes
2. Establish the viewing reference frame
i. Transform world coordinate to viewing coordinate
3. Define a viewport in normalized coordinate and map the viewing
coordinate to normalized coordinate.
4. All parts of the picture that lies outside the viewport are clipped
i. Viewport content are transferred to device coordinate
ii. By changing the position and size of the viewport, object are viewed at
different position and size.
Viewing Effects
1. Zooming effects
Successively mapping different- sized windows on a fixed- sized viewports
2. Panning effects
Moving a fixed-sized window across the various objects in a scene
3. Device independent
Viewports are typically defined within the unit square (normalized
coordinates)
Viewing coordinate reference frame
Steps
1. Setup viewing coordinate systems
i. Select the viewing coordinate origin at some world position p0=(x0,y0)
ii. Establish the orientation of reference frame
Mwc,vc= R .T
iii. Specify the view up vector V: defines the viewing yv direction
 Compute component of u = (ux,uy) and v=(vx,vy)

2. Obtain matrix for converting world coordinate to viewing coordinate


i. Translate viewing origin to world origin
ii. Rotate to align two coordinate reference frame
Window to viewport coordinate
transformation
•Transformation describes the mapping of a (rectangular) window in
one coordinate system into another ( rectangular) window in another
coordinate system.
Illustration
Point at position (xw,yw) in the window is mapped into position (xv,yv)
in the associated viewport.
Equation that maintains the relative position.
•Determining the viewport coordinate xv and yv from the equation
Xv=xvmin + ( xw – xwmin)sx
Yv= yvmin + (yw – ywmin)sy
Where the scaling factors are,
Sx=

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

Setting up the element of a window-to-viewport mapping matrix


setiewRepresentation(ws,viewIndex,viewMatrix,iewMappingMatrix,xclipmin,xclipmax,yclipmin,yclip
max,clipxy)
 Ws the output device (work station)
 viewIndex an integer identifier for this particular window view port pair

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.

Potrebbero piacerti anche