51
Ex: 1a Date : Drawing line using Bresenham algorithm Aim: To draw line using bresenham algorithm in c. Algorithm: 1.Input two line endpoints 2.Obtain starting value for the decision parameter as P0=2!" #.If p$ %0 the ne"t point to plot is &" $ '1($ ) and p$'1=p$ '2else ne"t point to plot is &" $ '1($ '1) and  p$'1=p$ '2!2" *.+epeat step # " times. Coding: ,include%stdio.h- ,include%conio.h- ,include%math.h- ,include%graphics.h- main&) int gd=/TT(gm int "a("b(a(b int d"(d(3(4(3end(p(b initgraph&5gd(5gm(6c788tc88bgi6)  printf&69T+ T:O ;<T 9/ POI9T6) scanf&6>d>d6(5"a(5a)  printf&69T+ T:O +I?@T 9/ POI9T 6) scanf&6>d>d6(5"b(5b) d"=abs&"a!"b) d=abs&a!b)  p=2Ad!d" if&"a-"b) 3="b 4=b 3end="a B  else 3="a 4=a 3end="b B  putpi"el&3(4(b) while&3%3end)

Cs2405 Cg Full Lab Manual

Embed Size (px)

Citation preview

Page 1: Cs2405 Cg Full Lab Manual

8/14/2019 Cs2405 Cg Full Lab Manual

http://slidepdf.com/reader/full/cs2405-cg-full-lab-manual 1/51

Ex: 1a

Date :

Drawing line using Bresenham algorithm

Aim: 

To draw line using bresenham algorithm in c.

Algorithm:

1.Input two line endpoints

2.Obtain starting value for the decision parameter as

P0=2∆!∆"

#.If p$ %0 the ne"t point to plot is &"$ '1($ ) and p$'1=p$ '2∆ else ne"t point to plot is &"$ '1($ '1) and p$'1=p$ '2∆!2∆"

*.+epeat step # ∆" times.

Coding:

,include%stdio.h-,include%conio.h-

,include%math.h-

,include%graphics.h-main&)

int gd=/TT(gmint "a("b(a(b

int d"(d(3(4(3end(p(b

initgraph&5gd(5gm(6c788tc88bgi6) printf&69T+ T:O ;<T 9/ POI9T6)

scanf&6>d>d6(5"a(5a)

 printf&69T+ T:O +I?@T 9/ POI9T 6)

scanf&6>d>d6(5"b(5b)d"=abs&"a!"b)

d=abs&a!b)

 p=2Ad!d"if&"a-"b)

3="b4=b

3end="a

B

 else

3="a

4=a3end="b

B

 putpi"el&3(4(b)while&3%3end)

Page 2: Cs2405 Cg Full Lab Manual

8/14/2019 Cs2405 Cg Full Lab Manual

http://slidepdf.com/reader/full/cs2405-cg-full-lab-manual 2/51

3=3'1

if&p%0)

 p=p'2Ad

B

else

4=4'1

 p=p'2A&d!d")B

 putpi"el&3(4(b)

B

getch&)return&0)

B

OUTPUT:nter two left endpoints7

100200

nter two right endpoints7

#00*00

RESULT:

  The line was drawn using bresenham algorithm successfull.

Page 3: Cs2405 Cg Full Lab Manual

8/14/2019 Cs2405 Cg Full Lab Manual

http://slidepdf.com/reader/full/cs2405-cg-full-lab-manual 3/51

Ex: 1

Date :

Drawing !ir!le using Bresenham algorithm

Aim: 

To draw circle using bresenham algorithm in c.

Algorithm:

1.Input radius r and circle center&"c(c)

2.calculate the initial value of the decision parameter as

P0=CD*!r 

#.If p$%0( the ne"t point along the circle is calculated as p$'1=p$ '2"$'1'1 otherwise calculate using

 p$'1=p$ '2"$'1'1!2$'1

*.+epeat step # until "-=.

COD"#$:

,include 6stdio.h6

,include 6conio.h6,include 6math.h6

,include 6graphics.h6main&)

int gd=/TT(gmint "center(center(radius

int p("(

initgraph&5gd(5gm(6c788tc88bgi6)

"=0 printf&6nter The +adius Ealue78n6)

scanf&6>d6(5radius)=radius printf&6nter The "center and center Ealues78n6)

scanf&6>d>d6(5"center(5center)

 plotpoints&"center(center("() p=1!radius

while&"%)

if&p%0)"="'1

else

"="'1=!1

B

if&p%0) p=p'2A"'1

else

 p=p'2A&"!)'1 plotpoints&"center(center("()

Page 4: Cs2405 Cg Full Lab Manual

8/14/2019 Cs2405 Cg Full Lab Manual

http://slidepdf.com/reader/full/cs2405-cg-full-lab-manual 4/51

B

getch&)

return&0)B

int plotpoints&int "center(int center(int "(int )

 putpi"el&"center'"(center'(1)

 putpi"el&"center!"(center'(1)

 putpi"el&"center'"(center!(1) putpi"el&"center!"(center!(1)

 putpi"el&"center'(center'"(1)

 putpi"el&"center!(center'"(1)

 putpi"el&"center'(center!"(1)B

OUTPUT:nter The +adius Ealue7

100nter The "center and center Ealues7

100

200

RESULT:

  The circle was drawn using bresenham algorithm successfull.

Page 5: Cs2405 Cg Full Lab Manual

8/14/2019 Cs2405 Cg Full Lab Manual

http://slidepdf.com/reader/full/cs2405-cg-full-lab-manual 5/51

Ex: 1!

Date :

Drawing elli%se using Bresenham algorithm

Aim: To draw ellipse using bresenham algorithm in c.

Algorithm:

1.Input ellipse center and maFor( minor a"is

2.Obtain starting value for the decision parameter as p1=&bAb)!&aAaAa)A&aAa)D* and p2=&aAa)

'&2.0AbAbAa)'&bAb)D*

#.If p1%=10 the ne"t point to plot calculated using p1=p1'&2.0AbAbA")'&bAb) else ne"t point to plot is

calculated using p1=p1'&2.0AbAbA")'&bAb)!&2.0AaAaA)

*. .If p2-0 the ne"t point to plot calculated using p2=p2'&aAa)!&2.0AaAaA) else ne"t point to plot is

calculated using p2=p2'&2.0AbAbA")!&2.0AaAaA)'&aAa)

C.+epeat step # and * until 2.0AbAbA" - 2.0AaAaA.

COD"#$:

,include 6stdio.h6,include 6conio.h6

,include 6math.h6

,include 6graphics.h6main&)

