cg journal

Upload: sadhanamca1

Post on 03-Jun-2018

222 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/12/2019 CG Journal

    1/49

    Ashish M. Gautam Application Id: 14131000229

    1

    Program No: 1

    Aim: Write a C Program to implement the DDA Line Drawing Algorithm to draw a line.

    Program:

    #include#include#include

    #include

    int xa, ya, xb, yb, step, dx, dy;float xi, yi, x, y;

    int cx, cy;

    void getdata();

    void getdata(){

    clrscr();

    printf("Enter Starting Co-Ordinate : ");scanf("%d",&xa);

    scanf("%d",&ya);

    printf("Enter Ending Co-Ordinate : ");scanf("%d",&xb);

    scanf("%d",&yb);

    }

    void dispdata()

    {

    int i;dx = xb - xa;

    dy = yb - ya;

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

    else

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

    yi = (float)dy/step;

    x = xa;

    y = ya;cx = x + 0.5;

    cy= y+0.5;

    putpixel(cx,cy,BLUE);

    for(i=1; i

  • 8/12/2019 CG Journal

    2/49

    Ashish M. Gautam Application Id: 14131000229

    2

    cy= y+0.5;

    putpixel(cx,cy,BLUE);

    }getch();

    }

    void main(){

    int gd=DETECT, gm, errorcode;

    clrscr();getdata();

    clrscr();

    initgraph(&gd, &gm, "D:\\TC\\BGI");

    errorcode = graphresult();

    if (errorcode != grOk)

    {

    printf("Graphics Error:");

    printf(grapherrormsg(errorcode));printf("\n");

    printf("Press any key to halt");

    getch();exit(1);

    }

    dispdata();

    }

    Output:

    Enter Starting Co-Ordinate: 100

    100

    Enter Ending Co-Ordinate: 200200

  • 8/12/2019 CG Journal

    3/49

    Ashish M. Gautam Application Id: 14131000229

    3

    Program No: 2

    Aim: Write a C Program to implement the DDA Line rasterisation of two end points.

    Program:

    #include#include#include

    int xa, ya, xb, yb, step, dx, dy;float xi, yi, x, y;

    int cx, cy;

    void getdata();

    void getdata()

    {

    clrscr();printf("Enter Starting Co-Ordinate : ");

    scanf("%d",&xa);

    scanf("%d",&ya);

    printf("Enter Ending Co-Ordinate : ");

    scanf("%d",&xb);

    scanf("%d",&yb);}

    void dispdata(){

    int i;

    dx = xb - xa;dy = yb - ya;

    if (abs(dx) > abs(dy))

    step = abs(dx);else

    step = abs(dy);

    printf("\n\tstep = %d",step);

    xi = (float)dx/step;

    yi = (float)dy/step;printf("\n\txi = %.3f",xi);

    printf("\n\tyi = %.3f",yi);

  • 8/12/2019 CG Journal

    4/49

    Ashish M. Gautam Application Id: 14131000229

    4

    x = xa;

    y = ya;

    printf("\n\n");

    printf("i \t Plotted(x,y) \t x=(x+xi) \t y=(y+yi)");printf("\n==========================================\n");cx = x+0.5; //round to nearest integer value

    cy= y+0.5;

    printf("0 \t ( %d, %d)",cx,cy);

    for(i=1; i

  • 8/12/2019 CG Journal

    5/49

    Ashish M. Gautam Application Id: 14131000229

    5

    Output:

  • 8/12/2019 CG Journal

    6/49

    Ashish M. Gautam Application Id: 14131000229

    6

    Program No: 3

    Aim: Write a C Program to Implement the Bresenhams Line Algorithm to draw a line.

    Program:

    #include#include#include

    #include

    int xa, ya, xb, yb;

    int dx,dy,sx,sy, x, y;

    int steps, flag, p;

    void getdata()

    {

    clrscr();printf("Enter Starting Co-Ordinate : ");

    scanf("%d",&xa);

    scanf("%d",&ya);

    printf("Enter Ending Co-Ordinate : ");

    scanf("%d",&xb);

    scanf("%d",&yb);}

    void dispdata()

    {int i,t;

    dx = abs(xb-xa);dy = abs(yb-ya);

    if ((xb-xa) > 0)sx = +1;

    else if((xb-xa)==0)

    sx = 0;

    elsesx = -1;

    if ((yb-ya) > 0)

    sy = +1;else if((yb-ya)==0)

    sy = 0;

    elsesy = -1;

  • 8/12/2019 CG Journal

    7/49

  • 8/12/2019 CG Journal

    8/49

  • 8/12/2019 CG Journal

    9/49

    Ashish M. Gautam Application Id: 14131000229

    9

    Program No: 4

    Aim: Write a C Program to Implement the Midpoint Circle Drawing Algorithm to draw a

    circle.

    Program:#include#include

    #include

    #include

    int xc, yc, r, x, y;

    float p;

    void dispcircle(int,int,int,int);

    void getdata()

    { clrscr();

    printf("Enter the center of the circle : ");

    scanf("%d %d", &xc,&yc);

    printf("Enter the radius of the circle : ");scanf("%d",&r);

    getch();

    }

    void dispdata()

    {

    x = 0;y = r;

    p = 1.25 - r;

    dispcircle(xc,yc,x,y);while(x < y)

    {

    x = x + 1;if ( p < 0)

    p = p + 2*x + 1;

    else

    {y = y - 1;

    p = p + 2*x - 2*y + 1;

    }

    dispcircle(xc,yc,x,y);}

    getch();

    }

  • 8/12/2019 CG Journal

    10/49

    Ashish M. Gautam Application Id: 14131000229

    10

    void dispcircle(int xc, int yc,int x,int y)

    {

    putpixel(xc+x,yc+y,BLUE);putpixel(xc+x,yc-y,BLUE);

    putpixel(xc-x,yc+y,BLUE);

    putpixel(xc-x,yc-y,BLUE);putpixel(xc+y,yc+x,BLUE);putpixel(xc+y,yc-x,BLUE);

    putpixel(xc-y,yc+x,BLUE);

    putpixel(xc-y,yc-x,BLUE);}

    void main()

    {int gd=DETECT, gm, errorcode;

    clrscr();

    getdata();clrscr();

    initgraph(&gd, &gm, "C:\\TC\\BGI");

    errorcode = graphresult();

    if (errorcode != grOk){

    printf("Graphics Error:");

    printf(grapherrormsg(errorcode));printf("\n");

    printf("Press any key to halt");

    getch();

    exit(1);}

    dispdata();

    closegraph();}

    Output:

  • 8/12/2019 CG Journal

    11/49

    Ashish M. Gautam Application Id: 14131000229

    11

    Program No: 5

    Aim: Write a C Program to implement the rasterization of Midpoint Circle Drawing

    Algorithm.

    Program:#include#include

    #include

    int xc, yc, r, x, y, p;

    void getdata()

    {clrscr();

    printf("Enter the center of the circle : ");

    scanf("%d %d", &xc,&yc);printf("Enter the radius of the circle : ");

    scanf("%d",&r);

    getch();

    clrscr();}

    void dispdata(){

    int k = 0;

    x = 0;

    y = r;p = 1 - r;

    printf("Initial Co-ordiante at Origin is : (%d, %d) ", x, y);

    printf("\nInitial Decision Parameter = %d", p);

    printf("\n\n\tK \t (x,y) \t (xc+x, yc+y) 2x \t 2y \t Pk");

    printf("\n\t 0 \t (%d,%d) \t (%d,%d) \t %d \t %d \t %d",x,y,xc+x,yc+y,2*x,2*y,p);

    while(x < y)

    {

    x = x + 1;if ( p < 0)

    p = p + 2*x + 1;

    else

    {y = y - 1;

    p = p + 2*x - 2*y + 1;

    }k++;

  • 8/12/2019 CG Journal

    12/49

    Ashish M. Gautam Application Id: 14131000229

    12

    printf("\n\t %d \t (%d,%d) \t (%d,%d) \t %d \t %d \t %d",k,x,y,xc+x,yc+y,2*x,2*y,p);

    }

    getch();}

    void main(){clrscr();

    getdata();

    dispdata();}

    Output:

  • 8/12/2019 CG Journal

    13/49

    Ashish M. Gautam Application Id: 14131000229

    13

    Program No: 6

    Aim: Write a C Program to implement the drawing of Ellipse Algorithm.

    Program:

    #include#include#include

    main(){

    long d1,d2;

    int i,gd,gm,x,y;

    long rx, ry, rxsq,rysq,tworxsq,tworysq,dx,dy;

    printf("Enter the x radius of the ellipse :");

    scanf("%ld",&rx);

    printf("Enter the y radius of the ellipse :");scanf("%ld",&ry);

    detectgraph(&gd,&gm);

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

    rxsq = rx * rx;

    rysq = ry * ry;

    tworxsq = 2 * rxsq;tworysq = 2 * rysq;

    x = 0;

    y = ry;

    d1 = rysq - rxsq * ry + (0.25 * rxsq) ;dx = tworysq * x;

    dy = tworxsq * y;

    do

    {

    putpixel(200+x,200+y,15);

    putpixel(200-x,200-y,15);putpixel(200+x,200-y,15);

    putpixel(200-x,200+y,15);

    if (d1 < 0)

    {x = x + 1;

    y = y;

    dx = dx + tworysq;d1 = d1 + dx + rysq;

    }

    else

  • 8/12/2019 CG Journal

    14/49

    Ashish M. Gautam Application Id: 14131000229

    14

    {

    x = x + 1;

    y = y - 1;dx = dx + tworysq;

    dy = dy - tworxsq;

    d1 = d1 + dx - dy + rysq;}delay(10);

    }while( dx < dy);

    d2 = rysq* (x+0.5) * (x+0.5)+rxsq*(y-1)*(y-1)-rxsq*rysq ;do

    {

    putpixel(200+x,200+y,15);

    putpixel(200-x,200-y,15);putpixel(200+x,200-y,15);putpixel(200-x,200+y,15);

    if(d2 > 0)

    {

    x = x;

    y = y -1;

    dy = dy -tworxsq;

    d2 = d2 - dy + rxsq;

    }

    else

    {

    x = x+1;

    y = y -1;

    dy = dy -tworxsq;dx = dx + tworysq;

    d2 = d2 + dx - dy + rxsq;

    }

    }while (y > 0);

    getch();closegraph();

    }

    Output:

    Enter the x radius of the ellipse: 26

    Enter the y radius of the ellipse: 16

  • 8/12/2019 CG Journal

    15/49

    Ashish M. Gautam Application Id: 14131000229

    15

    Program No: 7

    Aim: Write a C Program to implement the flood fill Algorithm.

    Program:

    #include#include#include

    #include

    void flood(int,int,int,int);

    void main()

    {

    int gd,gm=DETECT;int x, y, fillcolor=12, oldcolor=0;

    detectgraph(&gd,&gm);

    initgraph(&gd,&gm,"c:\\tc\\bgi");

    rectangle(50,50,100,100);

    x=y=55;

    flood(x,y,fillcolor,oldcolor);getch();

    }

    void flood(int x,int y, int fill_col, int old_col)

    {

    if(getpixel(x,y)==old_col)

    {delay(10);

    putpixel(x,y,fill_col);

    flood(x+1,y,fill_col,old_col);flood(x-1,y,fill_col,old_col);

    flood(x,y+1,fill_col,old_col);

    flood(x,y-1,fill_col,old_col);}

    }

    Output:

  • 8/12/2019 CG Journal

    16/49

    Ashish M. Gautam Application Id: 14131000229

    16

    Program No: 8

    Aim: Write a C Program to Implement the Boundary filling Algorithm.

    Program:

    #include#include#include

    void boundary_fill(int,int,int,int);

    void main()

    {

    int gd,gm=DETECT;

    int x, y, fillcolor=12, boundarycolor=15;detectgraph(&gd,&gm);

    initgraph(&gd, &gm, "d:\\tc\\bgi");

    rectangle(50,50,100,100);getch();

    x=y=55;

    boundary_fill(x,y,fillcolor,boundarycolor);

    getch();}

    void boundary_fill(int x, int y, int fcolor, int bcolor)

    {

    if ((getpixel(x, y) != fcolor) && (getpixel(x, y) != bcolor))

    {delay(10);

    putpixel(x, y, fcolor);

    boundary_fill(x+1, y, fcolor, bcolor);boundary_fill(x-1, y, fcolor, bcolor);

    boundary_fill(x, y+1, fcolor, bcolor);

    boundary_fill(x, y-1, fcolor, bcolor);}

    }

    Output:

  • 8/12/2019 CG Journal

    17/49

  • 8/12/2019 CG Journal

    18/49

    Ashish M. Gautam Application Id: 14131000229

    18

    detectgraph(&gm,&gr);

    initgraph(&gm,&gr,"c:\\tc\\BGI");

    rectangle(x,y,az,w);printf("*******Translation*******\n\n");

    printf("Enter the value of shift vector:\n");

    scanf("%f%f",&dx,&dy);x1=x+dx;y1=y+dy;

    az1=az+dx;

    w1=w+dy;rectangle(x1,y1,az1,w1);

    break;

    case 2:detectgraph(&gm,&gr);

    initgraph(&gm,&gr,"c:\\tc\\BGI");

    rectangle(x,y,az,w);printf("*******Rotation*******\n\n");

    printf("Enter the value of fixed point and angle of rotation:Enter the value

    of fixed point and angle of rotation:\n");

    scanf("%d%d%d",&xa,&ya,&ra);theta=(float)(ra*(3.14/180));

    for(i=0;i

  • 8/12/2019 CG Journal

    19/49

    Ashish M. Gautam Application Id: 14131000229

    19

    w1=w*sy;

    rectangle(x1,y1,az1,w1);

    break;

    case 4:

    detectgraph(&gm,&gr);initgraph(&gm,&gr,"c:\\tc\\BGI");rectangle(x,y,az,w);

    printf("*******Reflection*********\n");

    printf("1.About x-axis\n2.About y-axis\n3.About both axis\nEnter yourchoice:\n");

    scanf("%d",&ch1);

    switch(ch1)

    {case 1:

    printf("Enter the fixed point\n");

    scanf("%d%d",&xa,&ya);theta=(float)(90*(3.14/180));

    for(i=0;i

  • 8/12/2019 CG Journal

    20/49

    Ashish M. Gautam Application Id: 14131000229

    20

    line(a1[i],b1[i],a1[0],b1[0]);

    }

    break;

    case 3:

    printf("Enter the fixed point\n");scanf("%d%d",&xa,&ya);theta=(float)(180*(3.14/180));

    for(i=0;i

  • 8/12/2019 CG Journal

    21/49

    Ashish M. Gautam Application Id: 14131000229

    21

    x1=x;

    y1=y+(x*y1s);

    az1=az;w1=w+(az*y1s);

    rectangle(x1,y1,az1,w1);

    break;}break;

    case 6:exit(0);

    }

    }

    getch();}

    Output:

    Enter the upper left corner of the rectangle:

    100

    100Enter the lower right corner of the rectangle:

    200

    200

    ******2DTransformations*******1.Translation

    2.Rotation

    3.Scaling4.Reflection5.Shearing

    6.Exit

    Enter your choice: 1

    *******Translation******* Enter the value of shift vector:

  • 8/12/2019 CG Journal

    22/49

    Ashish M. Gautam Application Id: 14131000229

    22

    150

    150

    ******2DTransformations*******

    1.Translation2.Rotation

    3.Scaling

    4.Reflection

    5.Shearing6.Exit

    Enter your choice: 2*******Rotation*******Enter the value of fixed point and angle of rotation:

    300

    30070

  • 8/12/2019 CG Journal

    23/49

    Ashish M. Gautam Application Id: 14131000229

    23

    Program No: 10

    Aim: Write a C Program to rotate a small circle about an arbitrary center point.

    Program:

    #include#include#include

    #include

    #include

    int xc=50,yc=200,r=35;

    int x[15],y[15];

    void drawcircles()

    {

    setcolor(YELLOW);circle(xc,yc,r);

    circle(xc,yc,r+5);

    }

    void main()

    {

    double angle=0,theta;int i,a;

    int gd=DETECT,gm;

    initgraph(&gd,&gm,"C:\\tc\\bgi");

    a=xc+r;

    while(!kbhit())

    {while(a

  • 8/12/2019 CG Journal

    24/49

  • 8/12/2019 CG Journal

    25/49

    Ashish M. Gautam Application Id: 14131000229

    25

    Program No: 11

    Aim: Write a C Program for the character generation.

    Program:

    #include#includemain()

    {

    int gd,gm,i,j;// Save character map of letter A

    int a[13][9] = {

    { 0, 0, 0, 0, 1, 0, 0, 0, 0},

    { 0, 0, 0, 1, 0, 1, 0, 0, 0},{ 0, 0, 1, 0, 0, 0, 1, 0, 0},

    { 0, 1, 0, 0, 0, 0, 0, 1, 0},

    { 0, 1, 0, 0, 0, 0, 0, 1, 0},{ 0, 1, 0, 0, 0, 0, 0, 1, 0},

    { 0, 1, 1, 1, 1, 1, 1, 1, 0},

    { 0, 1, 0, 0, 0, 0, 0, 1, 0},

    { 0, 1, 0, 0, 0, 0, 0, 1, 0},{ 0, 1, 0, 0, 0, 0, 0, 1, 0},

    { 0, 1, 0, 0, 0, 0, 0, 1, 0},

    { 0, 1, 0, 0, 0, 0, 0, 1, 0},{ 0, 1, 0, 0, 0, 0, 0, 1, 0},

    };

    detectgraph(&gd,&gm);

    initgraph(&gd,&gm,"");for(i=0;i

  • 8/12/2019 CG Journal

    26/49

    Ashish M. Gautam Application Id: 14131000229

    26

    Program No: 12

    Aim: Write a C program for the Implementation of Bezier Curve Algorithm.

    Program:

    #include #include #include

    int gd,gm,maxx,maxy;

    float xxx[4][2];

    void line1(float x2,float y2)

    {

    line(xxx[0][0],xxx[0][1],x2,y2);xxx[0][0]=x2;

    xxx[0][1]=y2;

    }

    bezier(float xb,float yb,float xc,float yc,float xd,float yd,int n)

    {

    float xab,yab,xbc,ybc,xcd,ycd;float xabc,yabc,xbcd,ybcd;

    float xabcd,yabcd;

    if (n==0){

    line1(xb,yb);

    line1(xc,yc);

    line1(xd,yd);}

    else

    {xab = (xxx[0][0]+xb)/2;

    yab = (xxx[0][1]+yb)/2;

    xbc = (xb+xc)/2;ybc = (yb+yc)/2;

    xcd = (xc+xd)/2;

    ycd = (yc+yd)/2;

    xabc = (xab+xbc)/2;yabc = (yab+ybc)/2;

    xbcd = (xbc+xcd)/2;

    ybcd = (ybc+ycd)/2;

    xabcd = (xabc+xbcd)/2;yabcd = (yabc+ybcd)/2;

    n=n-1;

    bezier(xab,yab,xabc,yabc,xabcd,yabcd,n);bezier(xbcd,ybcd,xcd,ycd,xd,yd,n);

  • 8/12/2019 CG Journal

    27/49

    Ashish M. Gautam Application Id: 14131000229

    27

    }

    }

    void igraph()

    {

    detectgraph(&gd,&gm);if(gd

  • 8/12/2019 CG Journal

    28/49

    Ashish M. Gautam Application Id: 14131000229

    28

    Program No: 13

    Aim: Write a C program for the Implementation of Sutherland and Cohen subdivision line

    clipping Algorithm.

    Program:#include#include

    #include

    typedef struct

    {

    float x;

    float y;}PT;

    int n;

    main()

    {

    int i,j,gd,gm;PT d,p1,p2,p[20],pi1,pi2,pp[20];

    clrscr();

    detectgraph(&gd,&gm);initgraph(&gd,&gm,"C:\\TC\\BGI");

    printf("Enter coordinates (left,top) of point1 : ");

    scanf("%f %f",&p1.x,&p1.y);printf("Enter coordinates (right,bottom) of point2 : ");

    scanf("%f %f",&p2.x,&p2.y);

    printf("Enter the number of vertex : ");

    scanf("%d",&n);

    for(i=0;i

  • 8/12/2019 CG Journal

    29/49

    Ashish M. Gautam Application Id: 14131000229

    29

    right(p2,p,pp);

    top(p1,p,pp);

    bottom(p2,p,pp);cleardevice();

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

    drawpolygon(p,n);getch();closegraph();

    }

    left(PT p1,PT p[20],PT pp[20])

    {

    int i,j=0;

    for(i=0;i= p1.x)

    { if(p[i+1].x-p[i].x!=0)

    pp[j].y = (p[i+1].y-p[i].y)/(p[i+1].x-p[i].x)* (p1.x-p[i].x)+p[i].y;

    else

    pp[j].y = p[i].y;pp[j].x = p1.x;

    j++;

    pp[j].x=p[i+1].x;pp[j].y=p[i+1].y;

    j++;

    }

    if(p[i].x > p1.x && p[i+1].x >= p1.x)

    {

    pp[j].y = p[i+1].y;pp[j].x = p[i+1].x;

    j++;

    }

    if(p[i].x > p1.x && p[i+1].x

  • 8/12/2019 CG Journal

    30/49

  • 8/12/2019 CG Journal

    31/49

    Ashish M. Gautam Application Id: 14131000229

    31

    }

    p[i].x = pp[0].x;

    p[i].y = pp[0].y;n=j;

    }

    top(PT p1,PT p[20],PT pp[20]){

    int i,j=0;

    for(i=0;i= p1.y)

    {

    if(p[i+1].y-p[i].y!=0)pp[j].x = (p[i+1].x-p[i].x)/(p[i+1].y-p[i].y)* (p1.y-p[i].y)+p[i].x;

    else

    pp[j].x = p[i].x;pp[j].y = p1.y;

    j++;

    pp[j].x=p[i+1].x;

    pp[j].y=p[i+1].y;j++;

    }

    if(p[i].y > p1.y && p[i+1].y >= p1.y){

    pp[j].y = p[i+1].y;

    pp[j].x = p[i+1].x;

    j++;}

    if(p[i].y > p1.y && p[i+1].y

  • 8/12/2019 CG Journal

    32/49

    Ashish M. Gautam Application Id: 14131000229

    32

    n=j;

    }

    bottom(PT p2,PT p[20],PT pp[20])

    {

    int i,j=0;for(i=0;i p2.y && p[i+1].y

  • 8/12/2019 CG Journal

    33/49

    Ashish M. Gautam Application Id: 14131000229

    33

    drawpolygon(PT x[20],int n)

    {

    int i;for(i=0;i

  • 8/12/2019 CG Journal

    34/49

    Ashish M. Gautam Application Id: 14131000229

    34

    Program No: 14

    Aim: Write a C program for Drawing Smile Using Fractals.

    Program:

    #include#include#include

    main(){

    int gd = DETECT, gm, area, temp1, temp2, left = 25, top = 75;

    void *p;

    initgraph(&gd,&gm,"C:\\TC\\BGI");setcolor(YELLOW);

    circle(50,100,25);

    setfillstyle(SOLID_FILL,YELLOW);floodfill(50,100,YELLOW);

    setcolor(BLACK);

    setfillstyle(SOLID_FILL,BLACK);

    fillellipse(44,85,2,6);fillellipse(56,85,2,6);

    ellipse(50,100,205,335,20,9);

    ellipse(50,100,205,335,20,10);ellipse(50,100,205,335,20,11);

    area = imagesize(left, top, left + 50, top + 50);

    p = malloc(area);

    setcolor(WHITE);

    settextstyle(SANS_SERIF_FONT,HORIZ_DIR,2);outtextxy(155,451,"Smiling Face Animation");

    setcolor(BLUE);rectangle(0,0,639,449);

    while(!kbhit())

    {temp1 = 1 + random ( 588 );

    temp2 = 1 + random ( 380 );

    getimage(left, top, left + 50, top + 50, p);putimage(left, top, p, XOR_PUT);

    putimage(temp1 , temp2, p, XOR_PUT);

    delay(100);left = temp1;

  • 8/12/2019 CG Journal

    35/49

    Ashish M. Gautam Application Id: 14131000229

    35

    top = temp2;

    }

    getch();

    closegraph();

    return 0;}

    Output:

  • 8/12/2019 CG Journal

    36/49

  • 8/12/2019 CG Journal

    37/49

    Ashish M. Gautam Application Id: 14131000229

    37

    else if(Direct == 2)

    {

    x1 = (x2 + 640) / 2;y1 = (y2 + 480) / 2;

    }

    putpixel(x1, y1, WHITE);x2 = x1;y2 = y1;

    }

    }

    Cantor set:

    #include

    #include "stdlib.h"#include "math.h"

    #include

    #include #include

    float (*(createALine)(int x1,int y1,int x2,int y2,float (*previousArrLine)[2],int *arrLength))[2]

    {int q,i,k=0,no,n;

    int no_of_divisions=3;

    float (*arrLine)[2];float temp_x1,temp_x2,diff,next_temp_x1,next_temp_x2,equal_part;

    no=*arrLength;

    if(no==0){

    arrLine[0][0]=x1;

    arrLine[0][1]=x2;line(x1,y1,x2,y2);

    k=1;

    }else

    {

    k=0;

    for(q=0;q

  • 8/12/2019 CG Journal

    38/49

    Ashish M. Gautam Application Id: 14131000229

    38

    {

    if(i%2!=0)

    {arrLine[k][0]=next_temp_x1;

    arrLine[k][1]=next_temp_x2;

    line(next_temp_x1,y1,next_temp_x2,y2);k=k+1;}

    next_temp_x1=next_temp_x1+equal_part;

    next_temp_x2=next_temp_x2+equal_part;}

    }

    }

    *arrLength=*(&k);

    return arrLine;

    }

    int main()

    {

    int i;int gd=DETECT,gm, arrLength=0;

    int x1=0,x2=635,y1=0,y2=0,row;

    float arrLine1[100][2];float (*arrLine)[2];

    float (*(createALine)(int,int,int,int,float (*arrLine)[2],int *))[2];

    clrscr();

    initgraph(&gd,&gm,"c:tcbgi");arrLine=arrLine1;

    for(row=2;row

  • 8/12/2019 CG Journal

    39/49

    Ashish M. Gautam Application Id: 14131000229

    39

    Output:

  • 8/12/2019 CG Journal

    40/49

    Ashish M. Gautam Application Id: 14131000229

    40

    Program No: 16

    Aim: Write a C Program to Draw Bezier and B-Spline Curves with interactive user inputs.

    Program:

    #include

    typedef enum

    {

    BEZIER,INTERPOLATED,

    BSPLINE

    } curveType;

    void keyboard(unsigned char key, int x, int y);

    void computeMatrix(curveType type, float m[4][4]);

    void vmult(float m[4][4], float v[4][3], float r[4][3]);

    GLfloat colors[][3] =

    {

    { 1.0, 0.0, 0.0 },{ 0.0, 1.0, 0.0 },

    { 0.0, 0.0, 1.0 }

    };

    #define MAX_CPTS 25 /* Fixed maximum number of control points */

    GLfloat cpts[MAX_CPTS][3];int ncpts = 0;

    static int width = 500, height = 500; /* Window width and height */

    void reshape(int w, int h)

    {width = w;

    height = h;

    glMatrixMode(GL_PROJECTION);glLoadIdentity();

    glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);

    glMatrixMode(GL_MODELVIEW);

    glViewport(0, 0, w, h);}

    void vmult(float m[4][4], float v[4][3], float r[4][3]){

  • 8/12/2019 CG Journal

    41/49

    Ashish M. Gautam Application Id: 14131000229

    41

    int i, j, k;

    for (i = 0; i < 4; i++)for (j = 0; j < 3; j++)

    for (k = 0, r[i][j] = 0.0; k < 4; k++)

    r[i][j] += m[i][k] * v[k][j];}

    static float minterp[4][4] =

    {{ 1.0, 0.0, 0.0, 0.0 },

    { -5.0/6.0, 3.0, -3.0/2.0, 1.0/3.0 },

    { 1.0/3.0, -3.0/2.0, 3.0, -5.0/6.0 },

    { 0.0, 0.0, 0.0, 1.0 },};

    static float mbspline[4][4] ={

    { 1.0/6.0, 4.0/6.0, 1.0/6.0, 0.0 },

    { 0.0, 4.0/6.0, 2.0/6.0, 0.0 },

    { 0.0, 2.0/6.0, 4.0/6.0, 0.0 },{ 0.0, 1.0/6.0, 4.0/6.0, 1.0/6.0 },

    };

    static float midentity[4][4] =

    {

    { 1.0, 0.0, 0.0, 0.0},

    { 0.0, 1.0, 0.0, 0.0},{ 0.0, 0.0, 1.0, 0.0},

    { 0.0, 0.0, 0.0, 1.0}

    };

    void computeMatrix(curveType type, float m[4][4])

    {int i, j;

    switch (type)

    {case BEZIER:

    for (i = 0; i < 4; i++)

    for (j = 0; j < 4; j++)

    m[i][j] = midentity[i][j];break;

    case INTERPOLATED:

    for (i = 0; i < 4; i++)for (j = 0; j < 4; j++)

  • 8/12/2019 CG Journal

    42/49

    Ashish M. Gautam Application Id: 14131000229

    42

    m[i][j] = minterp[i][j];

    break;

    case BSPLINE:for (i = 0; i < 4; i++)

    for (j = 0; j < 4; j++)

    m[i][j] = mbspline[i][j];break;}

    }

    static void drawCurves(curveType type)

    {

    int i, j;

    int step;GLfloat newcpts[4][3];

    float m[4][4];

    computeMatrix(type, m);

    if(type == BSPLINE) step = 1;else step = 3;

    glColor3fv(colors[type]);

    i = 0;

    while (i + 3 < ncpts)

    {vmult(m, &cpts[i], newcpts);

    glMap1f(GL_MAP1_VERTEX_3, 0.0, 1.0, 3, 4, &newcpts[0][0]);

    glMapGrid1f(30, 0.0, 1.0);glEvalMesh1(GL_LINE, 0, 30);

    i += step;

    }glFlush();

    }

    static void display(void){

    int i;

    glClear(GL_COLOR_BUFFER_BIT);

    glColor3f(0.0, 0.0, 0.0);glPointSize(5.0);

    glBegin(GL_POINTS);

    for (i = 0; i < ncpts; i++)glVertex3fv(cpts[i]);

  • 8/12/2019 CG Journal

    43/49

  • 8/12/2019 CG Journal

    44/49

    Ashish M. Gautam Application Id: 14131000229

    44

    case 'i': case 'I':

    drawCurves(INTERPOLATED);

    lasttype = INTERPOLATED;break;

    case 's': case 'S':

    drawCurves(BSPLINE);lasttype = BSPLINE;break;

    }

    }

    main(int argc, char **argv)

    {glutInit(&argc, argv);

    glutInitDisplayMode(GLUT_RGB);

    glutInitWindowSize(width, height);glutCreateWindow("curves");

    glutDisplayFunc(display);

    glutMouseFunc(mouse);glutKeyboardFunc(keyboard);

    glutReshapeFunc(reshape);

    glClearColor(1.0, 1.0, 1.0, 1.0);glEnable(GL_MAP1_VERTEX_3);

    glutMainLoop();

    }

    Output:

  • 8/12/2019 CG Journal

    45/49

    Ashish M. Gautam Application Id: 14131000229

    45

    Program No: 17

    Aim: Write a C program to Demonstrate 2D animation such as clock simulation.

    Program:

    #include#include#include

    #include

    void main()

    {

    int gd=DETECT,gm;

    int x=320,y=240,r=200,i,h,m,s,thetamin,thetasec;struct time t;

    char n[12][3]={"3","2","1","12","11","10","9","8","7","6","5","4"};

    initgraph(&gd,&gm,"c:\tc");circle(x,y,210);

    setcolor(4);

    settextstyle(4,0,5);

    for(i=0;i

  • 8/12/2019 CG Journal

    46/49

  • 8/12/2019 CG Journal

    47/49

    Ashish M. Gautam Application Id: 14131000229

    47

    Output:

  • 8/12/2019 CG Journal

    48/49

  • 8/12/2019 CG Journal

    49/49

    Ashish M. Gautam Application Id: 14131000229

    return 0;

    }

    Output: