Graphics Sasikala

download Graphics Sasikala

of 68

Transcript of Graphics Sasikala

  • 8/8/2019 Graphics Sasikala

    1/68

    EX. NO. 1

    Bresenhams algorithms for line, circle, ellipse

    AIM

    To write and execute a c program to draw a line using BRESENHAMS line algorithm.ALGORITHM

    Initialize the graphics driver and mode.

    1. Read the coordinates for starting point (x1,y1) and ending point (x2,y2).

    2. Find dx=x2-x1 and dy=y2-y1 with its absolute value.

    3. If x1>x2 then set the increment step as -1, if x1 dx then exchange the dx, dy values.

    5. The initial decision value d=2*dy-dx;

    6. At each Xk (ie) k=0; k=1 etc perform the following test, If(d

  • 8/8/2019 Graphics Sasikala

    2/68

    scanf("%d",&y2);

    //Find the distance of x and y

    dx=abs(x2-x1);

    dy=abs(y2-y1);

    if((x2-x1)>0)

    xinc=1; // Drawing teh line from left to right

    else if((x2-x1)0)

    yinc=1; // top to bottom

    else if((y2-y1)dx)

    {

    temp=dy;

    dy=dx;

    dx=temp;

    exdy=1;

    }

    cleardevice();

    setbkcolor(WHITE);

    setcolor(BLUE);

    outtextxy(50,50,"BRESENHSAMS LINE");

    outtextxy(50,52,".................");

    // set the initial points

    x=x1;

    y=y1;

    2

  • 8/8/2019 Graphics Sasikala

    3/68

    d=2*dy-dx;

    for(i=1;i=0)

    {

    if(exdy==1)

    x=x+xinc;

    else

    y=y+yinc;

    d=d-2*dx;

    }

    if(exdy==1)

    y=y+yinc;

    else

    x=x+xinc;

    d=d+2*dy;

    delay(100);

    }

    getch();

    closegraph();

    return(0);

    }

    SAMPLE OUTPUT

    BRESENHSAMS LINE

    .

    Enter the x1,y1 coordinates

    100 100

    Enter the x1,y1 coordinates

    200 200

    3

  • 8/8/2019 Graphics Sasikala

    4/68

    Circle Drawing - Using BRESENHAMS algorithm

    AIM

    To write and execute a c program to draw a circle using BRESENHAMS circle

    algorithm.

    ALGORITHM

    1. Initialize the graphics driver and mode.

    2. Read the center point (xc,yc) and radius r.

    3. Calculate the pivot value p using suitable formula to find the pixel values.

    4. Using the values found out in step 3,put the pixels which forms the circle.

    PROGRAM

    #include#include

    #include

    void draw_circle(int,int,int);

    void plot(int,int,int,int);

    void main()

    {

    int xc,yc,r;

    int gd=DETECT,gm;

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

    cleardevice();

    printf(Enter the x axis\n);

    scanf(%d,&xc);

    4

  • 8/8/2019 Graphics Sasikala

    5/68

    printf(Enter the y axis\n);

    scanf(%d,&yc);

    printf(\n Enter the radius\n);

    scanf(%d,&r);

    circle(xc,yc,r);

    getch();

    closegraph();

    }

    void draw_circle(int xc,int yc,int r)

    {

    int p,x,y;

    x=0;

    y=r;

    while(x

  • 8/8/2019 Graphics Sasikala

    6/68

    putpixel(xc-x,yc-y,15) ;

    putpixel(xc+y,yc+x,15) ;

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

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

    putpixel(xc-y,yc-x,15) ;

    }

    SAMPLE OUTPUT

    ENTER THE CENTER AND THE RADIUS: 100 100 50.

    Ellipse Drawing - Using BRESENHAMS algorithm

    AIM

    To write and execute a c program to draw an ellipse using BRESENHAMS ellipse

    algorithm.

    ALGORITHM1. Change the normal mode to graphics mode by setting initgraph and detect

    graph

    2. Get input as the center co ordinate (xc,yc)of an ellipse and its semi major axis and semi

    minor axis (rx,ry)

    3. Intial point x,y is assigned by 0,ry

    4. Calculate the constant values used in the algorithm

    Twory2 = 2*ry*ry

    Tworx2 = 2*rx*rx

    5. For region 1 plot the initial point (x,y) using four pixel point.

    6. Initial P value is calculated by P = ry2*ry+(25*rx2)

    7. The step value and the region separation is calculated by px and py

    Px =0 and py = tworx2*y

    6

  • 8/8/2019 Graphics Sasikala

    7/68

    8. Repeat the process steps 9-12 until px value less than that of py

    9. Calculate x = x+1 and px=px+twory2

    10. If p>=0 then y=y-1 and py = py twory2

    11. If p0

    15. Calculate y=y-1 and py=py-tworx2

    16. If p0 then next p value is calculated by p=p+rx2-py else

    p-p+rx2-py+px

    18. Plot points using four pixel point process

    19. End the process. The plot point procedure consists of the following steps

    Putpixel (xc+x,yc+y)

    Putpixel (xc-x,yc+y)

    Putpixel (xc+x,yc-y)

    Putpixel (xc-x,yc-y)

    PROGRAM

    #include

    #include

    #include

    int xcenter,ycenter,x=0,y=0;

    void main()

    {

    int p,px,py;

    int ry,rx,ry2,rx2,tworx2,twory2;

    int gd=DETECT,gm;

    void plotpoints(int,int,int,int);

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

    7

  • 8/8/2019 Graphics Sasikala

    8/68

    clrscr();

    printf(Enter the center coordinates);

    scanf(%d%d,&xcenter,&ycenter);

    printf(enter the major and minor axes);

    scanf(%d%d,&rx,ry);

    ry2=ry*ry;

    rx2=rx*rx;

    twory2=2*ry2;

    tworx2=2*rx2;

    x=0;

    y=ry;

    plotpoints(xcenter,ycenter,x,y);

    p=ceil(ry2-rx2*ry+(0.25*rx2));

    px=0;

    py=tworx2*y;

    while(px=0)

    {

    y=y-1;

    py=py-tworx2;

    }

    if(p

  • 8/8/2019 Graphics Sasikala

    9/68

    while(y>0)

    {

    y=y-1;

    py=py-tworx2;

    if(p0)

    p=p+rx2-py;

    else

    p=p+rx2-py+px;

    plotpoints(xcenter,ycenter,x,y);

    }

    getch();

    }

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

    {

    setcolor(BLUE);

    putpixe1(xcenter+x,ycenter+y,1);

    putpixe1(xcenter-x,ycenter+y,1);

    putpixe1(xcenter+x,ycenter-y,1);

    putpixe1(xcenter-x,ycenter-y,1);

    /*putpixe1(xcenter+y,ycenter+x,1);

    putpixe1(xcenter-y,ycenter+x,1);

    putpixe1(xcenter+y,ycenter-x,1);

    putpixe1(xcenter-y,ycenter-x,1);*/}

    SAMPLE OUTPUT

    9

  • 8/8/2019 Graphics Sasikala

    10/68

    Enter the coordinates

    50 50

    EX. NO. 2

    2D Transformation such as translation, rotation, scaling, reflection and

    shearing

    AIM

    To perform the transformation of Translation on 2D objects.

    ALGORITHM

    Initialize the graphics driver and mode.

    1. Read the coordinates for 2D object which is transformed.

    2. For Translation, read the translation vector and add it to the original coordinates of

    the 2D object.

    3. Stop the program.

    PROGRAM

    #include

    #include

    #include

    #include

    void main()

    {

    int gdrive=0,gmode,ch;

    float tx,ty,r,a,b,c,d;

    char cho;

    10

  • 8/8/2019 Graphics Sasikala

    11/68

    printf("\n\n\t\t 2D Translation \n\n");

    printf("Enter the coordinates of the rectangle \n" );

    scanf("%f%f%f%f",&a,&b,&c,&d);

    initgraph(&gdrive,&gmode,"c:\\tc\\bgi\\");

    do{

    printf("1.Translation in X direction 2.Translation in Y direction

    3.Translation in X & Y direction\n\n");

    scanf("%d",&ch);

    switch(ch){

    case 1:

    cleardevice();

    rectangle(a,b,c,d);

    printf("Enter the translation factor for X direction");

    scanf("%f",&tx);

    rectangle(a,b,c,d);

    rectangle(a+tx,b,c+tx,d);

    break;

    case 2:

    cleardevice();

    rectangle(a,b,c,d);

    printf("Enter the translation factor for Y direction");

    scanf("%f",&ty);

    rectangle(a,b,c,d);

    rectangle(a,b+ty,c,d+ty);

    break;

    case 3:

    cleardevice();

    rectangle(a,b,c,d);

    printf("Enter the translation factor for X and Y direction");

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

    rectangle(a,b,c,d);

    11

  • 8/8/2019 Graphics Sasikala

    12/68

    rectangle(a+tx,b+ty,c+tx,d+ty);

    break;

    }

    getch();

    printf("continue y/n");

    scanf("%c",&cho);

    cleardevice();

    }

    while(cho=='y');

    closegraph();

    }

    SAMPLE OUTPUT

    2D Translation

    Enter the coordinates of the rectangle

    100 100 200 200

    1. Translation in X direction 2.Translation in Y direction

    3.Translation in X & Y direction

    1

    Enter the translation factor for X direction

    150

    12

  • 8/8/2019 Graphics Sasikala

    13/68

  • 8/8/2019 Graphics Sasikala

    14/68

    }

    SAMPLE OUTPUT

    Enter the coordinates of line

    100 100 200 100

    Enter the rotation angle

    30

    Scaling

    AIM

    To perform the transformation of Scaling on 2D objects.

    ALGORITHM

    Initialize the graphics driver and mode.

    1. Read the coordinates for 2D object which is transformed.

    2. For Scaling read the scaling vector and multiply it to the original coordinates of the 2Dobject.

    3. Stop the program

    PROGRAM

    #include

    #include

    #include

    #include

    void main()

    {

    int gdrive=0,gmode,ch;

    float sx,sy,r,a,b,c,d;

    char cho;

    14

  • 8/8/2019 Graphics Sasikala

    15/68

    printf("\n\n\t\t 2D Scaling \n\n");

    printf("Enter the coordinates of the rectangle \n" );

    scanf("%f%f%f%f",&a,&b,&c,&d);

    initgraph(&gdrive,&gmode,"c:\\tc\\bgi\\");

    do

    {

    printf("1.Scaling in X direction 2.Scaling in Y direction 3.Scaling in X & Y

    direction\n\n");

    scanf("%d",&ch);

    switch(ch){

    case 1:

    cleardevice();

    rectangle(a,b,c,d);

    printf("Enter the scaling factor for X direction");

    scanf("%f",&sx);

    rectangle(a,b,c,d);

    rectangle(a*sx,b,c*sx,d);

    break;

    case 2:

    cleardevice();

    rectangle(a,b,c,d);

    printf("Enter the scaling factor for Y direction");

    scanf("%f",&sy);

    rectangle(a,b,c,d);

    rectangle(a,b*sy,c,d*sy);

    break;

    case 3:

    cleardevice();

    rectangle(a,b,c,d);

    printf("Enter the scaling factor for X and Y direction");

    scanf("%f %f",&sx,&sy);

    15

  • 8/8/2019 Graphics Sasikala

    16/68

    rectangle(a,b,c,d);

    rectangle(a*sx,b*sy,c*sx,d*sy);

    break;

    }

    getch();

    printf("continue y/n");

    scanf("%c",&cho);

    cleardevice();

    }

    while(cho=='y');

    closegraph();

    }

    SAMPLE OUTPUT

    2D Scaling

    Enter the coordinates of the rectangle

    100 100 200 200

    1. Scaling in X direction 2.Scaling in Y direction 3.Scaling in X & Y direction

    1

    Enter the scaling factor for X direction

    2

    16

  • 8/8/2019 Graphics Sasikala

    17/68

    Shearing and Reflection

    AIM

    To perform the transformation of Shearing and Reflection on 2D objects.

    ALGORITHM

    Initialize the graphics driver and mode.

    1. Read the coordinates for 2D object which is transformed.

    2. For Shearing read the shearing factor and apply the shearing formula .

    Shearing with respect to x , x=x + shx*y, y=y

    Shearing with respect to y , y=y + shy*x, x=x

    3. Stop the program

    PROGRAM

    #include

    #include

    #include

    #include

    void main()

    {

    int gd=DETECT,gm,midx,midy,w;

    int xs1,xs2,xs3,ys1,ys2,ch,x1,y1,x2,y2,x3,y3,a,b,c,n,z,sh;initgraph(&gd,&gm,"c:\\tc\\bgi");

    printf("\nEnter the first coordinates");

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

    printf("\nEnter the second coordinates");

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

    midx=abs((x2-x1)/2);

    midy=abs((y2-y1)/2);

    cleardevice();

    do

    {

    printf("1.Shearing\n2.Reflection\n3.Quit\n");

    printf("Enter your choice");

    17

  • 8/8/2019 Graphics Sasikala

    18/68

    scanf("%d",&n);

    switch(n)

    {

    case 1:

    cleardevice();

    line(x1,y1,x2,y2);

    moveto(x2,y2);

    lineto(x1+midx,y1+midy);

    lineto(x1,y1);

    printf(" Enter the shearing factor \n");

    scanf("%d",&sh);

    printf("Enter the choice 1- with respect to X 2. with respect to

    Y\n");

    scanf("%d",&ch);

    printf("%d",ch);

    if(ch==1)

    {

    xs1=x1+sh*y1;

    xs2=x2+sh*y2;

    line(x1,y1,xs2,y2);

    moveto(xs2,y2);

    lineto(xs1+abs((xs2-xs1)/2),y1+abs((y2-y1)/2));

    lineto(x1,y1);

    }

    else

    {

    ys1=y1+sh*x1;

    ys2=y2+sh*x2;

    line(x1,y1,x2,ys2);

    moveto(x2,ys2);

    lineto(x1+abs((x2-x1)/2),ys1+abs((ys2-ys1)/2));

    18

  • 8/8/2019 Graphics Sasikala

    19/68

    lineto(x1,y1);

    }

    getch();

    break;

    case 2:

    cleardevice();

    line(x1,y1,x2,y2);

    moveto(x2,y2);

    lineto(x1+midx,y1+midy);

    lineto(x1,y1);

    if(y1>y2)

    z=y1;

    else

    z=y2;

    if(x1

  • 8/8/2019 Graphics Sasikala

    20/68

    getch();

    }

    SAMPLE OUTPUT

    Enter the first coordinates

    100 100

    Enter the second coordinates

    200 100

    1. Shearing

    2. Reflection

    3. Quit

    Enter your choice

    1

    Enter the shearing factor

    1

    Enter the choice 1- with respect to X 2. with respect to Y

    1

    1. Shearing

    2. Reflection

    3. Quit

    20

  • 8/8/2019 Graphics Sasikala

    21/68

  • 8/8/2019 Graphics Sasikala

    22/68

    7. If the line is partially inside and outside clip the line. Midpoint subdivision procedure is

    used to find the point lying on the window to clip the line.

    8. The accepted lines are redraw within the window area.

    9. Stop the process.

    PROGRAM

    #include

    #include

    #include

    #include

    #include

    #include

    typedef struct coordinate

    {

    int x,y;

    char code[100];

    }

    pt;

    void drawwindow();

    void drawline(pt p1,pt p2,int c1);

    pt setcode(pt p);

    int visibility(pt p1,pt p2);

    //pt resetendpt(pt p1,pt p2);

    void main()

    {

    int gd=DETECT,gm,v;

    pt p1,p2,ptemp;

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

    cleardevice();

    printf(enter points:(x,y));

    scanf(%d%d,&p1.x,&p2.y);

    printf(enter 2 point (x,y));

    22

  • 8/8/2019 Graphics Sasikala

    23/68

    scanf(%d%d,&p2.x,&p2y);

    cleardevice();

    drawwindow();

    getch();

    drawline(p1,p2,15);

    getch();

    cleardevice();

    drawwindow();

    midsub(p1,p2);

    getch();

    closegraph();

    }

    mindsub(pt p1,pt p2)

    {

    pt mid;

    int v;

    p1=setcode(p1);

    p2=setcode(p2);

    v=visibility(p1,p2);

    switch(v)

    {

    case 0:drawline(p1,p2,15);

    break;

    case 1:

    break;

    case 2:

    mid.x=p1.x+(p2.x-p1.x)/2;

    mid.y=p1.x+(p2.y-p1.y)/2;

    midsub(p1,mid);

    mid.x=mid.x+1;

    mid.y=mid.y+1;

    23

  • 8/8/2019 Graphics Sasikala

    24/68

    midsub(mid,p2);

    break;

    }

    return(0);

    }

    void drawwindow()

    {

    setcolor(4);

    line(150,100,450,100);

    line(450,100,450,400);

    line(450,400,150,400);

    line(150,400,150,100);

    }

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

    {

    setcolor(c1);

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

    }

    pt setcode(Pt p)

    {

    pt ptemp;

    if(p.y=450)

    ptemp.code[2]=1;

    else

    ptemp.code[2]=0;

    if(p.x

  • 8/8/2019 Graphics Sasikala

    25/68

    else

    ptemp.code[3]=0;

    ptemp.x=p.x;

    ptemp.y=p.y;

    return(ptemp);

    }

    int visibility(pt p1,pt p2)

    {

    int i,flag=0;

    for(i=0;

  • 8/8/2019 Graphics Sasikala

    26/68

    Before Clipping

    After Clipping

    Window view port mapping

    AIM

    To write a program in C for window to viewport transformations.

    ALGORITHM1. Initialize the graphics mode.

    2. Draw a window.

    3. Plot a point in the window.

    4. Transform that point to view port.

    PROGRAM

    #include

    #include

    #include

    void main(void)

    {

    int gd=0,gm;

    int xw=100,yw=100;

    26

  • 8/8/2019 Graphics Sasikala

    27/68

    int ywmax=50,ywmin=9,xwmax=500,xwmin=400;

    int yvmax=150,yvmin=50,xvmax=326,xvmin=300;

    int xv,yv,sx,sy;

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

    cleardevice();

    putpixel(xw,yw,5);

    rectangle(ywmin,ywmax,xwmax,xwmin);

    gotoxy(10,28);

    printf(\WINDOW COORDINATE SYSTEM);

    getch();

    cleardevice();

    setcolor(3);

    rectangle(yvmin,yvmax,xvmax,xvmin);

    sx=((xvmax-xvmin)/(xwmax-xwmin)) ;

    sy=((yvmax-vvmin)/(ywmax-ywmin)) ;

    xv=xvmin+((xw-xwmin)*sx) ;

    yv=yvmin+((yw-ywmin)*sy) ;

    putpixel(xv,yv,6) ;

    gotoxy(10,28) ;

    printf(\VIEW PORT COORDINATE SYSTEM);

    getch();

    closegraph();

    }

    SAMPLE OUTPUT

    27

  • 8/8/2019 Graphics Sasikala

    28/68

    EX. NO. 4

    3D Transformation such as translation, rotation and scaling

    AIM

    To perform the transformation operations such as Translation, Scaling and Rotation on

    3D objects.

    ALGORITHM

    Initialize the graphics driver and mode.

    1. Read the coordinates for 3D object which is transformed.

    2. For Translation, read the translation vector and add it to the original coordinates of

    the 3D object.

    3. For Scaling, read the scaling vector and multiply it to the original coordinates of the

    3D object.

    28

  • 8/8/2019 Graphics Sasikala

    29/68

    4. For Rotation, read the angle of rotation, distance value and calculate the new

    coordinates using the formula for rotation and draw the 3D object for the new

    coordinates.

    PROGRAM

    #include

    #include

    #include

    #include

    #include

    void main()

    {

    void cube(int,int,int);

    int gd=DETECT,gm;

    int tx,ty,tz,sx,sy,sz,x,y,z,m,o,ch,dx,dy,dz;

    int xf,yf,zf,a,teta,x1,x2,y1,y2,m1,n;

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

    clrscr();

    printf("\n 1-Translation \n 2-Scaling \n 3-Rotation \n ");

    printf("Enter your choice");

    scanf("%d",&ch);

    switch(ch)

    {

    case 1:

    printf("\n Enter the value for cube:");

    scanf("%d %d %d",&x,&y,&z);

    cube(x,y,z);

    printf("Enter the translation vertex");

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

    dx=x+tx;

    dy=y+ty;

    dz=z+tz;

    29

  • 8/8/2019 Graphics Sasikala

    30/68

    cube(dx,dy,dz);

    getch();

    case 2:

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

    scanf("%d %d %d",&x,&y,&z);

    cube(x,y,z);

    printf("Enter the pivot element");

    scanf("%d %d %d",&xf,&yf,&zf);

    printf("enter the scaling vector");

    scanf("%d %d %d",&sx,&sy,&sz);

    dx=x*sx+(1-sx)*xf;

    dy=y*sy+(1-sy)*yf;

    dz=z*sz+(1-sz)*zf;

    cube(dx,dy,dz);

    getch();

    case 3:

    printf("enter ur choice");

    printf("4. x axis 5.y axis 6.z axis");

    scanf("%d",&n);

    switch(n)

    {

    case 4:

    printf("4.with repect x axis");

    printf("enter the va;lue of the cube");

    scanf("%d%d%d",&x,&y,&z);

    cube(x,y,z);

    printf("THE ROTATION IS ONLY RESPECT WITH X AXIS");

    printf("\nenter the angle of rotation");

    scanf("%d",&m);

    dx=x;

    dy=y*cos(m)-z*sin(m);

    30

  • 8/8/2019 Graphics Sasikala

    31/68

    dz=y*sin(m)+z*cos(m);

    cube(dx,dy,dz);

    getch();

    case 5:

    printf("5.with repect y axis");

    printf("enter the va;lue of the cube");

    scanf("%d%d%d",&x,&y,&z);

    cube(x,y,z);

    printf("THE ROTATION IS ONLY RESPECT WITH Y AXIS");

    printf("\nenter the angle of rotation");

    scanf("%d",&m);

    dx=z*sin(m)+x*cos(m);

    dy=y;

    dz=z*cos(m)-x*sin(a);

    cube(dx,dy,dz);

    getch();

    case 6:

    printf("6.with repect z axis");

    printf("enter the va;lue of the cube");

    scanf("%d%d%d",&x,&y,&z);

    cube(x,y,z);

    printf("THE ROTATION IS ONLY RESPECT WITH Z AXIS");

    printf("\nenter the angle of rotation");

    scanf("%d",&m);

    dx=x*cos(m)-y*sin(m);

    dy=y*sin(m)+z*cos(m);

    dz=z;

    cube(dx,dy,dz);

    getch();

    }

    }

    31

  • 8/8/2019 Graphics Sasikala

    32/68

    getch();

    }

    void cube(int x,int y,int z)

    {

    line(x,y,x+100,y);

    line(x,y,x-50,y+50);

    line(x-50,y+50,x+50,y+50);

    line(x+50,y+50,x+100,y);

    line(x-50,y+50,x-50,y-50);

    line(x-50,y-50,x,y-100);

    line(x-50,y-50,x+50,y-50);

    line(x,y-100,x+100,y-100);

    line(x+100,y-100,x+50,y-50);

    line(x+50,y-50,x+50,y+50);

    line(x+100,y-100,x+100,y);

    line(x,y-100,x,y);

    getch();

    }

    SAMPLE OUTPUT

    THREE-D TRANSFORMATION

    1-TRANSLATION

    2-SCALING

    3-ROTATION

    32

  • 8/8/2019 Graphics Sasikala

    33/68

    ENTER THE CHOICE: 1

    ENTER THE VALUES FOR THE CUBE: 30 30 30

    ENTER TRANSLATION VALUE: 30 20 10

    ENTER THE CHOICE: 2

    1. FIXED POINT

    2. NOT FIXED POINT

    ENTER THE CHOICE: 1

    ENTER THE VALUES FOR THE CUBE: 30 30 30

    33

  • 8/8/2019 Graphics Sasikala

    34/68

    ENTER THE SCALING VALUES: 1 2 1

    1-TRANSLATION

    2-SCALING

    3-ROTATION

    ENTER THE CHOICE: 3

    1-ABOUT X-AXIS

    2-ABOUT Y-AXIS

    3-ABOUT Z-AXIS

    ENTER YOUR CHOICE: 1

    ENTER THE VALUES FOR THE CUBE: 30 30 30

    ENTER THE ROTATION ANGLES: 45 60 75

    34

  • 8/8/2019 Graphics Sasikala

    35/68

    EX. NO. 5

    Projections of 3D Images

    AIM

    To perform a parallel projection on a three dimensional image.ALGORITHM

    Initialize the graphics driver and mode.

    1. Read the coordinates for 3D object which is to be projected.

    2. Get the menu choices for parallel projection.

    3. Read the different coordinates for the four options

    Top-view

    Front-view

    Side-view

    Original view.

    PROGRAM

    #include

    #include

    void main()

    {

    int g_driver=DETECT,g_mode;

    int ae,ef,bf,ab,bc,choice,choice1;

    initgraph(&g_driver,&g_mode,"c:\\tc\\bgi");

    printf(" \t values of 3D-BAR \n");

    printf("Enter the value of left :"); //int left, int top, int right, int

    bottom,int depth

    scanf("%d",&ae); // ae -> ,ef-> ,bf-> , ab-> ,bc->

    printf("Enter the value of top :");

    scanf("%d",&ef);

    printf("Enter the value of right :");

    scanf("%d",&bf);

    printf("Enter the value of bottom :");

    scanf("%d",&ab);

    35

  • 8/8/2019 Graphics Sasikala

    36/68

    printf("Enter the value of depth :");

    scanf("%d",&bc);

    cleardevice();

    printf("\t Your 3D - BAR \n");

    bar3d(ae,ef,bf,ab,bc,1);

    //bar3d(midx-50, midy-50, midx+50,midy+50, 10, 1);

    getch();

    do

    {

    cleardevice();

    printf("\t OPTION DETAILS\n ");

    printf(" 1.To view 3D-BAR \n");

    printf(" 2.To view Front view\n");

    printf(" 3.To view Top view\n");

    printf(" 4.To view Side view\n");

    printf(" Enter your choice : ");

    scanf("%d",&choice);

    cleardevice();

    switch(choice)

    {

    case 1:

    bar3d(ae,ef,bf,ab,bc,1);

    case 2:

    // bar(int left, int top, int right, int bottom);

    bar(ae,ef,bf,ab);

    break;

    case 3:

    bar(ae,ae,bf,(ae+bc));

    break;

    case 4:

    bar(ae,ef,(ae+bc),ab);

    36

  • 8/8/2019 Graphics Sasikala

    37/68

    break;

    default:

    printf("\n\tWrong choice\n");

    }

    printf("Do you wish to be continue? [YES(1)/NO(other)]: ");

    scanf("%d",&choice1);

    }

    while(choice1==1);

    cleardevice();

    SAMPLE OUTPUT:

    values of 3D-BAR

    Enter the value of left :

    Enter the value of top :

    Enter the value of right :

    Enter the value of bottom :

    Enter the value of depth :

    Your 3D BAR

    OPTION DETAILS

    1.To view 3D-BAR

    2.To view Front view

    3.To view Top view

    4.To view Side view

    Enter your choice : 2

    37

  • 8/8/2019 Graphics Sasikala

    38/68

    Do you wish to be continue? [YES(1)/NO(other)]:1

    OPTION DETAILS

    1.To view 3D-BAR

    2.To view Front view

    3.To view Top view

    4.To view Side view

    Enter your choice : 3

    Do you wish to be continue? [YES(1)/NO(other)]:1

    OPTION DETAILS

    1.To view 3D-BAR

    2.To view Front view

    3.To view Top view

    4.To view Side view

    Enter your choice : 4

    Do you wish to be continue? [YES(1)/NO(other)]:2

    38

  • 8/8/2019 Graphics Sasikala

    39/68

    EX. NO.6

    Conversion between color models

    Aim:

    To change the colors of an object

    ALGORITHM:

    1. Click a layer name to make it the current layer, and select a frame in the layer

    where you want the animation to start.

    2. If the frame isn't already a key frame, choose Insert > Key frame to make it one.

    3. Create the artwork for the first frame of the sequence.

    4. You can use the drawing tools, paste graphics from the Clipboard

    5. Place the Required Object in the scene

    6. Place color buttons, text boxes and required tools

    7. Write the action script

    39

  • 8/8/2019 Graphics Sasikala

    40/68

    Sample Output

    40

  • 8/8/2019 Graphics Sasikala

    41/68

    EX. NO. 7

    Text Compression Algorithm

    Run Length Encoding:

    AIM:

    To implement run length encoding in C

    ALGORITHM:

    1. Get the input string of character from the user and store it in an array.

    2. Initially the string of characters is read one character by another and they are stored

    in a structure.

    3. The structure contains a character and an integer.

    4. Each and every time a character is read from the input string its occurrence is also

    found.

    5. When a character is checked, it is stored in the sample character in the structure

    6. Then the next character is also checked, if the ext character is matching the previous

    character, then a counter is increased.

    7. As soon as the matching is not there, store the character and the number of

    occurrence, i.e. the counter value in the structure.

    8. This process goes on until the input string comes to an end.

    9. This results in the compressed text.

    10. While decompression, the structure is read first.

    11. For each character, it checks for the number of occurrence, then prints the character

    that number of times.

    12. This results in the decompressed text.

    PROGRAM

    #include

    #includevoid main()

    {

    void decomp(char op[30]);

    char ip[30],op[30];

    char v[1],value1,value2;

    41

  • 8/8/2019 Graphics Sasikala

    42/68

    int i=0,j=1,k=0,t,x;

    int cmp,l,rpt=1,rpt1,rpt2;

    clrscr();

    printf("Enter the data : ");

    gets(ip);

    for(l=1;l

  • 8/8/2019 Graphics Sasikala

    43/68

    op[++k]=(value1);

    op[++k]=(value2);

    }

    rpt=1;

    i=t,j=t+1,k++;

    }

    else

    {

    rpt=1;

    op[k]=ip[i];

    i++,j++,k++;

    if(j>=strlen(ip))

    {

    op[k]=ip[i];

    op[++k]='\0';

    goto end;

    }

    }

    }

    end:

    printf("Compressed Data : %s\n",op);

    getch();

    decomp(op);

    }

    void decomp(char ip1[30])

    {

    char op1[30];

    char temp,*t1,*t2;

    int i,i1,i2,j,k=0,l;

    for(l=0;l

  • 8/8/2019 Graphics Sasikala

    44/68

    if (ip1[l]=='#')

    {

    temp=ip1[++l];

    *t1=ip1[++l];

    *t2=ip1[++l];

    i1=atoi(t1);

    i2=atoi(t2);

    i=(i1*10)+i2;

    for(j=0;j

  • 8/8/2019 Graphics Sasikala

    45/68

    SAMPLE INPUT AND OUTPUT

    Enter the data : aaaassdddddddddfffggggggg

    Compressed Data : aaaass#d09fff#g07

    Decompresed Output : aaaassdddddddddfffggggggg

    Huffman Coding:

    AIM

    To Implement Huffman Coding in C

    ALGORITHM

    1. First the source symbols are listed in the order of decreasing probability.

    2. The two source symbols of the lowest probability are assigned a 0 and a 1.

    3. This part of the step is referred to as a splitting stage.

    4. These two source symbols are regarded as being combined into a new source symbol

    with probability equal to the sum of the two original probabilities.

    5. The probability of the new symbol is placed in the list in accordance with its value.

    6. The procedure is repeated until we are left with a final list of source statistics of only

    two for which a 0 and a 1 are assigned.

    7. Then traversing backwards, find the desired digit or symbol.

    8. The resulting sequence of 0s and 1s are the resulting Huffman code for that particularsymbol.

    PROGRAM

    #include

    #include

    #include

    void main()

    {

    static char ip[30],code[30];

    static char table[30][30];

    static int count[30];

    static int related[30][30];

    int r,rpt;

    45

  • 8/8/2019 Graphics Sasikala

    46/68

    int i,j;

    void show(char code[30],char table[30][30]);

    void counting(char ip[30],char code[30],int count[30],int related[30][30]);

    void sort(char code[30],int count[30]);

    int calculate(int cou,int count[30],int related[30][30],char table[30][30]);

    void sort2(int count[30],int related[30][30],int cnt);

    void rectify(char table[30][30]);

    void tableit(char ip[30],char code[30],char table[30][30]);

    clrscr();

    for(i=0;i

  • 8/8/2019 Graphics Sasikala

    47/68

    getch();

    }

    void show(char code[30],char table[30][30])

    {

    int i,j,k;

    for(i=0;i

  • 8/8/2019 Graphics Sasikala

    48/68

    }

    if(found==0)

    {

    code[k]=ip[i];

    related[k][0]=k+1;

    count[k++]=1;

    }

    if(found==1)

    {

    count[f]=count[f]+1;

    }

    }

    }

    void sort(char code[30],int count[30])

    {

    int i,j,k,vari=0;

    char temp='\0';

    for(i=0;i

  • 8/8/2019 Graphics Sasikala

    49/68

    count[i]=vari;

    code[i]=temp;

    }

    }

    }

    }

    void sort2(int count[30],int related[30][30],int cnt)

    {

    int i,j,vari,k,x;

    int temp[2][30]={0};

    for(i=0;i

  • 8/8/2019 Graphics Sasikala

    50/68

    for(x=0;x

  • 8/8/2019 Graphics Sasikala

    51/68

    }

    ct=ct+1;

    }

    escape:

    for(i=0;i

  • 8/8/2019 Graphics Sasikala

    52/68

    {

    for(j=0;j

  • 8/8/2019 Graphics Sasikala

    53/68

  • 8/8/2019 Graphics Sasikala

    54/68

    EX. NO. 8:

    Image compression algorithm

    AIM:

    To perform image compression using JAVA

    ALGORITHM:

    1. Get the image to be compressed.

    2. Get pixel values of the image using PixelGrabber

    3. Store the pixel value in one dimensional array

    4. Compress the pixel array using run length encoding

    5. This will produced the compressed result

    6. End the program

    Program

    public class image extends Applet{

    Image im;

    int pixel[];

    int pixel1[];

    int w;

    int h;

    public void init(){

    try

    {

    im= getImage(getDocumentBase(),"yy.gif");

    MediaTracker mt= new MediaTracker(this);

    mt.addImage(im,0);

    mt.waitForID(0);

    w=im.getWidth(this);

    h=im.getHeight(this);

    pixel=new int[w*h];

    pixel1=new int[w*h];

    54

  • 8/8/2019 Graphics Sasikala

    55/68

    PixelGrabber pg=new PixelGrabber(im,0,0,w,h,pixel,w,h);

    pixel1= (int[]) pg.getPixels();

    compress cs=new compress();

    cs.rle(pixel1);

    System.out.println("the image has been compressed using RLE");

    //MemoryImageSource mm=new MemoryImageSource(w,h,pixel,w,h);

    }

    catch(Exception e){}

    }

    public void paint(Graphics g){

    g.drawImage(im,0,0,w,h,this);}

    }

    SAMPLE OUTPUT

    C:\jdk1.3\bin>javac image.java

    C:\jdk1.3\bin>appletviewer image.java

    the image has been compressed using RLE

    55

  • 8/8/2019 Graphics Sasikala

    56/68

    EX. NO.9:

    Animation using any animation software

    Frame By Frame

    AIM

    To perform Frame by Frame animation

    ALGORITHM

    1. Click a layer name to make it the current layer, and select a frame in the layer where

    you want the animation to start.

    2. If the frame isn't already a key frame, choose Insert > Key frame to make it one.

    3. Create the artwork for the first frame of the sequence.

    4. You can use the drawing tools, paste graphics from the Clipboard, or import a file.

    5. Click the next frame to the right in the same row and choose Insert > Keyframe, or

    right-click (Windows) or Control-click (Macintosh) and choose Insert Key frame from

    the context menu.

    6. This adds a new key frame whose contents are the same as those of the first key frame.

    7. Alter the contents of this frame on the Stage to develop the next increment of the

    animation.

    8. To complete your frame-by-frame animation sequence, repeat steps 4 and 5 until you've

    built the motion you want.

    9. To test the animation sequence, choose Control > Play or click the Play button on the

    Controller.

    56

  • 8/8/2019 Graphics Sasikala

    57/68

    SAMPLE OUTPUT

    Motion Tweening in Flash MX

    AIM

    To Implement Motion Tweening in Flash Mx

    ALGORITHM

    To create a motion tween using the Motion Tweening option:

    1. Click a layer name to make it the current layer, and select an empty key frame in the layer

    where you want the animation to start.

    2. To create the first frame of the motion tween, do one of the following:

    Create a graphic object with the Pen, Oval, Rectangle, Pencil, or Brush tool, and then

    convert it to a symbol. For more information on converting objects to symbols, see creating

    symbols.

    57

  • 8/8/2019 Graphics Sasikala

    58/68

    Create an instance, group, or text block on the Stage.

    Drag an instance of a symbol from the Library panel.

    3. Create a second key frame where you want the animation to end, and then select the ending

    frame (immediately to the left of the second key frame on the Timeline).

    4. Do any of the following to modify the instance, group, or text block in the ending frame:

    Move the item to a new position.

    Modify the item's size, rotation, or skew.

    Modify the item's color (instance or text block only).

    To tween the color of elements other than instances or text blocks, use shape tweening.

    See Tweening shapes.

    5. If the Property inspector is not visible, choose Window > Properties.

    6. Double-click the ending frame in the Timeline.

    7. Select Motion from the Tween pop-up menu in the Property inspector.

    8. If you modified the size of the item in step 4, select Scale to tween the size of the selected

    item.

    9. Drag the arrow next to the Easing value or enter a value to adjust the rate of change between

    tweened frames:

    To begin the motion tween slowly and accelerate the tween toward the end of the

    animation, drag the slider up or enter a negative value between -1 and -100.

    To begin the motion tween rapidly and decelerate the tween toward the end of the

    animation, drag the slider down or enter a positive value between 1 and 100.

    By default, the rate of change between tweened frames is constant. Easing creates a

    more natural appearance of acceleration or deceleration by gradually adjusting the rate of

    change.

    10. To rotate the selected item while tweening, choose an option from the Rotate menu:

    Choose none (the default setting) to prevent rotation.

    Choose Auto to rotate the object once in the direction requiring the least motion.

    Choose Clockwise (CW) or Counterclockwise (CCW) to rotate the object as indicated,

    and then enter a number to specify the number of rotations.

    Note: The rotation in step 9 is in addition to any rotation you applied to the ending frame in

    step 4.

    58

  • 8/8/2019 Graphics Sasikala

    59/68

    11. If you're using a motion path, select Orient to Path to orient the baseline of the tweened

    element to the motion path.

    12. Select the Sync checkbox in the Property inspector to synchronize the animation of

    graphic symbol instances with the main Timeline.

    Note: Modify > Frames > Synchronize Symbols and the Sync checkbox both recalculate the

    number of frames in a tween to match the number of frames allotted to it in the Timeline.

    13. If you're using a motion path, select snap to attach the tweened element to the motion path

    by its registration point.

    To create a motion tween using the Create Motion Tween command:

    1. Select an empty keyframe and draw an object on the Stage, or drag an instance of a symbol

    from the Library panel.

    Note: In order to create a tween, you must have only one item on the layer.

    2. Choose Insert > Create Motion Tween.

    If you drew an object in step 1, Flash automatically converts the object to a symbol and

    assigns it the name tween1.

    3. Click inside the frame where you want the animation to end, and choose Insert > Frame.

    4. Move the object, instance, or type block on the Stage to the desired position. Adjust the size

    of the element if you want to tween its scale. Adjust the rotation of the element if you want to

    tween its rotation. Deselect the object when you have completed adjustments.A key frame is automatically added to the end of the frame range.

    5. Drag the arrow next to the Easing value or enter a value to adjust the rate of change between

    tweened frames:

    To begin the motion tween slowly and accelerate the tween toward the end of the

    animation, drag the slider up or enter a value between -1 and -100.

    To begin the motion tween rapidly and decelerate the tween toward the end of the

    animation, drag the slider down or enter a positive value between 1 and 100.

    By default, the rate of change between tweened frames is constant. Easing creates a

    more natural appearance of acceleration or deceleration by gradually adjusting the rate of

    change.

    6. To rotate the selected item while tweening, choose an option from the Rotate menu:

    Choose Auto to rotate the object once in the direction requiring the least motion.

    59

  • 8/8/2019 Graphics Sasikala

    60/68

    Choose Clockwise (CW) or Counterclockwise (CCW) to rotate the object as indicated,

    and then enter a number to specify the number of rotations.

    Note: The rotation in step 6 is in addition to any rotation you applied to the ending frame in

    step 4.

    7. If you're using a motion path, select Orient to Path to orient the baseline of the tweened

    element to the motion path.

    8. Select Synchronize to ensure that the instance loops properly in the main movie.

    Use the Synchronize command if the number of frames in the animation sequence

    inside the symbol is not an even multiple of the number of frames the graphic instance

    occupies in the movie.

    9. If you're using a motion path, select snap to attach the tweened element to the motion

    path by its registration point.

    SAMPLE OUTPUT

    60

  • 8/8/2019 Graphics Sasikala

    61/68

    Shape Tweening in Flash MX

    AIM

    To perform using Shape tweening

    ALGORITHM

    By tweening shapes, you can create an effect similar to morphing, making one shape

    appear to change into another shape over time. Flash can also tween the location, size, and

    color of shapes. Tweening one shape at a time usually yields the best results. If you tween

    multiple shapes at one time, all the shapes must be on the same layer.

    To apply shape tweening to groups, instances, or bitmap images, you must first break these

    elements apart. To apply shape tweening to text, you must break the text apart twice to convert

    the text to objects. To control more complex or improbable shape changes, you use shape hints,

    which control how parts of the original shape move into the new shape. See Using shape hints.

    To tween a shape:

    1. Click a layer name to make it the current layer, and create or select a keyframe where

    you want the animation to start.

    2. Create or place the artwork for the first frame of the sequence. For best results, the

    frame should contain only one item (a graphic object or broken-apart group, bitmap,

    instance, or text block).

    3. Select the keyframe in the Timeline.

    4. Choose Window > Properties.

    5. In the Property inspector, select Shape from the Tween pop-up menu.

    6. Drag the arrow next to the Easing value or enter a value to adjust the rate of change

    between tweened frames:

    7. To begin the shape tween gradually and accelerate the tween toward the end of the

    animation, drag the slider down or enter a negative value between -1 and -100.

    8. To begin the shape tween rapidly and decelerate the tween toward the end of the

    animation, drag the slider up or enter a positive value between 1 and 100.

    9. By default, the rate of change between tweened frames is constant. Easing creates a

    more natural appearance of transformation by gradually adjusting the rate of change.

    10. Choose an option for Blend:

    61

  • 8/8/2019 Graphics Sasikala

    62/68

  • 8/8/2019 Graphics Sasikala

    63/68

    Use of Guide Layer in Flash MX

    AIM

    To create animation using guide layer

    ALGORITHM

    Motion guide layers let you draw paths along which tweened instances, groups, or text

    blocks can be animated. You can link multiple layers to a motion guide layer to have multiple

    objects follow the same path. A normal layer that is linked to a motion guide layer becomes a

    guided layer.

    To create a motion path for a tweened animation:

    1. Create a motion-tweened animation sequence as described in Tweening instances,

    groups, and type.

    2. If you select Orient to Path, the baseline of the tweened element will orient to the

    motion path. If you select Snap, the registration point of the tweened element will snap

    to the motion path.

    3. Do one of the following:

    4. Select the layer containing the animation and choose Insert > Motion Guide.

    5. Right-click (Windows) or Control-click (Macintosh) the layer containing the animation

    and choose Add Motion Guide from the context menu.

    6. Flash creates a new layer above the selected layer with a motion guide icon to the left

    of the layer name.

    7. Use the Pen, Pencil, Line, Circle, Rectangle, or Brush tool to draw the desired path.

    8. Snap the center to the beginning of the line in the first frame, and to the end of the line

    in the last frame.

    9. Note: For best snapping results, drag the symbol by its registration point.

    10. To hide the motion guide layer and the line so that only the object's movement is visible

    while you work, click in the Eye column on the motion guide layer.

    11. The group or symbol follows the motion path when you play the animation.

    To link layers to a motion guide layer, do one of the following:

    1. Drag an existing layer below the motion guide layer. The layer is indented under the

    motion guide layer. All objects on this layer automatically snap to the motion path.

    63

  • 8/8/2019 Graphics Sasikala

    64/68

    2. Create a new layer under the motion guide layer. Objects you tween on this layer are

    automatically tweened along the motion path.

    3. Select a layer below a motion guide layer. Choose Modify > Layer and select Guided in

    the Layer Properties dialog box.

    To unlink layers from a motion guide layer:

    1. Select the layer you want to unlink.

    2. Do one of the following:

    3. Drag the layer above the motion guide layer.

    4. Choose Modify > Layer and select Normal as the layer type in the Layer Properties

    dialog box.

    64

  • 8/8/2019 Graphics Sasikala

    65/68

  • 8/8/2019 Graphics Sasikala

    66/68

    EX NO. 10:

    Basic operations on image using any image editing software

    AIM

    To perform basic operations on a image using Photoshop

    ALGORITHM

    1. Open any image editing software say Photoshop

    2. Create a new file

    3. Load an image you want to edit

    4. Use lasso tool and select the required part in the image

    5. Open another file

    6. Move the selected part from file1 to file2 using move tool

    66

  • 8/8/2019 Graphics Sasikala

    67/68

    SAMPLE OUTPUT

    67

  • 8/8/2019 Graphics Sasikala

    68/68