int gd=/TT(gmint "center(center(r"(r

int p("((p"(p(r"1(r1(r"2(r2

initgraph&5gd(5gm(6c788tc88bgi6) printf&6nter The +adius Ealue78n6)scanf&6>d>d6(5r"(5r)

 printf&6nter The "center and center Ealues78n6)

scanf&6>d>d6(5"center(5center)r1=rAr

r"1=r"Ar"

r2=2Ar1r"2=2Ar"1

"=0

=r

 plotpoints&"center(center("() p=&r1!r"1Ar'&0.2CAr"1))

 p"=0

 p=r"2Awhile&p"%p)

"="'1 p"=p"'r2

Page 6: Cs2405 Cg Full Lab Manual

8/14/2019 Cs2405 Cg Full Lab Manual

http://slidepdf.com/reader/full/cs2405-cg-full-lab-manual 6/51

if&p-=0)

=!1

 p=p!r"2if&p%0)

 p=p'r1'p"

else p=p'r1'p"!p

 plotpoints&"center(center("()

 p=&r1A&"'0.C)A&"'0.C)'r"1A&!1)A&!1)!r"1Ar1)while&-0)

=!1

 p=p!r"2if&p%=0)

"="'1

 p"=p"'r2B

if&p-0) p=p'r"1!p

else

 p=p'r"1!p'p" plotpoints&"center(center("()

B

OUTPUT:

nter The +adius Ealue7

20

10nter The "center and center Ealues7

200

100

RESULT:

  The ellipse was drawn using bresenham algorithm successfull.

Page 7: Cs2405 Cg Full Lab Manual

8/14/2019 Cs2405 Cg Full Lab Manual

http://slidepdf.com/reader/full/cs2405-cg-full-lab-manual 7/51

Ex: &

Date :

"m%lementation o' line(!ir!le(elli%se attriutes

Aim: 

To appl the various attributes of line( circle and ellipse in c.

Algorithm:

1.Gse predefined functions line( circle and ellipse to draw line( circle and ellipse shapes with specified

 parameter values.

2.setcolor method used to set the color of the shapes.

#.setfillsle method sets the stle of the shapes.*.outte"t" method used to draw the given te"t.

O/I9?7

,include%stdio.h-

,include%conio.h-

,include%graphics.h-

,include%stdlib.h-

,include%string.h-

void main&)

int gdriver=/TT(gmode

initgraph&5gdriver(5gmode(6c788tc88bgi6)

setcolor&11)

setfillstle&11(1*)

line&#00(200(C00(200)circle&#00(#00(C0)

ellipse&C00(100(0(#H0(0(*0)

outte"t"&*00(*00(6@;;O6)

setlinestle&*((#)

fillellipse&C00(100(0(*0)getch&)

closegraph&)

B

OUTPUT:

@;;O

RESULT:The Implementation of line( circle( ellipse using attributes was verified successfull.

Page 8: Cs2405 Cg Full Lab Manual

8/14/2019 Cs2405 Cg Full Lab Manual

http://slidepdf.com/reader/full/cs2405-cg-full-lab-manual 8/51

Ex:)

Date :

&D TRA#S*OR+AT"O#

Aim: 

To appl transformation to 2!/imensional shapes in c.

Algorithm:

1.Input upper left corner and lower right corner of the rectangle.

2.To translate the rectangle appl formula "1="'t" and 1='t

#.To rotate the rectangle appla1=&"a'&&a!"a)Acos&theta)!&b!a)Asin&theta)))

 b1=&a'&&a!"a)Asin&theta)!&b!a)Acos&theta)))

*.To scale the rectangle siJe

"1="As"

1=As

C.+eflection

a1=&"a'&&a!"a)Acos&theta)!&!b!a)Asin&theta)))

 b1=&a'&&a!"a)Asin&theta)'&!b!a)Acos&theta)))

H.hearing

"1="'&A"1s)

1=

aJ1=aJ'&wA"1s)

w1=w

COD"#$:

, include%graphics.h- , include%stdio.h-

 , include%conio.h- , include%math.h- void main&)

 

  int gd=/TT(gm(i(F($(ch  float t"(t("((ang(n(temp

  float aKCLK#L(si(co(bKCLK#L(cKCLK#L

  initgraph&5gd(5gm(6e788tcpp88bgi6)  n=*

  aK0LK0L=0 aK0LK1L=0 aK1LK0L=100 aK1LK1L=0

  aK2LK0L=100 aK2LK1L=100 aK#LK0L=0 aK#LK1L=100

  aK*LK0L=0 aK*LK1L=0  while&1)

 

  cleardevice&)  goto"&1(M)

  printf&68n8tAAAAAAAA Program to perform 2!/ Transformations AAAAAAAA6)

  printf&68n8t8t8t 1. Nccept the polgon6)  printf&68n8t8t8t 2. Perform translation6)

Page 9: Cs2405 Cg Full Lab Manual

8/14/2019 Cs2405 Cg Full Lab Manual

http://slidepdf.com/reader/full/cs2405-cg-full-lab-manual 9/51

  printf&68n8t8t8t #. Perform scaling6)

  printf&68n8t8t8t *. Perform rotation6)

  printf&68n8t8t8t C. Perform reflection6)  printf&68n8t8t8t H. Perform shearing6)

  printf&68n8t8t8t . "it6)

  printf&68n8t8t8t nter our choice776)  scanf&6>d6(5ch)

  switch&ch)

    case 17

  cleardevice&)

  goto"&1(1)

  printf&68n8tnter no of points.76)  scanf&6>f6(5n)

  for&i=0i%ni'')

  printf&68n8t nter "( co!ordinates for >d776(i'1)  scanf&6>f >f6(5aKiLK0L(5aKiLK1L) B

  aKiLK0L=aK0LK0L  aKiLK1L=aK0LK1L

  cleardevice&)

  for&i=0i%ni'')  line&#20'aKiLK0L(2*0!aKiLK1L(#20'aKi'1LK0L(2*0!aKi'1LK1L)

  line&0(2*0(H#(2*0)

  line&#20(0(#20(*)

  getch&)  brea$

  case 27

  cleardevice&)  for&i=0i%ni'')

  line&#20'aKiLK0L(2*0!aKiLK1L(#20'aKi'1LK0L(2*0!aKi'1LK1L)

  line&0(2*0(H#(2*0)  line&#20(0(#20(*)

  goto"&1(1)

  printf&6nter translation vectors t" and t8n8t6)

  scanf&6>f >f6(5"(5)  cleardevice&)

 for&i=0i%ni'')

 line&#20'aKiLK0L'"(2*0!&aKiLK1L')(#20'aKi'1LK0L'"(2*0!&aKi'1LK1L')) line&0(2*0(H#(2*0)

 line&#20(0(#20(*)

 getch&)  brea$

  case #7

  cleardevice&)

  for&i=0i%ni'')

Page 10: Cs2405 Cg Full Lab Manual

8/14/2019 Cs2405 Cg Full Lab Manual

http://slidepdf.com/reader/full/cs2405-cg-full-lab-manual 10/51

  line&#20'aKiLK0L(2*0!aKiLK1L(#20'aKi'1LK0L(2*0!aKi'1LK1L)

  line&0(2*0(H#(2*0)

  line&#20(0(#20(*)  goto"&1(1)

  printf&6nter scaling vectors t" and t8n8t6)

  scanf&6>f >f6(5"(5)  if&"==0)

  "=1

  if&==0)  =1

  cleardevice&)

  for&i=0i%ni'')

  line&#20'&aKiLK0LA")(2*0!&aKiLK1LA)(#20'&aKi'1LK0LA")(2*0!&aKi'1LK1LA))  line&0(2*0(H#(2*0)

  line&#20(0(#20(*)

  getch&)

  brea$  case *7

  cleardevice&)  for&i=0i%ni'')

  line&#20'aKiLK0L(2*0!aKiLK1L(#20'aKi'1LK0L(2*0!aKi'1LK1L)

  line&0(2*0(H#(2*0)  line&#20(0(#20(*)

  goto"&1(1)

  printf&6nter the angle of rotation8n8t6)

  scanf&6>f6(5ang)  ang=angA0.01*C

  goto"&1(#)

  printf&6nter point of rotation8n8t6)  scanf&6>f >f6(5"(5)

  goto"&1(C)

  printf&61.cloc$wise 2.anticloc$wise8n8t6)  scanf&6>d6(5$)

  si=sin&ang)

  co=cos&ang)

  for&i=0i%n'1i'') cKiLK0L=aKiLK0L

  cKiLK1L=aKiLK1L

  cKiLK2L=1B  bK0LK0L=co

  bK0LK1L=si

  bK0LK2L=0  bK1LK0L=&!si)

  bK1LK1L=co

  bK1LK2L=0

  bK2LK0L=&!"Aco)'&Asi)'"

Page 11: Cs2405 Cg Full Lab Manual

8/14/2019 Cs2405 Cg Full Lab Manual

http://slidepdf.com/reader/full/cs2405-cg-full-lab-manual 11/51

  bK2LK1L=&!"Asi)!&Aco)'

  bK2LK2L=1

  if&$==1)  bK0LK1L=&!si)

 bK1LK0L=&si)

 bK2LK0L=&!"Aco)!&Asi)'" bK2LK1L=&!"Asi)'&Aco)'B

  for&i=0i%n'1i'')

    for&F=0F%#F'')

  aKiLKFL = 0

  for&$=0$%#$'')

  aKiLKFL = aKiLKFL ' cKiLK$L A bK$LKFL BB  cleardevice&)

  for&i=0i%ni'')

  line&#20'aKiLK0L(2*0!aKiLK1L(#20'aKi'1LK0L(2*0!aKi'1LK1L)

  line&0(2*0(H#(2*0)  line&#20(0(#20(*)

  getch&)  brea$

  case C7

  cleardevice&)  for&i=0i%ni'')

  line&#20'aKiLK0L(2*0!aKiLK1L(#20'aKi'1LK0L(2*0!aKi'1LK1L)

  line&0(2*0(H#(2*0)

  line&#20(0(#20(*)  goto"&1(1)

  printf&68n1.+eflection about 4!a"is6)

  printf&68n2.+eflection about 3!a"is6)  printf&68n#.+eflection about origin6)

  printf&68n*.+eflection about line ="6)

  printf&68nC.+eflection about line =!"6)  printf&68nnter our choice76)

  scanf&6>d6(5ch)

  switch&ch)

   case 17

  for&i=0i%n'1i'')

  aKiLK0L=aKiLK0LA&!1)  DA Plot the polgon AD

  cleardevice&)

  for&i=0i%ni'')  line&#20'aKiLK0L(2*0!aKiLK1L(#20'aKi'1LK0L(2*0!aKi'1LK1L)

  line&0(2*0(H#(2*0)

  line&#20(0(#20(*)

  getch&)

Page 12: Cs2405 Cg Full Lab Manual

8/14/2019 Cs2405 Cg Full Lab Manual

http://slidepdf.com/reader/full/cs2405-cg-full-lab-manual 12/51

  brea$

  case 27

  for&i=0i%n'1i'')  aKiLK1L=aKiLK1LA&!1)

  cleardevice&)

  for&i=0i%ni'')  line&#20'aKiLK0L(2*0!aKiLK1L(#20'aKi'1LK0L(2*0!aKi'1LK1L)

  line&0(2*0(H#(2*0)

  line&#20(0(#20(*)  getch&)

  brea$

 case #7

  for&i=0i%n'1i'')  aKiLK1L=aKiLK1LA&!1)

  aKiLK0L=aKiLK0LA&!1) B

  cleardevice&)

  for&i=0i%ni'')  line&#20'aKiLK0L(2*0!aKiLK1L(#20'aKi'1LK0L(2*0!aKi'1LK1L)

  line&0(2*0(H#(2*0)  line&#20(0(#20(*)

  getch&)

  brea$  case *7

  for&i=0i%n'1i'')

  temp=aKiLK0L

  aKiLK0L=aKiLK1L  aKiLK1L=tempB

  cleardevice&)

  for&i=0i%ni'')  line&#20'aKiLK0L(2*0!aKiLK1L(#20'aKi'1LK0L(2*0!aKi'1LK1L)

  line&0(2*0(H#(2*0)

  line&#20(0(#20(*)  line&0(*(H#(0)

  getch&)

  brea$

  case C7  for&i=0i%n'1i'')

  temp=aKiLK0L

  aKiLK0L=aKiLK1L  aKiLK1L=temp B

  for&i=0i%n'1i'')

  aKiLK1L=aKiLK1LA&!1)  aKiLK0L=aKiLK0LA&!1)B

  cleardevice&)

  for&i=0i%ni'')

  line&#20'aKiLK0L(2*0!aKiLK1L(#20'aKi'1LK0L(2*0!aKi'1LK1L)

