CG Lab Manual(2012)

download CG Lab Manual(2012)

of 95

Transcript of CG Lab Manual(2012)

  • 7/31/2019 CG Lab Manual(2012)

    1/95

    Exno:1 Fundamental Shapes using Built in functions

    __________________________________________________________________________

    Aim:

    To write a c graphics program and to display fundamental shapes using built in functions.

    Algorithm:

    1. Detect graphics device and graphic mode, and initialize graphic systemusing Initgraph().

    2. Declare the required variables and display the options in switch case statement.

    3. The options 1.circle 2.rectangle 3.ellipse 4.line 5.Sector 6.polygon 7.Filled

    ellipse 8.arc 9.square 10.exit

    4.If the choice is 1, drawing circle using circle(int x,int y,int radius)function where x,y are

    coordinate values.

    5. If the choice is 2, drawing rectangle using rectangle(int left,int top,int right,int bottom)function.

    6. If the choice is 3, drawing ellipse using ellipse(int x, int y, int stangle, int end angle, int x radius,

    int y radius)function where x,y are coordinate values.

    7. If the choice is 4, drawing filled ellipse using fillellipse(int x, int y, int x radius, int yradius)function where x,y are coordinate values.

    8. If the choice is 5, drawing line using line(int x1, int y1, int x2, int y2)function where x1,y1,x2,y2

    are line point coordinate values.

    9. If the choice is 6, drawing sector using sector(int x, int y, int stangle, int end angle, int x radius, int

    y radius)function where x,y are coordinate values.

    10. If the choice is 7, drawing polygon using draypoly(int number of points, int polypoints)function.

    11. If the choice is 8, drawing arc using arc(int x, int y, int stangle, int end angle, int radius)function.

    12.Display the images and closegraph() which is used to close current opened graphic device.

  • 7/31/2019 CG Lab Manual(2012)

    2/95

    PROGRAM:

    #include

    #includevoid main()

    {

    int ch, gd=DETECT, gm;int poly[12]={350,450, 350,410, 430,400, 350,350, 300,430, 350,450 };

    initgraph(&gd, &gm, "");

    printf("\n1.circle\n2.rectangle\n3.ellipse\n4.line\n5.Sector\n6.polygon\n7.Filled

    ellipse\n8.arc\n9.square\n10.exit");do

    {

    printf("\nEnter ur choice::");

    scanf("%d",&ch);switch(ch)

    {

    case 1:circle(200,50,20);

    outtextxy(175,100, "Circle");

    break;case 2:

    rectangle(250,50,300,100);

    outtextxy(240,170, "Rectangle");

    break;case 3:

    ellipse(400,100,0,360, 50,25);

    outtextxy(480, 170, "Ellipse");

    break;case 4:

    line(200,250,300,250);

    outtextxy(300,260,"Line");break;

    case 5:

    sector(150, 400, 30, 300, 100,50);outtextxy(120, 460, "Sector");

    break;

    case 6:

    drawpoly(6, poly);

    outtextxy(340, 400, "Polygon");break;

    case 7:outtextxy(340, 460, "Filled elipse");

    fillellipse(400,190,50,20);

    break;case 8:

    outtextxy(360, 480, "arc");

    arc(375,375,200,-20,70);

    break;

  • 7/31/2019 CG Lab Manual(2012)

    3/95

    case 9:outtextxy(270,370, "Square");

    rectangle(450,450,400,400);break;

    case 10:

    exit(0);

    break;}

    }while(1);

    getch();closegraph();

    }

  • 7/31/2019 CG Lab Manual(2012)

    4/95

    OUTPUT:

  • 7/31/2019 CG Lab Manual(2012)

    5/95

    RESULT:

    Thus, the program executed successfully and output was obtained.

  • 7/31/2019 CG Lab Manual(2012)

    6/95

    Exno:2 Displaying doll image

    __________________________________________________________________________

    Aim:

    To write a c graphics program and to display doll image using built in functions.

    Algorithm:

    1. Detect graphics device and graphic mode, and initialize graphic systemusing Initgraph().

    2. Drawing the doll using some built in functions, such as,

    3.Drawing circle using circle(int x,int y,int radius)function where x,y are coordinate values.

    4. Drawing ellipse using ellipse(int x, int y, int stangle, int end angle, int x radius, int y

    radius)function where x,y are coordinate values.

    5. Drawing filled ellipse using fillellipse(int x, int y, int x radius, int y radius)function where x,y are

    coordinate values.

    6. Drawing arc using arc(int x, int y, int stangle, int end angle, int radius)function.

    7.Display the images and closegraph() which is used to close current opened graphic device.

  • 7/31/2019 CG Lab Manual(2012)

    7/95

    PROGRAM:

    #include

    #include#include

    void main()

    {int gd = DETECT, gm;

    initgraph(&gd,&gm,"");

    setcolor(YELLOW);circle(200,200,100);

    ellipse(150,180,0,360,20,30);

    ellipse(150,190,0,360,10,20);

    fillellipse(150,190,10,20);ellipse(240,180,0,360,20,30);

    ellipse(240,190,0,360,10,20);

    fillellipse(240,190,10,20);

    arc(200,200,200,-20,70);setcolor(RED);

    ellipse(230,250,200,-10,10,60);

    getch();closegraph();

    }

  • 7/31/2019 CG Lab Manual(2012)

    8/95

    OUTPUT:

    RESULT:

    Thus, the program executed successfully and output was obtained.

  • 7/31/2019 CG Lab Manual(2012)

    9/95

    Exno:3 Display English characters using Built in functions

    __________________________________________________________________________

    Aim:

    To write a c graphics program and to display English characters using built in functions.

    Algorithm:

    1. Detect graphics device and graphic mode, and initialize graphic systemusing Initgraph().

    2. Display English characters using built in functions such as,

    3.Drawing circle using circle(int x,int y,int radius)function where x,y are coordinate values.

    4. Drawing line using line(int x1, int y1, int x2, int y2)function where x1,y1,x2,y2 are line point

    coordinate values.

    5.Display the images and closegraph() which is used to close current opened graphic device.

  • 7/31/2019 CG Lab Manual(2012)

    10/95

    PROGRAM:

    #include

    #include

    #includevoid main()

    {

    int gd = DETECT, gm;

    initgraph(&gd,&gm,"");//p

    setcolor(5);

    line(40,150,40,200);line(40,150,60,150);

    line(40,175,60,175);

    line(60,150,60,175);

    setcolor(WHITE);setfillstyle(1,WHITE);

    fillellipse(65,195,3,3);

    //jsetcolor(11);

    line(90,150,110,150);

    line(100,150,100,200);line(100,200,80,190);

    //e

    setcolor(2);line(120,150,120,200);

    line(120,150,140,150);line(120,200,140,200);

    line(120,175,130,175);

    //gsetcolor(3);

    line(150,150,150,200);

    line(150,150,170,150);line(150,200,170,200);

    line(170,200,170,180);

    //asetcolor(4);

    line(200,150,180,200);

    line(200,150,220,200);

    line(190,175,210,175);//t

    setcolor(5);

    line(240,150,240,200);

    line(225,150,255,150);//h

    setcolor(6);

    line(270,150,270,200);line(290,150,290,200);

    line(270,175,290,175);

    //e

  • 7/31/2019 CG Lab Manual(2012)

    11/95

    setcolor(7);line(310,150,310,200);

    line(310,150,330,150);line(310,200,330,200);

    line(310,175,320,175);

    //e

    setcolor(8);line(340,150,340,200);

    line(340,150,360,150);

    line(340,200,360,200);line(340,175,350,175);

    //s

    setcolor(9);line(380,150,400,150);

    line(380,150,380,175);

    line(380,175,400,175);

    line(380,200,400,200);line(400,175,400,200);

    //w

    setcolor(10);line(420,150,420,200);line(450,150,450,200);

    line(435,175,420,200);

    line(435,175,450,200);//a

    setcolor(11);

    line(470,150,460,200);

    line(470,150,490,200);line(465,175,480,175);

    //r

    setcolor(12);line(510,150,510,200);

    line(510,150,530,150);

    line(510,175,530,200);line(510,175,530,175);

    line(530,150,530,175);

    //i

    setcolor(1);line(550,150,550,200);

    line(540,150,560,150);

    line(540,200,560,200);

    getch();closegraph();

    }

  • 7/31/2019 CG Lab Manual(2012)

    12/95

    OUTPUT:

    RESULT:

    Thus, the program executed successfully and output was obtained.

  • 7/31/2019 CG Lab Manual(2012)

    13/95

    Exno:4 Displaying Tamil characters

    __________________________________________________________________________

    Aim:

    To write a c graphics program and to display tamil characters using built in functions.

    Algorithm:

    1. Detect graphics device and graphic mode, and initialize graphic systemusing Initgraph().

    2. Display Tamil characters using built in functions such as,

    3.Drawing circle using circle(int x,int y,int radius)function where x,y are coordinate values.

    4. Drawing ellipse using ellipse(int x, int y, int stangle, int end angle, int x radius, int y

    radius)function where x,y are coordinate values.

    5.Drawing line using line(int x1, int y1, int x2, int y2)function where x1,y1,x2,y2 are line point

    coordinate values.

    6.Display the images and closegraph() which is used to close current opened graphic device.

  • 7/31/2019 CG Lab Manual(2012)

    14/95

    PROGRAM:

    #include

    #include#include

    void main()

    {int gd = DETECT, gm;

    initgraph(&gd,&gm,"");

    setcolor(11);line(120,160,120,200);

    line(140,160,140,200);

    line(120,200,140,200);

    line(140,200,150,200);line(150,200,150,210);

    line(150,210,150,220);

    line(150,220,140,220);

    setcolor(12);

    line(160,160,190,160);line(160,160,160,200);

    line(180,160,180,200);

    line(180,200,160,220);

    setcolor(5);

    line(210,160,270,160);

    ellipse(210,180,0,360,8,20);

    ellipse(230,180,0,360,8,20);ellipse(250,180,0,360,8,20);

    line(265,140,265,200);

    ellipse(273,140,0,180,8,20);

    setcolor(4);

    line(280,160,280,200);line(280,200,320,200);

    ellipse(305,200,0,180,15,25);

    setcolor(3);

    line(330,160,360,160);line(330,160,330,200);

    line(350,160,350,200);getch();

    closegraph();

    }

  • 7/31/2019 CG Lab Manual(2012)

    15/95

    OUTPUT:

    RESULT:

    Thus, the program executed successfully and output was obtained.

  • 7/31/2019 CG Lab Manual(2012)

    16/95

    Exno:5 Display Date of birth using Built in functions

    __________________________________________________________________________

    Aim:

    To write a c graphics program and to display date of birth using built in functions.

    Algorithm:

    1. Detect graphics device and graphic mode, and initialize graphic systemusing Initgraph().

    2. Display date of birth using built in functions such as,

    3.Drawing circle using circle(int x,int y,int radius)function where x,y are coordinate values.

    4. Drawing line using line(int x1, int y1, int x2, int y2)function where x1,y1,x2,y2 are line point

    coordinate values.

    5.Display the images and closegraph() which is used to close current opened graphic device.

  • 7/31/2019 CG Lab Manual(2012)

    17/95

    PROGRAM:

    #include

    #include#include

    void main()

    {int gd = DETECT, gm;

    initgraph(&gd,&gm,"");

    //2line(80,200,110,200);

    line(110,200,110,230);

    line(110,230,80,230);

    line(80,230,80,260);line(80,260,110,260);

    //0

    line(120,200,120,260);

    line(120,200,150,200);line(150,200,150,260);

    line(120,260,150,260);

    //0line(170,200,170,260);

    line(170,200,200,200);

    line(200,200,200,260);

    line(200,260,170,260);//5

    line(210,200,240,200);

    line(210,200,210,230);

    line(210,230,240,230);line(240,230,240,260);

    line(240,260,210,260);

    //1line(260,200,260,260);

    //9

    line(270,200,300,200);line(270,200,270,230);

    line(270,230,300,230);

    line(300,200,300,260);

    //9

    line(310,200,340,200);line(310,200,310,230);

    line(310,230,340,230);line(340,200,340,260);

    //1

    line(350,200,350,260);getch();

    closegraph();

    }

  • 7/31/2019 CG Lab Manual(2012)

    18/95

    OUTPUT:

    RESULT:

    Thus, the program executed successfully and output was obtained.

  • 7/31/2019 CG Lab Manual(2012)

    19/95

    Exno: 6 Dream house using Built in functions

    __________________________________________________________________________

    Aim:

    To write a c graphics program and to display dream house using built in functions.

    Algorithm:

    1. Detect graphics device and graphic mode, and initialize graphic systemusing Initgraph().

    2. Display dream house using built in functions such as,

    3.Drawing circle using circle(int x,int y,int radius)function where x,y are coordinate values.

    4. Drawing line using line(int x1, int y1, int x2, int y2)function where x1,y1,x2,y2 are line point

    coordinate values.

    5. Drawing polygon using draypoly(int number of points, int polypoints)function.

    6.Display the images and closegraph() which is used to close current opened graphic device.

  • 7/31/2019 CG Lab Manual(2012)

    20/95

    PROGRAM:

    #include

    #include

    #include #include #include

    void main(){

    int gdriver, gmode;gdriver = DETECT;

    int col=BROWN;

    initgraph(&gdriver, &gmode, "c:\\Turboc3\\BGI\\Disk\\Turboc3\\BGI");

    int arr[106] = { 25,400,52,100,25,100,60,50,95,100,68,100,95,400,165,400,

    165,175,150,175,150,150,160,150,

    160,165,170,165,170,150,180,150,180,165,190,165,190,150,200,150,

    200,175,185,175,185,270,270,270,

    270,75,240,75,310,25,380,75,350,75,350,270,420,270,420,175,

    405,175,405,150,415,150,415,165,

    425,165,425,150,435,150,435,165,

    445,165,445,150,455,150,455,175,440,175,440,400,510,400,537,100,

    510,100,545,50,580,100,555,100,

    580,400 };

    setcolor(col);rectangle(0,0,getmaxx(),getmaxy());

    drawpoly(53,arr);

    line(0,400,getmaxx(),400);line(290,400,290,310);

    line(330,400,330,310);

    arc(310,310,0,180,20);circle(310,150,18);

    setfillstyle(1,col);

    floodfill(30,395,col);

    floodfill(520,395,col);

    floodfill(310,55,col);setfillstyle(1,GREEN);

    floodfill(100,440,col);getch();

    closegraph();

    }

  • 7/31/2019 CG Lab Manual(2012)

    21/95

    OUTPUT:

    RESULT:

    Thus, the program executed successfully and output was obtained.

  • 7/31/2019 CG Lab Manual(2012)

    22/95

    Exno:7 Display National flag using Built in functions

    __________________________________________________________________________

    Aim:

    To write a c graphics program and to display national flag using built in functions.

    Algorithm:

    1. Detect graphics device and graphic mode, and initialize graphic systemusing Initgraph().

    2. Display national flag using built in functions such as,

    3.Drawing circle using circle(int x,int y,int radius)function where x,y are coordinate values.

    4. Drawing line using line(int x1, int y1, int x2, int y2)function where x1,y1,x2,y2 are line point

    coordinate values.

    5. Drawing rectangle using rectangle(int left,int top,int right,int bottom)function.

    6. To fill color using,

    setcolor(WHITE);setfillstyle(int pattern,WHITE);

    floodfill(int x,int y,WHITE);

    7.Display the images and closegraph() which is used to close current opened graphic device.

  • 7/31/2019 CG Lab Manual(2012)

    23/95

    PROGRAM:

    #include

    #include void main()

    {

    int gdriver, gmode;gdriver = DETECT;

    initgraph(&gdriver, &gmode, "c:\\Turboc3\\BGI\\Disk\\Turboc3\\BGI");setcolor(RED);

    setcolor(RED);rectangle(150,100,300,140);

    setfillstyle(1,RED);

    floodfill(200,120,RED);

    setcolor(WHITE);

    rectangle(150,140,300,180);

    setfillstyle(1,WHITE);

    floodfill(200,150,WHITE);

    setcolor(GREEN);

    rectangle(150,180,300,220);setfillstyle(1,GREEN);

    floodfill(200,200,GREEN);

    setcolor(BROWN);rectangle(150,100,140,400);

    setfillstyle(1,BROWN);

    floodfill(145,140,BROWN);

    setcolor(BLUE);

    circle(225,160,20);

    line(225,140,225,180);line(230,141,220,179);

    line(235,142,215,178);

    line(238,145,212,176);line(242,150,208,169);

    line(244,155,206,165);

    line(205,160,245,160);

    line(244,166,205,156);

    line(245,171,207,150);line(239,175,211,146);

    line(236,176,214,143);line(232,179,219,142);

    getch();

    closegraph();}

  • 7/31/2019 CG Lab Manual(2012)

    24/95

    OUTPUT:

    RESULT:

    Thus, the program executed successfully and output was obtained.

  • 7/31/2019 CG Lab Manual(2012)

    25/95

    Exno:8 Display Ship on the river using Built in functions

    __________________________________________________________________________

    Aim:

    To write a c graphics program and to ship on the river using built in functions.

    Algorithm:

    1. Detect graphics device and graphic mode, and initialize graphic systemusing Initgraph().

    2. Display ship on the river using built in functions such as,

    3.Drawing circle using circle(int x,int y,int radius)function where x,y are coordinate values.

    4. Drawing line using line(int x1, int y1, int x2, int y2)function where x1,y1,x2,y2 are line point

    coordinate values.

    5. Drawing rectangle using rectangle(int left,int top,int right,int bottom)function.

    6. To fill color using,

    setcolor(WHITE);

    setfillstyle(int pattern,WHITE);

    floodfill(int x,int y,WHITE);

    7. Drawing ellipse using ellipse(int x, int y, int stangle, int end angle, int x radius, int y

    radius)function where x,y are coordinate values.

    8.Display the images and closegraph() which is used to close current opened graphic device.

  • 7/31/2019 CG Lab Manual(2012)

    26/95

    PROGRAM:

    #include

    #includevoid main()

    {

    int gdriver, gmode;gdriver = DETECT;

    initgraph(&gdriver, &gmode, "c:\\Turboc3\\BGI\\Disk\\Turboc3\\BGI");

    setcolor(BROWN);line(100,250,500,250);

    line(100,250,150,400);

    line(500,250,450,400);

    setcolor(RED);line(200,200,350,200);

    line(225,150,325,150);

    line(250,100,300,100);

    line(200,200,200,250);line(350,200,350,250);

    line(225,150,225,200);

    line(325,150,325,200);line(250,100,250,150);

    line(300,100,300,150);

    setcolor(11);line(400,250,400,100);

    line(450,250,450,100);

    line(400,100,450,100);

    //windowssetcolor(7);

    rectangle(130,260,150,280);

    rectangle(160,260,180,280);rectangle(190,260,210,280);

    rectangle(220,260,240,280);

    rectangle(250,260,270,280);

    rectangle(280,260,300,280);rectangle(310,260,330,280);

    rectangle(340,260,360,280);

    rectangle(370,260,390,280);rectangle(400,260,420,280);

    rectangle(430,260,450,280);

    rectangle(460,260,480,280);

    rectangle(140,290,160,310);

    rectangle(170,290,190,310);

    rectangle(200,290,220,310);rectangle(230,290,250,310);

    rectangle(260,290,280,310);

    rectangle(290,290,310,310);rectangle(320,290,340,310);

    rectangle(350,290,370,310);

    rectangle(380,290,400,310);

    rectangle(410,290,430,310);

  • 7/31/2019 CG Lab Manual(2012)

    27/95

    rectangle(440,290,460,310);

    rectangle(160,320,180,340);rectangle(190,320,210,340);

    rectangle(220,320,240,340);

    rectangle(250,320,270,340);

    rectangle(280,320,300,340);rectangle(310,320,330,340);

    rectangle(340,320,360,340);

    rectangle(370,320,390,340);rectangle(400,320,420,340);

    rectangle(430,320,450,340);

    rectangle(170,350,190,370);

    rectangle(200,350,220,370);

    rectangle(230,350,250,370);

    rectangle(260,350,280,370);rectangle(290,350,310,370);

    rectangle(320,350,340,370);

    rectangle(350,350,370,370);rectangle(380,350,400,370);rectangle(410,350,430,370);

    setcolor(3);ellipse(150,420,0,160,30,20);

    ellipse(190,420,0,160,30,20);

    ellipse(230,420,0,160,30,20);

    ellipse(270,420,0,160,30,20);ellipse(310,420,0,160,30,20);

    ellipse(350,420,0,160,30,20);

    ellipse(390,420,0,160,30,20);ellipse(430,420,0,160,30,20);

    ellipse(470,420,0,160,30,20);

    setcolor(7);

    setfillstyle(1,7);

    circle(445,60,15);

    floodfill(445,60,7);

    setcolor(7);

    setfillstyle(1,7);

    circle(420,85,10);floodfill(420,85,7);

    setcolor(7);

    setfillstyle(1,7);circle(420,30,20);

    floodfill(420,30,7);

    getch();closegraph();

    }

  • 7/31/2019 CG Lab Manual(2012)

    28/95

    OUTPUT:

    RESULT:

    Thus, the program executed successfully and output was obtained.

  • 7/31/2019 CG Lab Manual(2012)

    29/95

    Exno:9 DDA LINE DRAWING

    __________________________________________________________________________

    AIM

    To write a graphics program and to implement DDA line drawing algorithm to draw line.

    ALGORITHM

    1.Detect graphic derive and graphics mode to initialize the graphics system using initgraph().

    2.Declare the variables within detect graphics mode and init graph()

    3.Read the values x1,y1,x2,y2 respectively.

    4.Assign the values as dx=x2-x1,dy=y2-y1.

    5.Chech if(dx>dy)then step=dx else step=dy.

    6.Find out the value of next points for the line using xincr&yincr.

    7.Plot the pixel positions one by one from k=0 to step such printing the points of x y where

    x=x+incr&y=y+incr.

    8.Display the line & close graph () which is used to close current graphics mode initialized anddetected.

  • 7/31/2019 CG Lab Manual(2012)

    30/95

    PROGRAM:

    #include

    #include

    #include

    #include#define ro(a)((int)(a+0.5))

    void main()

    {int x1,x2,y1,y2,dx,dy,k,step;

    float xincr,yincr,x,y;

    int graphicdriver=DETECT,graphicmode;

    initgraph(&graphicdriver,&graphicmode," ");printf("\n\n\tDDA LINE DRAWING ALGORITHM");

    printf("\n\t----------------------------");

    printf("\n\n\nEnter the value of x1,y1,x2,y2:::");scanf("%d%d%d%d",&x1,&y1,&x2,&y2);

    x=x1,y=y1;

    dx=x2-x1;dy=y2-y1;

    if(abs(dx)>abs(dy))step=abs(dx);

    else

    step=abs(dy);xincr=dx/(float)step;

    yincr=dy/(float)step;

    putpixel(ro(x),ro(y),2);

    for(k=0;k

  • 7/31/2019 CG Lab Manual(2012)

    31/95

    OUTPUT:

    RESULT:

    Thus, the program executed successfully and output was obtained.

  • 7/31/2019 CG Lab Manual(2012)

    32/95

    Exno:10 Bressanhams line drawing algorithm

    __________________________________________________________________________

    Aim:

    To write a c graphics program and to implement Bressanhams line drawing algorithm.

    Algorithm:

    1. Detect graphics device and graphic mode, and initialize graphic systemusing Initgraph().

    2. Bressanhams line drawing algorithm such as,

    3.Input the twoline endpoints and store the left endpoint in (x0,y0)

    4.Load (x0, y0into the frame buffer; that is, plot the first point.

    5. Calculate constants x, y, 2y, and 2y2 , and obtain the starting

    value for the decision parameter as

    p0= 2y- X

    6. At each xkalong the line, starting at k= 0, perform the following test:

    IfPr < 0, the next point to plot is (Xk+ 1,yk) and

    Pk+1=P k+ Y

    Otherwise, the next point to plot is (xi + I ,yr+ 1) and

    pk+, =pk+ 2y- 2 x

    7. Repeat step 4 x times.

    8.Display the images and closegraph() which is used to close current opened graphic device.

  • 7/31/2019 CG Lab Manual(2012)

    33/95

    PROGRAM:

    #include #include

    #include #include

    void main(){

    int gdriver = DETECT, gmode;

    int x1,y1,x2,y2,dx,dy,x,y,xend,p;

    initgraph(&gdriver, &gmode, "");printf("\n\n\n\tBresanham line drawing algorithm");

    printf("\n\t-----------------------------");

    printf("\n\nEnter the value of x1,y1,x2,y2:::");

    scanf("%d%d%d%d",&x1,&y1,&x2,&y2);

    dx=abs(x1-x2), dy=abs(y1-y2),p=2*dy-dx;if(x1>x2)

    {x=x2;

    y=y2;

    xend=x1;}

    else

    {

    x=x1;y=y1;

    xend=x2;}

    putpixel(x,y,WHITE);

    while(x

  • 7/31/2019 CG Lab Manual(2012)

    34/95

    OUTPUT:

    RESULT:

    Thus, the program executed successfully and output was obtained.

  • 7/31/2019 CG Lab Manual(2012)

    35/95

    Exno:11 Bressanhams mid point circle generating algorithm

    __________________________________________________________________________

    Aim:

    To write a c graphics program and to implement Bressanhams mid point circle generatingalgorithm.

    Algorithm:

    1. Detect graphics device and graphic mode, and initialize graphic systemusing Initgraph().

    2. Bressanhams mid point circle generating algorithm such as,

    3.Input radius r and circle center (x, y,), and obtain the first point on the circumference of a circle

    centered on the origin as

    (x0,y0)=(0,r)

    4. calculate the initial value of the decision parameter as

    P0=5/4-r

    5. Ateach xk position, starting at k = 0, perform the following test: If Pk=y.

    9.Display the images and closegraph() which is used to close current opened graphic device.

  • 7/31/2019 CG Lab Manual(2012)

    36/95

    PROGRAM:

    #include

    #include

    #include#include

    void circlepoints(int xcenter,int ycenter,int x,int y)

    {

    putpixel(xcenter+x,ycenter+y,WHITE);putpixel(xcenter-x,ycenter+y,WHITE);

    putpixel(xcenter+x,ycenter-y,WHITE);

    putpixel(xcenter-x,ycenter-y,WHITE);putpixel(xcenter+y,ycenter+x,WHITE);

    putpixel(xcenter-y,ycenter+x,WHITE);

    putpixel(xcenter+y,ycenter-x,WHITE);

    putpixel(xcenter-y,ycenter-x,WHITE);}

    void circle1(int xcenter,int ycenter,int radius)

    {int x=0,y=radius,p=1-radius;

    circlepoints(xcenter, ycenter,x,y);

    while( x < y){

    if(p

  • 7/31/2019 CG Lab Manual(2012)

    37/95

    OUTPUT:

    RESULT:

    Thus, the program executed successfully and output was obtained.

  • 7/31/2019 CG Lab Manual(2012)

    38/95

    Exno:12 Ellipse generating algorithm

    __________________________________________________________________________

    Aim:

    To write a c graphics program and to implement Ellipse generatingalgorithm.

    Algorithm:

    1. Detect graphics device and graphic mode, and initialize graphic systemusing Initgraph().

    2. Ellipse generating algorithm such as,

    3.Input rx, ry, and ellipse center (xc, yc,), and obtain the first point on an ellipse centered on the origin

    as (x0,y0)=(0,ry)

    4.Calculate the initial value of the decision parameter in region as

    P10=ry^2 1-rx^2ry+ rx^2

    5. At each x, position in region 1, starting at k= 0, perform the following test:If p1k< 0, the next point along the ellipse centered on (0, 0) is (Xk+1,yk) and

    P1k+1 = p1k + 2ry^2xk+1 + ry^2

    Otherwise, the next point along the circle is (xk + 1, yk-1) and

    P1k+1 = p1k + 2ry^2xk+1 - 2rx^2yk+1 +ry^2

    With

    2ry^2xk+1 = 2ry^2xk + 2ry^2,

    2ry^2xyk+1 = 2rx^2yk + 2rx^2

    and continue until 2ry^2X >= 2rx^2y.

    6. Calculatethe initial value of the decision parameter in region 2 using the last point (xo, yo)

    calculated in region 1 as

    P20= ry^2(x0+ 1/2) + rx^2 (y0-1)^2 - ry^2rX^2

    7. At each yk position in region 2, starting at k= 0, perform the following test:

    Ifp2k> 0, the next point along the ellipse centered on (0, 0) is (xk+1,yk-1) and

    P2k+1=p2k-2rx^2yK+1 + rx^2

  • 7/31/2019 CG Lab Manual(2012)

    39/95

    Otherwise, the next point along the circle is(Xk+ 1, yk - 1) and

    P2k+1 = p2k + 2ry^2Xk-1 - 2rx^2yk-1 + r^2

    using the same incremental calculations for X and Y as in region 1.

    8.Determine symmetry points in the other three quadrants.

    7. Move each calculated pixel position (x, y) onto the elliptical path centered on (x,, y,) and plot thecoordinate values:

    x=x+xc,

    y=y+yc

    8. Repeat the steps for region 1 until 2ry^2x >= 2rx^2y

    9.Display the images and closegraph() which is used to close current opened graphic device.

  • 7/31/2019 CG Lab Manual(2012)

    40/95

    PROGRAM:

    #include#include

    #include

    #define round(a)((int)(a+0.5))

    void ellip(int xcen,int ycen,int x,int y)

    {

    putpixel(xcen+x,ycen+y,2);putpixel(xcen-x,ycen+y,2);

    putpixel(xcen+x,ycen-y,2);

    putpixel(xcen-x,ycen-y,2);

    }

    void main()

    {int gd=DETECT,gm;

    int xcen,ycen,rx,ry;

    int rx1,ry1,rx2,ry2;int p,x,y,px,py;

    void ellip(int,int,int,int);

    initgraph(&gd,&gm,"");coutry;

    coutxcen>>ycen;rx1=rx*rx,ry1=ry*ry,rx2=2*rx1,ry2=2*ry1;

    x=0,y=ry,px=0,py=rx2*y;

    ellip(xcen,ycen,x,y);p=round(ry1-(rx1*ry)+(0.25*rx1));

    while(px

  • 7/31/2019 CG Lab Manual(2012)

    41/95

    while(y>0){

    y--;

    py-=rx2;

    if(p>0)p+=rx1-py;

    else

    {x++;

    px+=ry2;

    p+=rx1-py+px;}

    ellip(xcen,ycen,x,y);

    }

    getch();closegraph();

    }

  • 7/31/2019 CG Lab Manual(2012)

    42/95

    OUTPUT:

    RESULT:

    Thus, the program executed successfully and output was obtained.

  • 7/31/2019 CG Lab Manual(2012)

    43/95

    Exno:13 2D transformation

    __________________________________________________________________________

    AIM:

    To write a program in C to implement 2DTransformation on an object.

    ALGORITHM:

    1. Enter the choice for transformation.

    2. Perform the translation, rotation, scaling, reflection and shearing of 2D object.

    3. Get the needed parameters for the transformation from the user.

    4. Incase of rotation, object can be rotated about x or y axis.

    5. Display the transmitted object in the screen.

  • 7/31/2019 CG Lab Manual(2012)

    44/95

    PROGRAM:

    #include

    #include#include

    #include

    void disp(int n,float c[][3]){

    float maxx,maxy;

    int i;maxx=getmaxx();

    maxy=getmaxy();

    maxx=maxx/2;

    maxy=maxy/2;i=0;

    while(i

  • 7/31/2019 CG Lab Manual(2012)

    45/95

    int i=0,j;for(i=0;i

  • 7/31/2019 CG Lab Manual(2012)

    46/95

    b[i][j]=1;}

    switch(ch){

    case 1:

    b[1][1]=-1;

    break;case 2:

    b[0][0]=-1;

    break;case 3:

    b[0][0]=-1;

    b[1][1]=-1;break;

    case 4:

    b[0][0]=0;

    b[1][1]=0;b[0][1]=1;

    b[1][0]=1;

    break;case 5:

    b[0][0]=0;

    b[1][1]=0;

    b[0][1]=-1;b[1][0]=-1;

    break;

    case 6:

    break;default:

    printf("\n\tINVALID CHOICE ! ");

    break;}

    mul(n,b,c,a);

    setcolor(RED);disp(n,a);

    }

    void shearing(int n,float c[][3])

    {float b[10][3],sh,a[10][3];

    int i=0,ch,j;

    cleardevice();

    printf("\n\t* * * MENU * * *");printf("\n\t1) X SHEARING");

    printf("\n\t2) Y SHEARING");

    printf("\n\t3) EXIT ");printf("\n\tENTER YOUR CHOICE : ");

    scanf("%d",&ch);

    if(ch==3)return;

    printf("\n\tENTER THE VALUE FOR SHEARING: ");

    scanf("%f",&sh);clrscr();

  • 7/31/2019 CG Lab Manual(2012)

    47/95

    cleardevice();for(i=0;i

  • 7/31/2019 CG Lab Manual(2012)

    48/95

    case 1:printf("\n\tEnter translation factor for X & Y axis :\t");

    scanf("%f%f",&tx,&ty);clrscr();

    cleardevice();

    setcolor(BLUE);

    disp(n,c);translation(n,c,tx,ty);

    setcolor(RED);

    disp(n,c);getch(); break;

    case 2:

    printf("\n\tEnter scaling factor for X & Y axis :\t");scanf("%f%f",&sx,&sy);

    clrscr();

    cleardevice();

    setcolor(BLUE);disp(n,c);

    scaling(n,c,sx,sy);

    getch(); break;case 3:

    printf("\n\n\tEnter the angle of rotation:\t");

    scanf("%f",&ra);

    clrscr();cleardevice();

    setcolor(BLUE);

    disp(n,c);

    rotation(n,c,ra);getch();

    break;

    case 4:clrscr();

    cleardevice();

    setcolor(BLUE);disp(n,c);

    reflection(n,c);

    getch(); break;

    case 5:clrscr();

    cleardevice();

    setcolor(BLUE);

    disp(n,c);shearing(n,c);

    getch(); break;

    case 6 :exit();

    break;

    default: printf("\n\tInvalid choice !!"); break;}

    }while(cho!=6);

    getch();closegraph();}

  • 7/31/2019 CG Lab Manual(2012)

    49/95

    OUTPUT

  • 7/31/2019 CG Lab Manual(2012)

    50/95

  • 7/31/2019 CG Lab Manual(2012)

    51/95

  • 7/31/2019 CG Lab Manual(2012)

    52/95

  • 7/31/2019 CG Lab Manual(2012)

    53/95

  • 7/31/2019 CG Lab Manual(2012)

    54/95

  • 7/31/2019 CG Lab Manual(2012)

    55/95

  • 7/31/2019 CG Lab Manual(2012)

    56/95

  • 7/31/2019 CG Lab Manual(2012)

    57/95

  • 7/31/2019 CG Lab Manual(2012)

    58/95

  • 7/31/2019 CG Lab Manual(2012)

    59/95

    RESULT:

    Thus, the program executed successfully and output was obtained.

  • 7/31/2019 CG Lab Manual(2012)

    60/95

    Exno:14 Implementing motion of the object using algorithm

    __________________________________________________________________________

    AIM:

    To write a program in C to implement 2DTransformation on an object using algorithm.

    ALGORITHM:

    1. importing DDA line drawing algorithm

    2. importing Bresenham circle generating line drawing algorithm

    3. Perform the translation 2D object.

    4. Get the needed parameters for the transformation from the user.

    5. use for loop for motion

    6. Read the translation vector value and perform motion on transformation object.

  • 7/31/2019 CG Lab Manual(2012)

    61/95

    PROGRAM:

    #include#include

    #include

    #include#define ro(a)((int)(a+0.5))

    void circlepoints(int xcenter,int ycenter,int x,int y);

    void circle1(int xcenter,int ycenter,int radius);int size,*bitmap;

    void circlepoints(int xcenter,int ycenter,int x,int y)

    {

    putpixel(xcenter+x,ycenter+y,WHITE);putpixel(xcenter-x,ycenter+y,WHITE);

    putpixel(xcenter+x,ycenter-y,WHITE);

    putpixel(xcenter-x,ycenter-y,WHITE);

    putpixel(xcenter+y,ycenter+x,WHITE);putpixel(xcenter-y,ycenter+x,WHITE);

    putpixel(xcenter+y,ycenter-x,WHITE);

    putpixel(xcenter-y,ycenter-x,WHITE);}

    void circle1(int xcenter,int ycenter,int radius)

    {int x=0;

    int y=radius;

    int p=1-radius;

    circlepoints(xcenter, ycenter,x,y);while( x < y)

    {

    if(p

  • 7/31/2019 CG Lab Manual(2012)

    62/95

    if(abs(dx)>abs(dy))step=abs(dx);

    elsestep=abs(dy);

    xincr=dx/(float)step;

    yincr=dy/(float)step;

    putpixel(ro(x),ro(y),2);for(k=0;k350)

    i=0;

    putimage(i,130,bitmap,0);delay(100);

    cleardevice();

    }}

    void main()

    {int op,xa,ya,xb,yb,xc,yc,tx,ty,sx,sy,angle,i,j,size,*bitmap,xcen,ycen,r;

  • 7/31/2019 CG Lab Manual(2012)

    63/95

    int gd=DETECT,gm;initgraph(&gd,&gm,"");

    tx=0,ty=0;car(tx,ty);

    ani();

    printf("\nEnter the tx and ty values-->");

    scanf("%d%d",&tx,&ty);car(tx,ty);

    //ani();

    for(i=0;!kbhit();i=i+10){

    car(tx+i,ty);

    delay(100);cleardevice();

    }

    getch();

    closegraph();}

  • 7/31/2019 CG Lab Manual(2012)

    64/95

    OUTPUT:

  • 7/31/2019 CG Lab Manual(2012)

    65/95

  • 7/31/2019 CG Lab Manual(2012)

    66/95

  • 7/31/2019 CG Lab Manual(2012)

    67/95

    RESULT:

    Thus, the program executed successfully and output was obtained.

  • 7/31/2019 CG Lab Manual(2012)

    68/95

    Exno:15 Cohen Sutherland line clipping algorithm

    __________________________________________________________________________

    AIM:

    To write a program in C to implement Cohen-Sutherland line clipping algorithm .

    ALGORITHM:

    1 : Given a line segment with endpoint p1=(x1,y1) and p2=(x2,y2)

    2 : Compute the 4-bit codes for each endpoint.

    3 : If both codes are 0000,(bitwise OR of the codes yields 0000 ) line lies

    completely inside the window: pass the endpoints to the draw routine.

    4 : If both codes have a 1 in the same bit position (bitwise AND of the codes

    is not 0000), the line lies outside the window. It can be trivially rejected.

    5 : If a line cannot be trivially accepted or rejected, at least one of the two

    endpoints must lie outside the window and the line segment crosses a

    window edge. This line must be clipped at the window edge before being

    passed to the drawing routine.

    6 : Examine one of the endpoints, say P1=(X1,Y1). Read P1 's 4-bit code in

    order: Left-to-Right, Bottom-to-Top.

    7 : When a set bit (1) is found, compute the intersection I of the

    corresponding window edge with the line from P1 to P2.

    Replace P1 with I and repeat the algorithm.

  • 7/31/2019 CG Lab Manual(2012)

    69/95

    PROGRAM:

    #include#include

    #include

    int xa,xb,ya,yb;int encode(float x,float y)

    {

    return(8*(xxb)+2*(yyb));}

    void swap(int *a,int *b)

    {

    int *t=a;a=b;

    b=t;

    }

    void lineclip(int x1,int y1,int x2,int y2){

    float dx,dy;

    int c1,c2;c1=encode(x1,y1);

    c2=encode(x2,y2);

    while(c1|c2){

    if(c1&c2)

    return;

    dx=x2-x1;dy=y2-y1;

    if(c1)

    {if(c1&8)

    {

    y1+=dy*(xa-x1)/dx;

    x1=xa;}

    else if(c1&4)

    {y1+=dx*(xb-x1)/dx;

    x1=xb;

    }

    else if(c1&2){

    x1+=dx*(ya-y1)/dy;

    y1=ya;}

    else if(c1&1)

    {x1+=dx*(yb-y1)/dy;

    y1=yb;

    }

    c1=encode(x1,y1);

  • 7/31/2019 CG Lab Manual(2012)

    70/95

    }

    else{

    if(c2&8)

    {

    y2+=dy*(xa-x2)/dx;x2=xa;

    }

    else if(c2&4){

    y2+=dy*(xb-xa)/dx;

    x2=xb;}

    else if(c2&2)

    {

    x2+=dx*(yb-y2)/dy;y2=ya;

    }

    else if(c2&1){

    x2+=dx*(yb-y2)/dy;

    y2=yb;}

    c2=encode(x2,y2);

    }

    }line(x1,y1,x2,y2);

    }

    void main(){

    int gd=DETECT,gm,x1,y1,x2,y2;

    initgraph(&gd,&gm,"");textbackground(0);

    setbkcolor(1);

    coutya>>xb>>yb;

    coutx1>>y1;

    coutx2>>y2;

    if(x1>x2)

    swap(&x1,&x2);if(y1>y2)

    swap(&y1,&y2);

    cleardevice();setcolor(13);

    outtextxy(50,50,"Before clipping");

    rectangle(xa,ya,xb,yb);line(x1,y1,x2,y2);

  • 7/31/2019 CG Lab Manual(2012)

    71/95

    getch();cleardevice();

    outtextxy(50,50,"After clipping");rectangle(xa,ya,xb,yb);

    lineclip(x1,y1,x2,y2);

    getch();

    closegraph();}

  • 7/31/2019 CG Lab Manual(2012)

    72/95

    OUTPUT:

    RESULT:

    Thus, the program executed successfully and output was obtained.

  • 7/31/2019 CG Lab Manual(2012)

    73/95

    Exno:16 Liang barsky line clipping algorithm

    __________________________________________________________________________

    AIM:

    To write a program for line clipping using Liang baskey line clipping algorithm.

    ALGORITHM:

    1.Define the structure and name as p and their necessary variable.

    2.Under the main function declare the necessary variables.

    3.Get the values for window coordinates and point values.

    4.Then go to clip line function then check the values (dx,wp.x-p1.x&u1,u2)

    5.Then assign their values as p1. x=p1.x +u1*dx,p1.y=p1.y+u1*dy.

    6.Under the clip test function check whether (p0.0)then do the assignment.

    8.Return the v values.

    9.Gisplay the clipping window.

  • 7/31/2019 CG Lab Manual(2012)

    74/95

    PROGRAM:

    #include#include

    #include

    struct p{

    int x,y;

    };typedef struct p point;

    int cliptest(float,float,float *,float *);

    void clipline(point,point,point,point);

    void main(){

    int gd=DETECT,gm;

    point wp1,wp2,p1,p2;

    initgraph(&gd,&gm,"");textbackground(0);

    setbkcolor(1);

    printf("\n\nLiang barsey line clipping algorithm\n-------------------------------");printf("\n\n\nEnter 4 values of clipping area::");

    scanf("%d%d%d%d",&wp1.x,&wp1.y,&wp2.x,&wp2.y);

    printf("\n\nEnter the first-second co-ordinate value::");scanf("%d%d%d%d",&p1.x,&p1.y,&p2.x,&p2.y);

    printf("\n\nBEFORE CIPPING");

    rectangle(wp1.x,wp1.y,wp2.x,wp2.y);

    line(p1.x,p1.y,p2.x,p2.y);getch();

    clipline(wp1,wp2,p1,p2);

    getch();closegraph();

    }

    void clipline(point wp1,point wp2,point p1,point p2)

    {float u1=0.0,u2=1.0,dx=p2.x-p1.x,dy=p2.y-p1.y;

    if(cliptest(-dx,p1.x-wp1.x,&u1,&u2))

    if(cliptest(dx,wp2.x-p1.x,&u1,&u2)){

    if(cliptest(-dy,p1.y-wp1.y,&u1,&u2))

    if(cliptest(dy,wp2.y-p1.y,&u1,&u2))

    {if(u10.0){

    p1.x=p1.x+u1*dx;

    p1.y=p1.x+u1*dy;

    }

  • 7/31/2019 CG Lab Manual(2012)

    75/95

    }cleardevice();

    outtextxy(50,50,"ATER CLIPPING");rectangle(wp1.x,wp1.y,wp2.x,wp2.y);

    line(p1.x,p1.y,p2.x,p2.y);

    getch();

    }}

    int cliptest(float p,float q,float *u1,float *u2)

    {float r;

    int v=1;

    if(p*u1)

    *u1=r;}

    if(p>0.0)

    {r=q/p;if(r

  • 7/31/2019 CG Lab Manual(2012)

    76/95

    OUTPUT:

    RESULT:

    Thus, the program executed successfully and output was obtained.

  • 7/31/2019 CG Lab Manual(2012)

    77/95

    Exno:17 Text clipping

    __________________________________________________________________________

    AIM: To write a program for line clipping using text clipping algorithm.

    ALGORITHM:

    1.To initialize graphic drive & graphic mode using DETECT graph and initgraph().

    2.Reading the window coordinates from the user.

    3.Reading the text coordinates from the user.

    4.Display the string 1,string 2,string 3,string 4 on the window before clipping.

    5.Get the values from the user.

    6.Check the values whether it display inside the window coordinates.

    7.Close graph() which is used to open the current graphics mode.

  • 7/31/2019 CG Lab Manual(2012)

    78/95

    PORGRAM:

    #include

    #include#include

    #include

    void show_clipped_text(int,int,char *,int,int,int);char key=NULL;

    int flag=0,x_min,y_min,x_max,y_max,width,height,dx,dy,inc_dec;

    int color,x1,y1,x2,y2;int main()

    {

    int gd=DETECT,gm;

    initgraph(&gd,&gm,"");setcolor(15);

    printf("\n\t\tEnter the value of x-min,y-min,x-max,y-max:");

    scanf("%d%d%d%d",&x_min,&y_min,&x_max,&y_max);

    rectangle(x_min,y_min,x_max,y_max);settextstyle(0,0,2);

    outtextxy(160,160,"String 1");

    outtextxy(250,300,"String 2");settextstyle(0,1,2);

    outtextxy(350,120,"String 3");

    outtextxy(440,180,"String 4");outtextxy(100,300,"Press C to See The Clipped");

    do

    {

    key=getch();}

    while(key!='C'&& key!='c');

    settextstyle(0,0,1);setcolor(0);

    setcolor(12);

    cleardevice();

    outtextxy(100,100,"After Clipping");outtextxy(213,350,"Press Key to Exit:");

    setcolor(10);

    rectangle(x_min,y_min,x_max,y_max);show_clipped_text(160,160,"String 1",0,0,2);

    show_clipped_text(250,300,"String 2",0,0,2);

    show_clipped_text(350,120,"String 3",0,1,2);

    show_clipped_text(440,180,"String 4",0,1,2);getch();

    return 0;

    }void show_clipped_text(int x,int y,char *string,int front_style,int text_direction,int front_size)

    {

    settextstyle(front_style,text_direction,front_size);flag=0;

    width=textwidth(string);

    height=textheight(string);

    if(text_direction==0)

  • 7/31/2019 CG Lab Manual(2012)

    79/95

    {if(x>=x_min&&y>=y_min&&(x+width)=y_min&&(x+height)

  • 7/31/2019 CG Lab Manual(2012)

    80/95

    OUTPUT:

    RESULT:

    Thus, the program executed successfully and output was obtained.

  • 7/31/2019 CG Lab Manual(2012)

    81/95

    Exno:18 Polygon clipping

    __________________________________________________________________________

    AIM:

    To write a program for polygon clipping algorithm.

    ALGORITHM:

    1.Declare the necessary variables.

    2.Get the number of coordinate window from the user.

    3.Check whether if(!( out code 0|outcode 1))and accept =TRUE otherwise done=TRUE.

    4.If the out code is in the top assign y=ymax.

    5.If the out code is in Right then y=xmax.

    6.Otherwise x=xmax and clip the polygon.

    7.Display the clipping of polygon.

  • 7/31/2019 CG Lab Manual(2012)

    82/95

    PROGRAM:

    #include

    #include

    #include#include

    #define TRUE 1

    #define FALSE 0

    typedef unsigned int outcode;

    outcode CompOutcode(float x,float y);

    enum{TOP=0x1,BOTTOM=0x2,RIGHT=0x4,LEFT=0x8};float xmin,xmax,ymin,ymax;

    void clip(float x0,float y0,float x1,float y1)

    {

    outcode outcode0,outcode1,outcodeOut;int accept=FALSE,done=FALSE;

    outcode0=CompOutcode(x0,y0);

    outcode1=CompOutcode(x1,y1);do

    {

    if(!(outcode0|outcode1)){

    accept=TRUE;

    done=TRUE;}

    else if(outcode0 & outcode1)done=TRUE;

    else

    {float x,y;

    outcodeOut=outcode0?outcode0:outcode1;

    if(outcodeOut & TOP){

    x=x0+(x1-x0)*(ymax-y0)/(y1-y0);

    y=ymax;}

    else if(outcodeOut & BOTTOM)

    {

    x=x0+(x1-x0)*(ymin-y0)/(y1-y0);y=ymin;

    }

    else if(outcodeOut & RIGHT)

    {y=y0+(y1-y0)*(xmax-x0)/(x1-x0);

    x=xmax;

    }

  • 7/31/2019 CG Lab Manual(2012)

    83/95

    else{

    y=y0+(y1-y0)*(xmin-x0)/(x1-x0);x=xmin;

    }

    if(outcodeOut==outcode0)

    {x0=x;

    y0=y;

    outcode0=CompOutcode(x0,y0);}

    else

    {x1=x;

    y1=y;

    outcode1=CompOutcode(x1,y1);

    }}

    }

    while(done==FALSE);if(accept)line(x0,y0,x1,y1);

    outtextxy(150,50,"After using polygon clipping");

    rectangle(xmin,ymin,xmax,ymax);}

    outcode CompOutcode(float x,float y)

    {

    outcode code=0;if(y>ymax)

    code|=TOP;

    else if(yxmax)

    code|=RIGHT;else if(x

  • 7/31/2019 CG Lab Manual(2012)

    84/95

    for(i=0;i

  • 7/31/2019 CG Lab Manual(2012)

    85/95

    OUTPUT:

    RESULT:

    Thus, the program executed successfully and output was obtained.

  • 7/31/2019 CG Lab Manual(2012)

    86/95

    Exno:19 Window to viewport Transformation

    __________________________________________________________________________

    AIM: To write a program for window to viewport.

    ALGORITHM:

    1. To initialize graphic drive & graphic mode using DETECT graph and initgraph().

    2. Reading the window coordinates from the user.

    3.Reading the values of viewport coordinates values from the user.

    4.Read the object values from the user to display in viewport.

    5.Since the scaling factor is calculated using certain functions.

    6.Porting to the view and displaying the object.

    7.Closegraph() which is used to open the current graphic mode.

  • 7/31/2019 CG Lab Manual(2012)

    87/95

    PROGRAM:

    #include

    #include#include

    #include

    void main(){

    float xwmin,xwmax,ywmin,ywmax;

    float xvmin,xvmax,yvmin,yvmax;float x[10],y[10],yv,xv,sx,sy;

    int gd=DETECT,gm,i;

    clrscr();

    printf("\n\n\t\tWindow to Viewport ");printf("\n\nEnter window-port coordinates (xwmin,ywmin,xwmax,ywmax)::");

    scanf("%f%f%f%f",&xwmin,&ywmin,&xwmax,&ywmax);

    printf("\nEnter view-port coordinates(xvmin,yvmin,xvmax,yvmax)::");

    scanf("%f%f%f%f",&xvmin,&yvmin,&xvmax,&yvmax);printf("\nEnter the vertices for triangle:");

    for(i=0;i

  • 7/31/2019 CG Lab Manual(2012)

    88/95

    OUTPUT:

  • 7/31/2019 CG Lab Manual(2012)

    89/95

    RESULT:

    Thus, the program executed successfully and output was obtained.

  • 7/31/2019 CG Lab Manual(2012)

    90/95

    Exno:20 3d transformation

    __________________________________________________________________________

    AIM:

    To write a program in C to implement 3DTransformation on an object.

    ALGORITHM:

    1: Enter the choice for transformation.

    2: Perform the translation, rotation and scaling of 3D object.

    3: Get the needed parameters for the transformation from the user.

    4: Incase of rotation, object can be rotated about x or y axis.

    5: Display the transmitted object in the screen.

  • 7/31/2019 CG Lab Manual(2012)

    91/95

    PORGRAM:

    #include

    #include#include

    int i,j;

    void tcube()

    {for(i=1;i

  • 7/31/2019 CG Lab Manual(2012)

    92/95

    delay(200);j=j+2;

    }outtextxy(250,50,"*After Rotation*");

    getch();

    cleardevice();

    }void scube()

    {

    for(i=1;i

  • 7/31/2019 CG Lab Manual(2012)

    93/95

    OUTPUT:

  • 7/31/2019 CG Lab Manual(2012)

    94/95

  • 7/31/2019 CG Lab Manual(2012)

    95/95