Subject:Computer Graphics

Subject:Computer Graphics

Study Material

Unit:II

Semester:V

Subject:Computer Graphics

Class:III B.C.A ‘A’ & ‘B’

Syllabus

2D – Transformations

Transformation principles-concatenation-Matrix representation.

Clipping and Windowing

A line Clipping algorithm –Mid –point subdivision- clipping other graphic entities – polygon clipping – viewing Transformations – The windowing Transformation.

2D- Transformations

Basic Transformations

Translation :

A translation is applied to an object by reposition it along a straight – line path from one coordinate location another. We translate a two- dimensional point by adding translation distances, tx & ty , to the original coordinate position (x,y) to move the point to a new position (x’,y’)

X’ = x + tx

Y’ = y + ty

The translation distance pair(tx,ty) is called a translation vector or shift vector.

P= x1P’= x2 T = tx

Y1 y2 ty

P’= P + T.

Rotation :

A 2D rotation is applied to an object by repositiong it along a circular path in the xy plane. To generate 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.

X’= rcos (+) = rcoscos - rsinsin

Y’= rsin (+) = rcossin - rsincos

The original coordinates of the polar coordinates are x =rcos, y= rsin.

X’=xcos –ysin

Y’= xsin + ycos

Rotation equation matrix format is P’=R.P

Where the matrix rotation is

Cos –sin

R= sin cos

Scaling:

A scaling transformation alters the size of an object. This operation can be carried out for polygons multiplying the coordinate values (x,y) of each vertex by scaling factors sx,sy to produce transformed coordinates (x’,y’)

X’= x.sx y’= y.sy

Matrix format is

X’ = sx 0 P’= S.P

Y’ 0 sy

Positive numeric values can be assigned to the scaling factors sx,sy. Values less than 1 reduce the size of the objects , Values less than 1 produce an enlargement, Specifying value 1 for both sx and sy leaves the size of objects unchanged. When sx,sy are assigned the same value , a uniform scaling is produced. Unequal values for sx,sy result in a differential scaling.

We can control the location of a scaled object by choosing a position , called the fixed point, that is to remain unchanged after the scaling transformation.

(xf,yx) fixed point.

X’=x.sx +xf(1-sx)

Y’= y.sy +yf(1-sy)

Other transformations are shearing and Reflection.

Matrix Representation

Translation

X’ 1 0 tx x

Y’ = 0 1 ty y

1 0 0 1 1

P’=T(tx,ty).P

Rotation

X’cos-sin0 x

Y’=sincos0y

10011

P’=R().P

Scaling

X’sx 0 0 x

Y’=0 sy 0 y

10 0 1 1

P’=S(sx,sy).P

Composite Transformations

We can set up a matrix for any sequence of transformations as a composite transformation matrix by calculating the matrix product of individual transformation.

Transaltions

P’ = T(tx2,ty2). { T(tx1,ty1).P}

{T(tx2,ty2).T(tx1,ty1)}.p

1 0 tx2 1 0 tx1 1 0 tx1 + tx2

0 1 ty2=0 1 ty1 0 1ty1 + ty2

0 0 10 0 1 0 0 1

(or)

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

Rotations

P’= R(2).{R(1).P}

{R(2).R(1)}.P

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

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

Scaling

Sx2 0 0 sx1 0 0sx1.sx2 0 0

0 sy2 0 . 0 sy1 0=0 sy1.sy2 0

0 0 1 0 0 1 0 0 1

Clipping and windowing

Windowing Concepts

A rectangular area specified in world coordinates is called a window. The rectangular area on the display device to which a window is mapped is called a view port. This mapping is called a viewport transformation, windowing transformations, or normalized transformation.

Commands to set window and Viewport

Set_window(xw_min,xw_max,yw_min,ywmax)

Set_Viewport(xv_min,xv_max,yv_min,yvmax)

Clipping Algorithms

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 clipping. The region against which an object is to clipped is called clip window.