Page 13: Cs2405 Cg Full Lab Manual

8/14/2019 Cs2405 Cg Full Lab Manual

http://slidepdf.com/reader/full/cs2405-cg-full-lab-manual 13/51

  line&0(2*0(H#(2*0)

  line&#20(0(#20(*)

  line&0(0(H#(*)  getch&)

  brea$

 default7  brea$

  B

  brea$  case H7

  cleardevice&)

  for&i=0i%ni'')

  line&#20'aKiLK0L(2*0!aKiLK1L(#20'aKi'1LK0L(2*0!aKi'1LK1L)  line&0(2*0(H#(2*0)

  line&#20(0(#20(*)

  goto"&1(1)

  printf&68n1.3 shear with reference line6)  printf&68n2.4 shear with " reference line6)

  printf&68nnter our choice76)  scanf&6>d6(5ch)

  switch&ch)

    case 17

  printf&68nnter the "!shear parameter value76)

  scanf&6>f6(5temp)

  printf&68nnter the ref line6)  scanf&6>f6(5t)

  bK0LK0L=1

  bK0LK1L=0  bK0LK2L=0

  bK1LK0L=temp

  bK1LK1L=1  bK1LK2L=0

  bK2LK0L=&!temp)A&t)

  bK2LK1L=0

  bK2LK2L=1

  for&i=0i%n'1i'')

  aKiLK2L=1 for&i=0i%n'1i'')

  for&F=0F%#F'')

  cKiLKFL = 0   for&$=0$%#$'')

  cKiLKFL = cKiLKFL ' aKiLK$L A bK$LKFL BB

  cleardevice&)

  for&i=0i%ni'')

Page 14: Cs2405 Cg Full Lab Manual

8/14/2019 Cs2405 Cg Full Lab Manual

http://slidepdf.com/reader/full/cs2405-cg-full-lab-manual 14/51

  line&#20'cKiLK0L(2*0!cKiLK1L(#20'cKi'1LK0L(2*0!cKi'1LK1L)

  line&0(2*0(H#(2*0)

  line&#20(0(#20(*)  getch&)

 brea$

  case 27  printf&68nnter the !shear parameter value76)

  scanf&6>f6(5temp)

  printf&68nnter the "ref line6)  scanf&6>f6(5t")

  bK0LK0L=1

  bK0LK1L=temp

  bK0LK2L=0  bK1LK0L=0

  bK1LK1L=1

  bK1LK2L=0

  bK2LK0L=0  bK2LK1L=&!temp)A&t")

  bK2LK2L=0  for&i=0i%n'1i'')

  aKiLK2L=1

 for&i=0i%n'1i'')

  for&F=0F%#F'')

  cKiLKFL = 0

  for&$=0$%#$'')  cKiLKFL = cKiLKFL ' aKiLK$L A bK$LKFL BB

  cleardevice&)  for&i=0i%ni'')

  line&#20'cKiLK0L(2*0!cKiLK1L(#20'cKi'1LK0L(2*0!cKi'1LK1L)

  line&0(2*0(H#(2*0)  line&#20(0(#20(*)

  getch&)

 brea$

  default7 brea$ B  brea$

case 7

  e"it&1)  closegraph&)

  restorecrtmode&)

  brea$  default7 brea$

  B

  B

B

Page 15: Cs2405 Cg Full Lab Manual

8/14/2019 Cs2405 Cg Full Lab Manual

http://slidepdf.com/reader/full/cs2405-cg-full-lab-manual 15/51

OUTPUT:

AAAAAAAA Program to perform 2!/ Transformations AAAAAAAA

  1. Nccept the polgon  2. Perform translation

  #. Perform scaling

  *. Perform rotation  C. Perform reflection

  H. Perform shearing

 nter our choice7 1 nter no of points7 *

nter "( co!ordinates for 17100 1C0

nter "( co!ordinates for 27200 100

nter "( co!ordinates for #71C0 100nter "( co!ordinates for *7200 100

nter our choice7 2

nter translation vectors t" and t7 C0 C0

nter our choice7 #

nter scaling vectors t" and t70.C 0.C

nter our choice7 *

nter the angle of rotation70

Page 16: Cs2405 Cg Full Lab Manual

8/14/2019 Cs2405 Cg Full Lab Manual

http://slidepdf.com/reader/full/cs2405-cg-full-lab-manual 16/51

nter point of rotation72C 20

1.cloc$wise 2.anticloc$wise 1

nter our choice7 C

1.+eflection about 4!a"is

2.+eflection about 3!a"is

#.+eflection about origin*..+eflection about line ="

 C.+eflection about line =!" nter our choice71

nter our choice7 C

1.3 shear with reference line2.4 shear with " reference line

nter our choice71

nter the "!shear parameter value70.# 0.#

RESULT: The rotation(scaling(reflection(shearing is implemented successfull.

Page 17: Cs2405 Cg Full Lab Manual

8/14/2019 Cs2405 Cg Full Lab Manual

http://slidepdf.com/reader/full/cs2405-cg-full-lab-manual 17/51

Ex%t no: ,

Date :

CO+POS"TE &D TRA#S*OR+AT"O#

Aim: 

To appl composite transformation to 2!/imensional shapes in c.

Algorithm:

1.Input upper left corner and lower right corner of the rectangle.

2.To multiple translation the rectangle appl formula "1="'t" and 1='t

#.To rotate multiple times the rectangle appla1=&"a'&&a!"a)Acos&theta)!&b!a)Asin&theta)))

 b1=&a'&&a!"a)Asin&theta)!&b!a)Acos&theta)))

*.To composite scale the rectangle siJe

"1="As"

1=As

C.omposite +eflection

a1=&"a'&&a!"a)Acos&theta)!&!b!a)Asin&theta))) b1=&a'&&a!"a)Asin&theta)'&!b!a)Acos&theta)))

H. omposite hearing

"1="'&A"1s)

1=

aJ1=aJ'&wA"1s)

w1=w

Program:

,include%stdio.h-

,include%conio.h-

,include%graphics.h-

,include%math.h-int ch("((aJ(i(w(ch1(ch2("a(a(ra(ra1(aK10L(bK10L(da(db

float "1(1(aJ1(w1(d"1(d1(d"2(d2(theta(theta1("1s(1s(s"2(s2(s"1(s1(a1K10L(b1K10L

void main&)

int gd(gm

clrscr&)

detectgraph&5gd(5gm)

initgraph&5gd(5gm(6c788tc88bgi6)

 printf&68nnter the upper left corner of the rectangle76)

scanf&6>d>d6(5"(5)

 printf&68nnter the lowest right corner of the rectangle76)

scanf&6>d>d6(5aJ(5w)

rectangle&"((aJ(w)da=aJ!"

db=w!

aK0L="

 bK0L=

aK1L="'da bK1L=

aK2L="'da

 bK2L='db

Page 18: Cs2405 Cg Full Lab Manual

8/14/2019 Cs2405 Cg Full Lab Manual

http://slidepdf.com/reader/full/cs2405-cg-full-lab-manual 18/51

aK#L="

 bK#L='db

while&1)

 printf&68nAAAAAomposite 2/ TransformationsAAAAAA6)

 printf&68n1.Translation8n2.+otation8n#.caling8n*."it8n nter our choice76)scanf&6>d6(5ch)

switch&ch)case 17

detectgraph&5gd(5gm)

initgraph&5gd(5gm(6c788tc88bgi6)

rectangle&"((aJ(w)

 printf&68nAAAAATranslationAAAAAA6)

 printf&68nnter the value of 1st shift factor76)

scanf&6>f>f6(5d"1(5d1)

 printf&68nnter the value of 2nd shift factor76)

scanf&6>f>f6(5d"2(5d2)

"1="'d"1

1='d1

aJ1=aJ'd"1w1=w'd1

rectangle&"1(1(aJ1(w1)

"1="1'd"2

1=1'd2aJ1=aJ1'd"2

w1=w1'd2

rectangle&"1(1(aJ1(w1)

 brea$

case 27

detectgraph&5gd(5gm)

initgraph&5gd(5gm(6c788tc88bgi6)

rectangle&"((aJ(w) printf&68nAAAAA+otationAAAAAA6)

 printf&68nnter the value of fi"ed point76)

scanf&6>d>d6(5"a(5a)

 printf&68nnter the value of angle of rotation 1 5 rotation 276)

scanf&6>d>d6(5ra(5ra1)

theta=&float)&raA&#.1*D1M0))for&i=0i%*i'')

a1KiL=&"a'&&aKiL!"a)Acos&theta)!&bKiL!a)Asin&theta)))

 b1KiL=&a'&&aKiL!"a)Asin&theta)'&bKiL!a)Acos&theta)))

Bfor&i=0i%*i'')

if&i=#)line&a1KiL(b1KiL(a1Ki'1L(b1Ki'1L)

else

line&a1KiL(b1KiL(a1K0L(b1K0L)

B

theta1=theta'&float)&ra1A&#.1*D1M0))

for&i=0i%*i'')

a1KiL=&"a'&&aKiL!"a)Acos&theta1)!&bKiL!a)Asin&theta1)))

 b1KiL=&a'&&aKiL!"a)Asin&theta1)'&bKiL!a)Acos&theta1)))

Page 19: Cs2405 Cg Full Lab Manual

8/14/2019 Cs2405 Cg Full Lab Manual

http://slidepdf.com/reader/full/cs2405-cg-full-lab-manual 19/51

B

for&i=0i%*i'')

if&i=#)

line&a1KiL(b1KiL(a1Ki'1L(b1Ki'1L)else

line&a1KiL(b1KiL(a1K0L(b1K0L)B

 brea$

case #7

detectgraph&5gd(5gm)

initgraph&5gd(5gm(6c788tc88bgi6)

rectangle&"((aJ(w)

 printf&68nAAAAAcalingAAAAAA6)

 printf&68nnter the value of caling factor176)

scanf&6>f>f6(5s"1(5s1)

 printf&68nnter the value of caling factor276)

scanf&6>f>f6(5s"2(5s2)

"1="As"11=As1

aJ1=aJAs"1

w1=wAs1

rectangle&"((aJ1(w1)"1="1As"2

1=1As2

aJ1=aJ1As"2

w1=w1As2

rectangle&"1(1(aJ1(w1)

 brea$

case *7

e"it&0)B

getch&)

B

B

Out%ut:nter the upper left corner of the rectangle7

100

100

nter the lower right corner of the rectangle7

200

200AAAAAA2/TransformationsAAAAAAA

1.Translation

2.+otation

#.caling

*."it

nter our choice7 1

AAAAAAATranslationAAAAAAA

nter the value of 1st shift factor7 C0 C0

Page 20: Cs2405 Cg Full Lab Manual

8/14/2019 Cs2405 Cg Full Lab Manual

http://slidepdf.com/reader/full/cs2405-cg-full-lab-manual 20/51

nter the value of 2nd shift factor7 100 100

AAAAAA2/TransformationsAAAAAAA1.Translation

2.+otation

#.caling

*."it

nter our choice7 2

AAAAAAA+otationAAAAAAA

nter the value of fi"ed point7

200

200

nter the value of angle of rotation 1 5 rotation 27 C0 0

AAAAAA2/TransformationsAAAAAAA 1.Translation

2.+otation

Page 21: Cs2405 Cg Full Lab Manual

8/14/2019 Cs2405 Cg Full Lab Manual

http://slidepdf.com/reader/full/cs2405-cg-full-lab-manual 21/51

#.caling

*."it

nter our choice7 #

AAAAAAAAcalingAAAAAAA

nter the value of caling factor17 2 2nter the value of caling factor27 2 2

Result:

Page 22: Cs2405 Cg Full Lab Manual

8/14/2019 Cs2405 Cg Full Lab Manual

http://slidepdf.com/reader/full/cs2405-cg-full-lab-manual 22/51

Thus the program was e"ecuted successfull.

E-.#O.:/

Date : CO0E#SUT0ERLA#D L"#E CL"PP"#$

Aim: To clip the lines fallen outside the window using cohen utherland line clipping algorithm.

Algorithm:

1.Input two line endpoints

2.alculate the differences between the endpoints and clipping boundaries

#.if line as to be clipped left side of the window p1&")="!"wmin and to clip right side set p2&")="wma"!"

*. if line as to be clipped down side of the window p2&)=!wmin and to clip upper side set p2&)=wma"!

Program:

,include%iostream.h-

,include%graphics.h-

,include%conio.h-

,include%stdlib.h-

tpedef struct coord

int "(

char codeK*L

Bptclass sulc

 public7

void drawwindow&)

void drawline&pt p1(pt p2(int c1)

 pt setcode&pt p)

int visibilit&pt p1(pt p2)

 pt resetendpt&pt p1(pt p2)

Bvoid sulc77drawwindow&)

setcolor&12)

line&1C0(100(*C0(100)

line&*C0(100(*C0(#C0)

line&*C0(#C0(1C0(#C0)

line&1C0(#C0(1C0(100)B

void sulc77drawline&pt p1(pt p2(int c1)

setcolor&c1)line&p1."(p1.(p2."(p2.)

B pt sulc77setcode&pt p)

 pt ptemp

if&p.%100)

 ptemp.codeK0L=Q1Q

else

 ptemp.codeK0L=Q0Q

if&p.-#C0)

 ptemp.codeK1L=Q1Q

Page 23: Cs2405 Cg Full Lab Manual

8/14/2019 Cs2405 Cg Full Lab Manual

http://slidepdf.com/reader/full/cs2405-cg-full-lab-manual 23/51

else

 ptemp.codeK1L=Q0Q

if&p."-*C0)

 ptemp.codeK2L=Q1Q

else ptemp.codeK2L=Q0Q

if&p."%1C0) ptemp.codeK#L=Q1Q

else

 ptemp.codeK#L=Q0Q

 ptemp."=p."

 ptemp.=p.

return&ptemp)

B

int sulc77visibilit&pt p1(pt p2)

int i(flag=0

for&i=0i%*i'')

if&&p1.codeKiL=Q0Q)RR&p2.codeKiL=Q0Q))

flag=1

B

if&flag==0)return&0)

for&i=0i%*i'')

if&&p1.codeKiL==p2.codeKiL)55&p1.codeKiL==Q1Q))

flag=0

B

if&flag==0)

return&1)return&2)

B

 pt sulc77resetendpt&pt p1(pt p2)

 pt temp

int "((ifloat m($

if&p1.codeK#L==Q1Q)

"=1C0

if&p1.codeK2L==Q1Q)"=*C0

if&&p1.codeK#L==Q1Q)RR&p1.codeK2L==Q1Q))

m=&float)&p2.!p1.)D&p2."!p1.")

$=&p1.'&mA&"!p1.")))

temp.=$

temp."="

for&i=0i%*i'')

temp.codeKiL=p1.codeKiL

if&temp."%=*C055temp."-=1C0)

Page 24: Cs2405 Cg Full Lab Manual

8/14/2019 Cs2405 Cg Full Lab Manual

http://slidepdf.com/reader/full/cs2405-cg-full-lab-manual 24/51

return&temp)

B

if&p1.codeK0L==Q1Q)

=100

if&p1.codeK1L==Q1Q)=#C0

if&&p1.codeK0L==Q1Q)RR&p1.codeK1L==Q1Q))

m=&float)&p2.!p1.)D&p2."!p1.")

$=&float)p1."'&float)&!p1.)Dm

temp."=$

temp.=

for&i=0i%*i'')

temp.codeKiL=p1.codeKiL

if&temp.%=#C055temp.-100)

return&temp)

B

else

return&p1)B

void main&)

int gd=/TT(gm(v

sulc c1 pt p1(p2(ptemp

initgraph&5gd(5gm(6c788tc88bgi6)

int "1K10L(1K10L("2K10L(2K10L

cleardevice&)

int i(n

sette"tstle&*(0(*)

outte"t&68nAAAAAohen utherland ;ine lippingAAAAA6)

cout%%68n8n8nnter the no. of lines76cin--n

for&i=0i%ni'')

cout%%68nnter end point1&"1(1)76

cin--"1KiL--1KiL

cout%%68nnter end point2&"2(2)76

cin--"2KiL--2KiLB

cleardevice&)

sette"tstle&0(0(#)

outte"t&68nSefore lipping6)c1.drawwindow&)

for&i=0i%ni'') p1."="1KiL

 p1.=1KiL

 p2."="2KiL

 p2.=2KiL

c1.drawline&p1(p2(1C)

B

getch&)

cleardevice&)

Page 25: Cs2405 Cg Full Lab Manual

8/14/2019 Cs2405 Cg Full Lab Manual

http://slidepdf.com/reader/full/cs2405-cg-full-lab-manual 25/51

sette"tstle&0(0(#)

outte"t&68nNfter lipping6)

for&i=0i%ni'')

 p1."="1KiL p1.=1KiL

 p2."="2KiL p2.=2KiL

 p1=c1.setcode&p1)

 p2=c1.setcode&p2)