Xwmin  x  xwmax , ywmin  y  ywmax

Line clipping

P4

Ywmax ywmax

P3

P7 Pp55

P7

P8p8

Ywminp9 ywminp9

Xwmin xwmax xwmin xwmax

Before ClippingAfter Clipping

We test a line for clipping by determining whether the end points are inside or outside the window. A line with both end points inside the window boundaries , such as , line form p5 to p6 is saved. A line with one endpoint outside(p9) and one end point (p10) inside is clipped of the boundary intersection point(p’9). Lines that have both endpoints beyond the boundaries are either totally outside the window or cross two window boundaries. The line from p3 to p4 is completely clipped, But the line from p7 to p8 extends beyond the window boundaries on both sides and so the section of the line from p’7 to p’8 is saved.

The line clipping is based on a coding scheme developed by Cohen Sutherland. Every line endpoint in a picture is assigned a four digit binary code, called a region code. That identifies the coordinate region of the point. Regions are set up in the reference to the window boundaries

1001 1000 1010

0001 00 0010

0101 0100 0110

Each bit position in the region code is used to indicate one of the four relative coordinate positions of the point with respect to the window: to left ,right ,top ,bottom. Numbering the bit positions in the regions can be correlated with bit position as

bit 1- left bit 2 –right bit 3 –below bit 4- above.

A value of 1 in any bit position indicates that the point in the relative position : otherwise the bit position is set to 0. If a point is with in the window , the region code is 0000. A point below to the left of window has a region code of 0101.

Bit values in the region code a re determined by comaring endpoint coordinate values (x,y) to the window boundaries. Bit 1 is set to 1 if x<Xwmin. The other 3 bit values can be determined using similar comparisons. for the languages in which bit manipulation is possible, region-code bit values can be determined these steps:

  1. Calculate difference between end points coordinate and window boundaries.
  2. Use the resultant sign bit of each code. Bit 1 is the sign bit of Xwmin; bit 2 is the sign bit of Xwmax –X; bit 3 is the sign bit of Y-Ywmin; and bit 4 is the sign bit of Ywmax-Y.

Any lines that are completely contained within the window boundaries have a region code of 0000 for both endpoints and we trivially accept these lines. Any line that have a 1 in the same bit position in the same bit position in the region codes for each endpoints are completely outside the window, and we trivially reject these lines.

Lines that can not be identified as completely inside or outside a window by these tests are checked for intersection with boundaries. We can process the lines by comparing window boundary to determine how much of the line can be discarded. Then the remaining part of the line is checked against other boundaries, and we continue until either the line is totally discarded or a section is found inside the window.

Intersection points with the window boundary are calculated using the equation parameters. For a line with endpoint coordinates (x1,y1) and (x2,y2), the y coordinate of the intersection point with vertical window boundary can be obtained with the calculation.

Y= y1 +m(x-x1)

Where the x value is set either to XWmin or XWmax and the slope m is calculated as m= (y2-y1)/(x2-x1). Similarly if we are looking for the intersection with a horizontal boundary , the coordinate can be calculated as

y-y1

X= x1 +------with y set either to YWmin or Ywmax.

M

Cohen –Sutherland line clipping algorithm.

Var

Xw_min,xw_max,yw_min,yw_max : real;

Procedure clip_a_line(x1,y1,x2,y2 : real);

Type

Boundaries = (left,right,bottom,top);

Code = array [boundaries] of boolean;

Var

Code1,code2 :code;

Done, display :Boolean;

M :real;

Procedure encode(x,y:real,var c:code);

Begin

If x<xw_min then c[left]:=true

Elsec[left]:= false;

If x>xw_max then c[right]:=true

Elsec[right]:= false;

If y<yw_min then c[bottom]:=true

Elsec[bottom]:= false;

If y>yw_max then c[top]:=true

Elsec[top]:= false;

End; {encode}

Function accept(c1,c2:code):Boolean;

Var k:boundaries,

Begin