v=c1.visibilit&p1(p2)

switch&v)

case 07

c1.drawwindow&)

c1.drawline&p1(p2(1C)

 brea$

case 17

c1.drawwindow&)

 brea$case 27

 p1=c1.resetendpt&p1(p2)

 p2=c1.resetendpt&p2(p1)

c1.drawwindow&)c1.drawline&p1(p2(1C)

 brea$

B

B

getch&)

closegraph&)

B

Out%ut:

nter the no.of lines7 1

nter end!point1&"1(1)7#0 *0

nter end!point1&"2(2)7#00 *00

Sefore clipping

Page 26: Cs2405 Cg Full Lab Manual

8/14/2019 Cs2405 Cg Full Lab Manual

http://slidepdf.com/reader/full/cs2405-cg-full-lab-manual 26/51

A'ter !li%%ing

Result:

Thus the program was e"ecuted successfull.

Page 27: Cs2405 Cg Full Lab Manual

8/14/2019 Cs2405 Cg Full Lab Manual

http://slidepdf.com/reader/full/cs2405-cg-full-lab-manual 27/51

E-.#O.:2

Date : 0OD$E+A# SUT0ERLA#D POL3$O# CL"PP"#$

Aim:

 To clip the polgon region fallen outside the window using @odgeman utherland polgon clippingalgorithm.

Algorithm:1.if first verte" is outside the window boundar and the second verte" is inside( both the intersection point

of the polgon edge with window boundar and the second verte" are added to the output verte" list

2.if both input vertices are inside the window( onl the second verte" is added to the output verte" list

#.if the first verte" is inside the window boundar and the second verte" is outside( onl the edge

intersection with the window boundar is added to the output verte" list

*. if both input vertices are outside the window boundar( nothing is added to the output list.

Program:

,include%iostream.h-

,include%conio.h-

,include%graphics.h-

,define round&a) &&int)&a'0.C))int $

float "min(min("ma"(ma"(arrK20L(m

void clipl&float "1(float 1(float "2(float 2)

if&"2!"1)

m=&2!1)D&"2!"1)

else

m=100000

if&"1-="min 55 "2-="min)

arrK$L="2

arrK$'1L=2

$'=2B

if&"1%"min 55 "2-="min)

arrK$L="min

arrK$'1L=1'mA&"min!"1)arrK$'2L="2

arrK$'#L=2

$'=*

Page 28: Cs2405 Cg Full Lab Manual

8/14/2019 Cs2405 Cg Full Lab Manual

http://slidepdf.com/reader/full/cs2405-cg-full-lab-manual 28/51

B

if&"1-="min 55 "2%"min)

arrK$L="min

arrK$'1L=1'mA&"min!"1)$'=2

BB

void clipt&float "1(float 1(float "2(float 2)

if&2!1)

m=&"2!"1)D&2!1)

else

m=100000

if&1%=ma" 55 2%=ma")

arrK$L="2

arrK$'1L=2

$'=2B

if&1-ma" 55 2%=ma")

arrK$L="1'mA&ma"!1)arrK$'1L=ma"

arrK$'2L="2

arrK$'#L=2

$'=*

B

if&1%=ma" 55 2-ma")

arrK$L="1'mA&ma"!1)arrK$'1L=ma"

$'=2

BB

void clipr&float "1(float 1(float "2(float 2)

if&"2!"1)m=&2!1)D&"2!"1)

else

m=100000

if&"1%="ma" 55 "2%="ma")

arrK$L="2arrK$'1L=2

$'=2

B

if&"1-"ma" 55 "2%="ma")

arrK$L="ma"

arrK$'1L=1'mA&"ma"!"1)

arrK$'2L="2

Page 29: Cs2405 Cg Full Lab Manual

8/14/2019 Cs2405 Cg Full Lab Manual

http://slidepdf.com/reader/full/cs2405-cg-full-lab-manual 29/51

arrK$'#L=2

$'=*

B

if&"1%="ma" 55 "2-"ma")

arrK$L="ma"

arrK$'1L=1'mA&"ma"!"1)$'=2

B

B

void clipb&float "1(float 1(float "2(float 2)

if&2!1)

m=&"2!"1)D&2!1)

else

m=100000

if&1-=min 55 2-=min)

arrK$L="2arrK$'1L=2

$'=2

B

if&1%min 55 2-=min)

arrK$L="1'mA&min!1)

arrK$'1L=min

arrK$'2L="2

arrK$'#L=2

$'=*

B

if&1-=min 55 2%min)

arrK$L="1'mA&min!1)

arrK$'1L=min

$'=2

B

Bvoid main&)

int gdriver=/TT(gmode(n(polK20L

float "i(i("f(f(polK20Lclrscr&)

cout%%6oordinates of rectangular clip window78n "min(min76cin--"min--min

cout%%6"ma"(ma"76

cin--"ma"--ma"

cout%%68n8n Polgon to be clipped78n 9umber of sides76

cin--n

cout%%6nter the coordinates76

for&int i=0i%2Ani'')

cin--polKiL

Page 30: Cs2405 Cg Full Lab Manual

8/14/2019 Cs2405 Cg Full Lab Manual

http://slidepdf.com/reader/full/cs2405-cg-full-lab-manual 30/51

 polKiL=polK0L

 polKi'1L=polK1L

for&i=0i%2An'2i'')

 polKiL=round&polKiL)

initgraph&5gdriver(5gmode(6c788tc88bgi6)setcolor&+/)

rectangle&"min(ma"("ma"(min)cout%%68t8t G9;IPP/ PO;4?O96

setcolor&:@IT)

fillpol&n(pol)

getch&)

cleardevice&)

$=0

for&i=0i%2Ani'=2)

clipl&polKiL(polKi'1L(polKi'2L(polKi'#L)

n=$D2

for&i=0i%$i'')

 polKiL=arrKiL

 polKiL=polK0L polKi'1L=polK1L

$=0

for&i=0i%2Ani'=2)

clipt&polKiL(polKi'1L(polKi'2L(polKi'#L)n=$D2

for&i=0i%$i'')

 polKiL=arrKiL

 polKiL=polK0L

 polKi'1L=polK1L

$=0

for&i=0i%2Ani'=2)

clipr&polKiL(polKi'1L(polKi'2L(polKi'#L)n=$D2

for&i=0i%$i'')

 polKiL=arrKiL

 polKiL=polK0L

 polKi'1L=polK1L

$=0for&i=0i%2Ani'=2)

clipb&polKiL(polKi'1L(polKi'2L(polKi'#L)

for&i=0i%$i'')

 polKiL=round&arrKiL)if&$)

fillpol&$D2(pol)setcolor&+/)

rectangle&"min(ma"("ma"(min)

cout%%68t ;IPP/ PO;4?O96

getch&)

closegraph&)

B

Out%ut:

Page 31: Cs2405 Cg Full Lab Manual

8/14/2019 Cs2405 Cg Full Lab Manual

http://slidepdf.com/reader/full/cs2405-cg-full-lab-manual 31/51

oordinates of rectangular clip window7

 "min(min7 100 200

 "ma"(ma"7 #C0 *00

Polgon to be clipped7

 9umber of sides *nter the coordinates C #00 2C0 100 #00 #00 2C0 *C0

 U#CL"PPED POL3$O#

CL"PPED POL3$O#

Result: Thus the program was e"ecuted successfull.

Page 32: Cs2405 Cg Full Lab Manual

8/14/2019 Cs2405 Cg Full Lab Manual

http://slidepdf.com/reader/full/cs2405-cg-full-lab-manual 32/51

E-.#O.: 4

Date : )D TRA#S*OR+AT"O#

Aim: 

To appl transformation to #!/imensional shapes in c''.

Algorithm:

1.To translate the cube appl formula"#="1't"

#=1't

J#=J1'tJ

2.To rotate the cube appl"#="1

#=1Acos&theta)!J1Asin&theta)

J#=1Asin&theta)'J1Acos&theta)

#.To scale the cube siJe

"#="f'&"1As")'"fA&1!s")

#=f'&1As)'fA&1!s)

J#=Jf'&J1AsJ)'JfA&1!sJ)

Program:

,include%iostream.h-

,include%graphics.h-

,include%math.h-

,include%conio.h-

,include%stdlib.h-class cube

 public7

void drawcube&int "1KL(int 1KL)

int ifor&i=0i%*i'')

if&i%#)

line&"1KiL(1KiL("1Ki'1L(1Ki'1L)

line&"1K0L(1K0L("1K#L(1K#L)