{ if either point has true in its code, a trivial accept is not possible}

accept :=true

for k :=left to top do

if c1[k] or c2[k] then accept :=false

end; {accept}

function reject(c1,c2 :code):Boolean;

var k :boundaries;

begin

{ if endpoints have matching trued lines can be rejected }

reject :=false;

for k:= left to top do

if c1[k] and c2[k] then reject :=true

end{ reject}

procedure swap_if_needed (var x1,y1,x2,t2 :real;var c1,c2 :code)

begin

{ insures that x1,y1 is a point outside of window and c1 contains its code}

end {swap_if _needed}

begin { clip_a _line }

done :=false;

diaplay := false;

while not done do begin

encode(x1,y1,code1);

encode(x2,y2,code2);

if accept(code1,code2) then begin

done =true;

display= true;

end {if accept}

else

if reject(code1,code2) then done:=true

else

begin{find intersection}

{make sure that x1,y1 outside the window}

wap_if_needed(x1,y1,x2,y2,code1,code2);

m:=(y2-y1)/(x2-x1)

if code1[left] then begin

y1:= y1+(xw_min_x1)*m;

x1:=xw_min;

end{crosses left}

else

if code1[right] then begin

y1:= y1+(xw_max_x1)*m;

x1:=xw_max;

end{crosses right}

else

if code1[bottom] then begin

x1:=x1+(yw_min_y1)*m;

y1:=yw_min;

end{crosses bottom }

else

if code1[top] then begin

x1:=x1+(yw_max_y1)*m;

y1:=yw_max;

end{crosses top }

end {else find intersection}

end; {while not done}

if display then { draw x1,y1 to x2,y2 }

end;{clip_a_line}

Midpoint subdivision

A technique for locating window intersections without the line equation calculations is a binary search procedure, called mid point subdivision.

For any line with endpoints (x1,y1) &(x2,y2) the midpoint of the line is calculates as

X1+x2y1+y2

Xm= ------Ym= ------

22

Other clippings

Text clipping, curve clipping, Polygon clipping.

Window –to –viewport transformation.

Once all points, lines ,polygons and text have been clipped ,they we mapped onto the viewport area for display. This transformations is carried out so that relative proportions are maintained.

(xw,yw) window is mapped to (xv,yv) viewport

xw-xwminXv-Xvmin

------=------

xwmax-xwminxvmax-xvmin

and

Yw-Ywminyv-yvmin

------=------

Ywmax-ywminyvmax-yvmin

Rewrite the above equation we get

Xvmax –xvmin

Xv =------(xw-xwmin)+xvmin

Xwmax-xwmin

yvmax –yvmin

yv =------(yw-ywmin)+yvmin

ywmax-ywmin

these window to viewport transformation calculations can be written more compactly as

xv =sx(xw-xwmin)+xvmin

yv=sy(yw-ywmin)+yvmin

which include both scaling and translation factors. The rations represented by sx and sy scale objects according to the relative sizes of window view port. These ratios must be equal if objects are to maintain the same proportions when they are mapped into view port. When window view port are the same size(sx=sy=1),those is no change in transformed objects. Values xvmin and yvmin provide the translation factors for moving objects into the view port area.

------*****************------

II unit questions
  1. what is 2D?
  2. what are the 2D basic transformations?
  3. what is translation?
  4. what is rotation?
  5. what is scaling?
  6. what are the other basic transformation?
  7. What is concatenation of transformation? And its uses?
  8. give matrix representations for transformations.
  9. what is clipping?
  10. what is line clipping algorithm? Or cohen Sutherland with procedure.
  11. what is midpoint subdivision clipping algorithm?
  12. what is Text and curve clipping algorithm?
  13. what is polygon clipping algorithm?
  14. what is viewport?
  15. what is window?
  16. what is viewing transformation and windowing transformation?
  17. explain window to view port transformation.

Book ref:

William m.Newmen Robert .F.sproull

“principles of interactive computer graphics”

______************************______

1