B

for&i=*i%Mi'')

if&i%)

Page 33: Cs2405 Cg Full Lab Manual

8/14/2019 Cs2405 Cg Full Lab Manual

http://slidepdf.com/reader/full/cs2405-cg-full-lab-manual 33/51

line&"1KiL(1KiL("1Ki'1L(1Ki'1L)

line&"1K*L(1K*L("1KL(1KL)

B

for&i=0i%*i'')

line&"1KiL(1KiL("1Ki'*L(1Ki'*L)

BB

B

void main&)

int

i("1KML(1KML("2KML(2KML(J1KML("#KML(#KML(J#KML("*KML(*KML(theta(op(ch(t"(t(tJ(s"(s(sJ("f(f(Jf("((J(siJ

e

int driver=/TT

int mode

initgraph&5driver(5mode(6c788tc88bgi6)

cout%%6enter the points on the cube76

cin--"----Jcout%%6enter the siJe of the edge76

cin--siJe

"1K0L="1K#L="

"1K1L="1K2L="'siJe"1K*L="1KL="

"1KCL="1KHL="'siJe

1K0L=1K1L=

1K2L=1K#L='siJe

1K*L=1KCL=

1KHL=1KL='siJe

J1K1L=J1K2L=J1K#L=J1K0L=J

J1K*L=J1KCL=J1KHL=J1KL=J!siJefor&i=0i%Mi'')

"2KiL="1KiL'J1KiLD2

2KiL=1KiL'J1KiLD2

B

cube c

getch&)cleardevice&)

do

cout%%6menu6%%endl

cout%%68n 1.Translation8n 2.+otation8n #.caling8n *."it8n6cout%%6enter the choice76

cin--chswitch&ch)

case 17

cout%%6enter the translation vector76

cin--t"--t--tJ

for&i=0i%Mi'')

"#KiL="1KiL't"

#KiL=1KiL't

Page 34: Cs2405 Cg Full Lab Manual

8/14/2019 Cs2405 Cg Full Lab Manual

http://slidepdf.com/reader/full/cs2405-cg-full-lab-manual 34/51

J#KiL=J1KiL'tJ

B

for&i=0i%Mi'')

"*KiL="#KiL'J#KiLD2*KiL=#KiL'J#KiLD2

Bcleardevice&)

cout%%6before translation6

c.drawcube&"2(2)

getch&)

cleardevice&)

cout%%6after translation6

c.drawcube&"*(*)

getch&)

cleardevice&)

 brea$

case 27

cout%%6enter the rotation angle76cin--theta

theta=&thetaA#.1*)D1M0

cout%%6enter the direction6%%endl

cout%%61.+otation about " a"is6%%endl%%62.+otation about a"is6%%endl%%6#.+otation about J a"is6cin--op

if&op==1)

for&i=0i%Mi'')

"#KiL="1KiL

#KiL=1KiLAcos&theta)!J1KiLAsin&theta)

J#KiL=1KiLAsin&theta)'J1KiLAcos&theta)B

B

else

if&op==2)

for&i=0i%Mi'')

#KiL=1KiL

"#KiL=J1KiLAcos&theta)!"1KiLAsin&theta)

"#KiL=J1KiLAsin&theta)'"1KiLAcos&theta)B

Belse

if&op==#)

for&i=0i%Mi'')

J#KiL=J1KiL

"#KiL="1KiLAcos&theta)!1KiLAsin&theta)

#KiL="1KiLAsin&theta)'1KiLAcos&theta)

Page 35: Cs2405 Cg Full Lab Manual

8/14/2019 Cs2405 Cg Full Lab Manual

http://slidepdf.com/reader/full/cs2405-cg-full-lab-manual 35/51

B

B

else

cout%%6enter correct option6

for&i=0i%Mi'')

"*KiL="#KiL'J#KiLD2*KiL=#KiL'J#KiLD2

B

cleardevice&)

cout%%6before rotation6

c.drawcube&"2(2)

getch&)

cleardevice&)

cout%%6after rotation6

c.drawcube&"*(*)

getch&)

cleardevice&)

 brea$case #7

cout%%6enter scaling factor76

cin--s"--s--sJ

cout%%6enter the reference point76cin--"f--f--Jf

for&i=0i%Mi'')

"#KiL="f'&"1KiLAs")'"fA&1!s")

#KiL=f'&1KiLAs)'fA&1!s)

J#KiL=Jf'&J1KiLAsJ)'JfA&1!sJ)

B

for&i=0i%Mi'')

"*KiL="#KiL'J#KiLD2

*KiL=#KiL'J#KiLD2

B

cleardevice&)

cout%%6before scaling6c.drawcube&"2(2)

getch&)

cleardevice&)

cout%%6after scaling6c.drawcube&"*(*)

getch&) brea$

case *7

e"it&0)

 brea$

B

B

while&op=*)

getch&)

Page 36: Cs2405 Cg Full Lab Manual

8/14/2019 Cs2405 Cg Full Lab Manual

http://slidepdf.com/reader/full/cs2405-cg-full-lab-manual 36/51

B

Out%ut:

nter the point in the cube7 100 100 100

nter the siJe of the edge7 C0

enu

1. translation

2. rotation

#. scaling

*. e"it

nter the choice71

nter the translation vector7 C(10(1C

Sefore translation Nfter translation

Result: Thus the program was e"ecuted successfull.

Page 37: Cs2405 Cg Full Lab Manual

8/14/2019 Cs2405 Cg Full Lab Manual

http://slidepdf.com/reader/full/cs2405-cg-full-lab-manual 37/51

E-.#O.: 5

Date : CO+POS"TE )D TRA#S*OR+AT"O#

Aim: To appl transformation to #!/imensional shapes in c''.

Algorithm:

1.To composite translate the cube appl formula

"#="1't"#=1't

J#=J1'tJ

2.To composite rotate the cube appl

"#="1#=1Acos&theta)!J1Asin&theta)

J#=1Asin&theta)'J1Acos&theta)

#.To composite scale the cube siJe

"#="f'&"1As")'"fA&1!s")

#=f'&1As)'fA&1!s)

J#=Jf'&J1AsJ)'JfA&1!sJ)

Program:,include%iostream.h-

,include%graphics.h-

,include%math.h-

,include%conio.h-

,include%stdlib.h-

class cube

 public7

void drawcube&int "1KL(int 1KL)

int i

for&i=0i%*i'')if&i%#)

line&"1KiL(1KiL("1Ki'1L(1Ki'1L)

line&"1K0L(1K0L("1K#L(1K#L)

B

 for&i=*i%Mi'')

if&i%)

line&"1KiL(1KiL("1Ki'1L(1Ki'1L)

Page 38: Cs2405 Cg Full Lab Manual

8/14/2019 Cs2405 Cg Full Lab Manual

http://slidepdf.com/reader/full/cs2405-cg-full-lab-manual 38/51

line&"1K*L(1K*L("1KL(1KL)

B

for&i=0i%*i'')

line&"1KiL(1KiL("1Ki'*L(1Ki'*L)B

BB

void main&)

int

i("1KML(1KML("2KML(2KML(J1KML("#KML(#KML(J#KML("*KML(*KML(theta(theta1("f1(f1(Jf1(op(ch(t"(t(tJ(t"1(t1

(tJ1(s"1(s1(sJ1(s"(s(sJ("f(f(Jf("((J(siJe

int driver=/TT

int mode

initgraph&5driver(5mode(6c788tc88bgi6)

cout%%6enter the points on the cube76

cin--"----J

cout%%6enter the siJe of the edge76cin--siJe

"1K0L="1K#L="

"1K1L="1K2L="'siJe

"1K*L="1KL=""1KCL="1KHL="'siJe

1K0L=1K1L=

1K2L=1K#L='siJe

1K*L=1KCL=

1KHL=1KL='siJe

J1K1L=J1K2L=J1K#L=J1K0L=J

J1K*L=J1KCL=J1KHL=J1KL=J!siJe

for&i=0i%Mi'')

"2KiL="1KiL'J1KiLD2

2KiL=1KiL'J1KiLD2

B

cube c

getch&)cleardevice&)

do

cout%%6menu6%%endl

cout%%68n1.translation8n2.rotation8n#.scaling8n*.e"it8n6cout%%6enter the choice76

cin--chswitch&ch)

case 17

cout%%6enter the first translation vector76

cin--t"--t--tJ

cout%%68n enter the second translation vector76

cin--t"1--t1--tJ1

cleardevice&)

Page 39: Cs2405 Cg Full Lab Manual

8/14/2019 Cs2405 Cg Full Lab Manual

http://slidepdf.com/reader/full/cs2405-cg-full-lab-manual 39/51

cout%%6before translation6

c.drawcube&"2(2)

getch&)

for&i=0i%Mi'')

"#KiL="1KiL't"

#KiL=1KiL'tJ#KiL=J1KiL'tJ

B

for&i=0i%Mi'')

"*KiL="#KiL'J#KiLD2

*KiL=#KiL'J#KiLD2

B

cleardevice&)

cout%%6after first translation6

c.drawcube&"*(*)

getch&)

for&i=0i%Mi'')

"#KiL="#KiL't"1

#KiL=#KiL't1

J#KiL=J#KiL'tJ1B

for&i=0i%Mi'')

"*KiL="#KiL'J#KiLD2

*KiL=#KiL'J#KiLD2

B

cleardevice&)

cout%%6after second translation6c.drawcube&"*(*)

getch&)

cleardevice&)

 brea$

case 27

cout%%6enter the first rotation angle76cin--theta

cout%%6enter the second rotation angle76

cin--theta1

theta=&thetaA#.1*)D1M0theta1=theta'&theta1A#.1*)D1M0

cout%%6enter the direction6%%endlcout%%61.rotation about " a"is6%%endl%%62.rotation about a"is6%%endl%%6#.rotation about J a"is6

cin--op

cleardevice&)

cout%%6before rotation6

c.drawcube&"2(2)

getch&)

if&op==1)

Page 40: Cs2405 Cg Full Lab Manual

8/14/2019 Cs2405 Cg Full Lab Manual

http://slidepdf.com/reader/full/cs2405-cg-full-lab-manual 40/51

for&i=0i%Mi'')

"#KiL="1KiL

#KiL=1KiLAcos&theta)!J1KiLAsin&theta)

J#KiL=1KiLAsin&theta)'J1KiLAcos&theta)B

for&i=0i%Mi'')

"*KiL="#KiL'J#KiLD2

*KiL=#KiL'J#KiLD2

B

cleardevice&)

cout%%6after first rotation6

c.drawcube&"*(*)

getch&)

for&i=0i%Mi'')

"#KiL="1KiL

#KiL=1KiLAcos&theta1)!J1KiLAsin&theta1)J#KiL=1KiLAsin&theta1)'J1KiLAcos&theta1)

B

for&i=0i%Mi'')

"*KiL="#KiL'J#KiLD2

*KiL=#KiL'J#KiLD2

B

cleardevice&)

cout%%6after second rotation6

c.drawcube&"*(*)

getch&)

Belse

if&op==2)

for&i=0i%Mi'')

#KiL=1KiL"#KiL=J1KiLAcos&theta)!"1KiLAsin&theta)

"#KiL=J1KiLAsin&theta)'"1KiLAcos&theta)

B

for&i=0i%Mi'')

"*KiL="#KiL'J#KiLD2*KiL=#KiL'J#KiLD2

B

cleardevice&)

cout%%6after first rotation6

c.drawcube&"*(*)

getch&)

for&i=0i%Mi'')

Page 41: Cs2405 Cg Full Lab Manual

8/14/2019 Cs2405 Cg Full Lab Manual

http://slidepdf.com/reader/full/cs2405-cg-full-lab-manual 41/51

#KiL=1KiL

"#KiL=J1KiLAcos&theta1)!"1KiLAsin&theta1)

"#KiL=J1KiLAsin&theta1)'"1KiLAcos&theta1)

B

for&i=0i%Mi'')

"*KiL="#KiL'J#KiLD2*KiL=#KiL'J#KiLD2

B

cleardevice&)

cout%%6after second rotation6

c.drawcube&"*(*)

getch&)

B

else

if&op==#)

for&i=0i%Mi'')

J#KiL=J1KiL

"#KiL="1KiLAcos&theta)!1KiLAsin&theta)

#KiL="1KiLAsin&theta)'1KiLAcos&theta)

Bfor&i=0i%Mi'')

"*KiL="#KiL'J#KiLD2

*KiL=#KiL'J#KiLD2

B

cleardevice&)

cout%%6after first rotation6

c.drawcube&"*(*)getch&)

for&i=0i%Mi'')

J#KiL=J1KiL

"#KiL="1KiLAcos&theta1)!1KiLAsin&theta1)

#KiL="1KiLAsin&theta1)'1KiLAcos&theta1)B

for&i=0i%Mi'')

"*KiL="#KiL'J#KiLD2*KiL=#KiL'J#KiLD2

Bcleardevice&)

cout%%6after second rotation6

c.drawcube&"*(*)

getch&)

B

else

cout%%6enter correct option6

cleardevice&)

Page 42: Cs2405 Cg Full Lab Manual

8/14/2019 Cs2405 Cg Full Lab Manual

http://slidepdf.com/reader/full/cs2405-cg-full-lab-manual 42/51

 brea$

case #7

cout%%6enter the first scaling factor76

cin--s"--s--sJ

cout%%6enter the second scaling factor76cin--s"1--s1--sJ1

cout%%6enter the first reference point76cin--"f--f--Jf

cout%%6enter the second reference point76

cin--"f1--f1--Jf1

cleardevice&)

cout%%6before scaling6

c.drawcube&"2(2)

getch&)

for&i=0i%Mi'')

"#KiL="f'&"1KiLAs")'"fA&1!s")

#KiL=f'&1KiLAs)'fA&1!s)

J#KiL=Jf'&J1KiLAsJ)'JfA&1!sJ)B

for&i=0i%Mi'')

"*KiL="#KiL'J#KiLD2*KiL=#KiL'J#KiLD2

B

cleardevice&)

cout%%6after first scaling6

c.drawcube&"*(*)

getch&)

for&i=0i%Mi'')

"#KiL="f1'&"1KiLAs"As"1)'"f1A&1!s"!s"1)

#KiL=f1'&1KiLAsAs1)'f1A&1!s!s1)

J#KiL=Jf1'&J1KiLAsJAsJ1)'Jf1A&1!sJ!sJ1)

B

for&i=0i%Mi'')

"*KiL="#KiL'J#KiLD2

*KiL=#KiL'J#KiLD2

B

cleardevice&)cout%%6after second scaling6

c.drawcube&"*(*)getch&)

cleardevice&)

 brea$

case *7

e"it&0)

 brea$BB

while&op=*)

getch&)

Page 43: Cs2405 Cg Full Lab Manual

8/14/2019 Cs2405 Cg Full Lab Manual

http://slidepdf.com/reader/full/cs2405-cg-full-lab-manual 43/51

B

Out%ut:

nter the point in the cube7 100 100 100

nter the siJe of the edge7 C0

enu

1. translation

2. rotation

#. scaling

*. e"it

nter the choice71

nter the <irst translation vector7 C(10(1C

nter the econd translation vector71C(1C(1C

Sefore translation

Nfter <irst translation

Nfter second translation

Result: Thus the program was e"ecuted successfull.

Page 44: Cs2405 Cg Full Lab Manual

8/14/2019 Cs2405 Cg Full Lab Manual

http://slidepdf.com/reader/full/cs2405-cg-full-lab-manual 44/51

E-.#O: 6

Date : ) D"+E#S"O#AL OB7ECTS A#D SCE#ES

Aim:

To create #/ obFects and scenes using blender animation software.

Algorithm:

ste% 1: Place the cursor to center b pressing shift' and selecting cursor to center option from the menu.

ste% &: Ndd an obFect b pressing shift'N and selecting the cube obFect from the pop menu.ste% ): hape of the obFect can be modified to a human bod b dividing the obFect and e"panding part of

the divided obFect.

ste% ,: Nnimating the human bod done b placing the armature bones in arms and legs of the human

 bod.

ste% /: Sones has to be deformed with the human bod such that arms and legs of the human bod moves

when the bone moves.

ste% 2: Gsing $e frames( movements of the human bod can be given for each timeline.

Pro!edure:

Slender provides ou with twent ;aers to help organiJe our wor$. 4ou can see which ;aers

are currentl visible from the group of twent buttons in the #/ Eiew window header.

Building the Bod8:

hange to the front view with 1 9umpad. pressing pace U Ndd U esh U ube. N cube will

appear. Press tab( and it will be in dit mode with all its vertices selected. dit mode is a mode in which

ou can edit the vertices of the mesh.

S default( all vertices are selected for ever new obFect created. ObFect mode is a mode where

vertices cannot be selected or individuall edited. The obFect can be changed onl as a whole. Press tab toswitch between these two modes. This will split each side of the cube in two( creating new vertices and

faces.

:ith our cursor hovering in the #/ window press N to deselect all elements 9ow locate the

ubdivide button in the esh Tools panel and press it once. ?o to right view b pressing # 9umpad. Nndselect the top two faces of the subdivided cube b right clic$ing the faces with the face selection button

on. <rom modifiers panel select add modifiers and select mirror option. 9ow e"trude the top right twofaces b selecting e"trude region or pressing e.

"trude b moving the mouse till the needed siJe for the hands. @ere( the both side top right and

left two faces e"trudes as the mirror option is selected. imilarl( change to bottom view and select the

left two faces in face selection mode and change to front view.

+otate the faces slight to the left direction b pressing + and moving mouse to the left. Nfter the

needed rotate press left mouse button. 9ow select e"trude region and e"trude the leg part where the both

sides are e"truded as the mirror is selected. hange to ObFect mode b press tab and clic$ on the Nppl

 button of the irror modifier.

Page 45: Cs2405 Cg Full Lab Manual

8/14/2019 Cs2405 Cg Full Lab Manual

http://slidepdf.com/reader/full/cs2405-cg-full-lab-manual 45/51

a$e sure ou are in dit mode. ove the cursor to e"actl one Slender Gnit &grid sVuare) above

?usWs bod &leftmost image in ?iving ?us a head). To place the cursor at a specific grid point( position it

near to where ou want it( then press shift's to summon the nap menu. The ursor➔ ?rid entr places

the cursor e"actl on a grid point. ursor➔ election places it e"actl on the selected obFect.

Ndd a new cube with shift's UNdd U ube. o far( what we have is a rough figure( at best. Toma$e ?us appear smoother( locate the odifiers panel in the diting conte"t( and add a ubsurface

modifier. To appl our smoothing effect to ?us( press the et mooth button found in the ;in$ andaterials panel of the diting conte"t. ?us should now appear smooth.

Animating $us:

Rigging

X In obFect mode( set our #/ cursor where ?usWs shoulder is( and press shift's U Ndd U Nrmature.

N rhomboidal obFect will appear( which is a bone of the armature sstem. nter dit mode. The end( or

tip( of the bone is selected &ellow).

X 9ow in dit mode( place the tip of the bone in ?usWs hand b grabbing ? and moving it( &Ndding

the first bone( an elbowless arm). :e donWt need an other bones right now. 4ou should now have one

 bone running from the shoulder to the hand area. Ns ou move the tip( ou will notice that the whole bone

gets bigger Y ou reall are scaling up the bone.

X Periodicall loo$ at the ?ingerbread man and armature from man different viewpoints to ma$e

sure the armature is inside the gingerbread man( Fust as bones are inside a human bod.X ta in dit mode( then move the cursor to where the hip Foint will be and add a new bone shift's

U Ndd U Sone.

X ?rab ? and move the ellow tip of the new bone to the $nee area.

X 9ow Zchain[ a new bone from the $nee to the foot clic$ing in the area of the foot. N new chained bone will appear automaticall lin$ed with the $nee and ending at the foot. Nnother wa of chaining the

new bone would be to e"trude using the shortcut. This variation creates the new bone and places ou in

grab mode automaticall. imilarl place the bones on the right side.

Posing

X elect the armature onl( then select Pose ode from the ode menu &ode menu in the #/

window header) Y or simpl hit trl⇆ Tab. This option is onl available when an armature is selected.

X The armature will turn blue. 4ou are now in Pose mode. If ou now select a bone it will turncan( not pin$( and if ou move it &?)( or rotate it &+)( the bod will deform accordingl

Original %osition

X Slender remembers the original position of the bones. 4ou can set our armature bac$ b

 pressing Nlt+ to clear the bonesW rotation( and Nlt? to clear their location. Nlternativel( the +est Position

 button ma be used to temporaril show the original position.

"n9erse inemati!s

X Inverse \inematics &I\) is where ou actuall define the position of the last bone in the chain(

often called an Zend effector[. Nll the other bones assume an algorithmic position( automaticallcomputed b the I\ solver( to $eep the chain without gaps &i.e. I\ will mathematicall solve the chain

 positions for us). This allows a much easier and precise positioning of hands and feet using I\.

*orward inemati!s

X :hile handling bones in Pose mode( notice that the act as rigid( ine"tensible bodies with

spherical Foints at the end. 4ou can grab onl the first bone of a chain and all the others will follow it. Nll

subseVuent bones in the chain cannot be grabbed and moved( ou can onl rotate them( so that the

selected bone rotates with respect to the previous bone in the chain while all the subseVuent bones of the

chain follow its rotation.

Page 46: Cs2405 Cg Full Lab Manual

8/14/2019 Cs2405 Cg Full Lab Manual

http://slidepdf.com/reader/full/cs2405-cg-full-lab-manual 46/51

X This procedure( called <orward \inematics &<\)( is eas to follow but it ma$es precise location

of the last bone in the chain difficult.

X a$e ?us wal$( using <\( b defining four different poses relative to four different stages of a

stride. Slender will do the wor$ of creating a fluid animation.

X <irst( verif that ou are at frame 1 of the timeline. The frame number appears in a numeric fieldon the right of the Suttons window header &The current frame numeric field in the Suttons window

header). If it is not set to 1( set it to 1 now.X 9ow( b rotating onl one bone at a time &+)( weWll raiseGp;eg.; and bend ;o;eg.; bac$wards

while raising Nrm.+a little and lowering Nrm.; a little( as shown in &Our first pose).

X elect all bones with N. :ith the mouse pointer on the #/ window( press I. N menu pops up

&toring the pose to the frame). elect ;oc+ot from this menu. This will get the position and orientation

of all bones and store them as a pose at frame 1. This pose represents ?us in the middle of his stride(

while moving his left leg forward and above the ground.

X 9ow move to frame 11 either b entering the number in the numeric field or b pressing ]. Then

move ?us to a different position( li$e &Our second pose). tart with clearing the rotation on both arms

using Nlt+ as mentioned earlier. <rom the top view( rotate Nrm.+ slightl forward and Nrm.; slightl

 bac$. <inish the pose with his left leg forward and right leg bac$ward( both slightl bent. ?us is wal$ing

in place

X elect all bones again and press I to store this pose at frame11( and select +ot.X :e now need a third pose at frame 21( with the right leg up( because we are in the middle of the

other half of the stride. This pose is the mirror of the one we defined at frame 1. Therefore( return to

frame 1 and( with all the bones selected( in the Pose menu of the #/ window header( select theop

urrent Pose entr( see &Pose menu). 4ou have now copied the current pose to the buffer.X ?o to frame 21 and paste the pose with thePaste <lipped Pose option in the Pose menu( see &Pose

menu). This will paste the cut pose( e"changing the positions of bones with suffi" Z.;[ with those of

 bones with suffi" Z.+[( effectivel flipping it

The pose is there but it has not been stored et 4ou must press I U +ot with all bones selected.

X 9ow appl the same procedure to cop the pose at frame 11to frame #1( also flipping it.

X To complete the ccle( we need to cop the pose at frame 1(without flipping it( to frame *1. /o

so b coping it as usual( and b using the Paste Pose entr. nd the seVuence b storing the pose with I

U +ot.

Page 47: Cs2405 Cg Full Lab Manual

8/14/2019 Cs2405 Cg Full Lab Manual

http://slidepdf.com/reader/full/cs2405-cg-full-lab-manual 47/51

Out%ut:

Page 48: Cs2405 Cg Full Lab Manual

8/14/2019 Cs2405 Cg Full Lab Manual

http://slidepdf.com/reader/full/cs2405-cg-full-lab-manual 48/51

Result:Thus the #/ obFect was created and animated using the blender software.

Page 49: Cs2405 Cg Full Lab Manual

8/14/2019 Cs2405 Cg Full Lab Manual

http://slidepdf.com/reader/full/cs2405-cg-full-lab-manual 49/51

E-.#O.: 1;

Date : $E#ERAT"#$ *RACTAL "+A$ES

Aim: To draw fractal images in c.

Algorithm:

1.Gse random function to select random number between 0 to 2

2.if random number is 0 ne"t point can be given b

"1 = &"2 ' #20) D 2

1 = &2 ' 0) D 2

#.if random number is 1 ne"t point can be given b"1 = &"2 ' 0) D 2

1 = &2 ' *M0) D 2

*.if random number is 2 ne"t point can be given b

"1 = &"2 ' H*0) D 2

1 = &2 ' *M0) D 2

Program:

,include %stdio.h-

,include %conio.h-

,include %stdlib.h-,include %math.h-

,include %graphics.h-void /rawierpins$i&void)

void main&void)

int gd=E?N

int gm=E?N@I

initgraph&5gd( 5gm( 688tc88bgi6)

/rawierpins$i&)

getch&)

Page 50: Cs2405 Cg Full Lab Manual

8/14/2019 Cs2405 Cg Full Lab Manual

http://slidepdf.com/reader/full/cs2405-cg-full-lab-manual 50/51

B

void /rawierpins$i&void)

char /irect

int iterateunsigned int "1( 1( "2( 2

"1 = "2 = #201 = 2 = 0

for&iterate = 0 iterate % 10000 iterate'')

/irect = random&#)

if&/irect == 0)

"1 = &"2 ' #20) D 2

1 = &2 ' 0) D 2

B

else if&/irect == 1)

"1 = &"2 ' 0) D 21 = &2 ' *M0) D 2

B

else if&/irect == 2)

"1 = &"2 ' H*0) D 2

1 = &2 ' *M0) D 2

B

 putpi"el&"1( 1( :@IT)

"2 = "1

2 = 1

BB

Out%ut :

Page 51: Cs2405 Cg Full Lab Manual

8/14/2019 Cs2405 Cg Full Lab Manual

http://slidepdf.com/reader/full/cs2405-cg-full-lab-manual 51/51

Result:

Thus the program was e"ecuted successfull.