CS2405 Computer Graphics Lab - AP/IT CS2405 Computer Graphics Lab - Manual Bresenhams Line Drawing EX NO.1(A) Aim: To implement Bresenhams line drawing Algorithm for drawing lines.

  • Published on
    22-Mar-2018

  • View
    212

  • Download
    0

Transcript

  • S.SAKTHI, AP/IT

    CS2405 Computer Graphics Lab - Manual

    Bresenhams Line Drawing

    EX NO.1(A)

    Aim: To implement Bresenhams line drawing Algorithm for drawing lines.

    Functions used:

    Line()

    The function line() is used to draw a line from(x1,y1)to (x2,y2)

    Syntax:

    line (x1,y1,x2,y2)

    initgraph().

    This function takes thee arguments and they are

    i).the video driver to be used (gd).

    ii).the graphics mode (gm).

    iii).the path name.

    Syntax:

    Initgraph(gd,gm,path)

    Algorithm:

    Step 1: Start

    Step 2: Get the values of the end points as(x1, y1) &(x2, y2)

    Step 3: Assign x=x1, y=y1;

    Step 4: Compute dx=x2-x1

    Step 5: Compute dy=y2-y1

    Step 6: Assign sx=x2-x1, sy=y2-y1

    Step 7: If dy>dx then interchange the values of dx and dy and assign exch=1

    Step 8: Compute p=2xdy-dx

    Step 9: Put a pixel on(x,y)

    Step 10: If exch=1, y=sy else x=x+sx

    Step 11: If p>0 and exch =1, x=x+sx else y=y+sy, p=p-2xdx

    Step 12: Compute p=p+2xdy

    Step 13: Do steps (9) t0 (12) for dx times

    Step 14: Stop

  • S.SAKTHI, AP/IT

    Program:

    #include

    #include

    #include

    #include

    int signs(int m);

    void bres_line(int,int,int,int);

    void main()

    {

    int gd=DETECT,gm;

    int x1,x2,y1,y2;

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

    cout>x1;

    cout>y1;

    cout>x2;

    cout>y2;

    bres_line(x1,y1,x2,y2);

    getch();

    closegraph();

    }

    void bres_line(int x1,int y1,int x2,int y2)

    {

    int x,y,sx,sy,p,temp,exch=0,i,dx,dy;

    x=x1;

    y=y1;

    dy=abs(y1-y2);

    dx=abs(x1-x2);

    sx=signs(x2-x1);

    sy=signs(y2-y1);

    if(dy>dx)

    {

    temp=dx;

    dx=dy;

    dy=temp;

    exch=1;

    }

    p=2*dy-dx;

    for(i=0;i

  • S.SAKTHI, AP/IT

    y+=sy;

    else

    x+=sx;

    if(p>=0)

    {

    if(exch==1)

    x+=sx;

    else

    y+=sy;p=p-2*dx;}

    p=p+2*dy;

    }

    }

    int signs(int m)

    {

    if(m0)

    return(1);

    else

    return(0);

    }

    Input:

    Enter starting value of X-Axis----->100

    Enter starting value of Y-Axis----->100

    Enter ending value of X-Axis----->200

    Enter ending value of Y-Axis----->200

    Output:

    Result:

    Thus the program to draw line using Bresenhams line drawing Algorithm was executed

    successfully.

    Circle Drawing

    EX NO 1(B)

  • S.SAKTHI, AP/IT

    Aim: To write a program to draw a circle using Bresenhams circle drawing Algorithm.

    Functions used:

    Circle()

    The function circle() is used to draw a circle using(x,y) as centre point.

    Syntax:

    circle (x,y,radius)

    initgraph().

    This function takes thee arguments and they are

    i).the video driver to be used (gd).

    ii).the graphics mode (gm).

    iii).the path name.

    Syntax:

    Initgraph(gd,gm,path)

    Putpixel ()

    The function putpixel() is used to place a pixel at particular coordinate

    Syntax:

    Putpixel(x,y,color)

    Algorithm:

    Step 1: Start

    Step 2: Get the center point as (xc,yc),get the radius as r.

    Step 3: Assign y=r,x=0

    Step 4: Calculate p=3-2r

    Step 5: If p

  • S.SAKTHI, AP/IT

    Step14: Put a pixel on (xc+y, yc-x, 15);

    Step 15: Put a pixel on (xc-y, yc+x, 15);

    Step 16: Put a pixel on (xc-y, yc-x, 15);

    Step 17: Stop.

    Program: #include

    #include

    #include

    #include

    void my_circle(int,int,int);

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

    void main()

    {

    int gd=DETECT,gm;

    int xc,yc,r;

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

    cout>xc;

    cout>yc;

    cout>r;

    my_circle(xc,yc,r);

    getch();

    closegraph();

    }

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

    {

    int x,y,p;

    p=3-2*r;

    x=0;

    y=r;

    while(x

  • S.SAKTHI, AP/IT

    {

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

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

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

    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);

    }

    Input:

    Enter X-Axis----->:100

    Enter Y-Axis----->:200

    Enter radius----->: 40

    Output:

    Result: Thus the program to draw circle using Bresenhams circle drawing Algorithm was

    executed successfully.

    Ellipse Drawing

    EX NO 1(C)

    Aim: To write a program to draw a ellipse using Bresenhams ellipse drawing Algorithm.

    Functions used:

  • S.SAKTHI, AP/IT

    initgraph().

    This function takes thee arguments and they are

    i).the video driver to be used (gd).

    ii).the graphics mode (gm).

    iii).the path name.

    Syntax:

    Initgraph(gd,gm,path)

    Putpixel ()

    The function putpixel() is used to place a pixel at particular coordinate

    Syntax:

    Putpixel(x,y,color)

    Algorithm: Step 1: Start

    Step 2: Get the center point as(x1, y1)

    Step 3: Get the length of semi-major, semi-minor axes as r1 & r2

    Step 4: Calculate t=pi/180

    Step 5: Initialise i=0;

    Step 6: Compute d=i*t

    Step 7: Compute x=x1+y1*sin(d), y=y1+r2*cos(d).

    Step 8: Put a pixel on(x,y)

    Step 9: Increment I by 1

    Step 10: Repeat steps(6) to (9) until i>y1;

  • S.SAKTHI, AP/IT

    coutr1;

    coutr2;

    for(i=0;i

  • S.SAKTHI, AP/IT

    Functions used:

    Circle()

    The function circle() is used to draw a circle using(x,y) as centre point.

    Syntax:

    circle (x,y,radius)

    initgraph().

    This function takes thee arguments and they are

    i).the video driver to be used (gd).

    ii).the graphics mode (gm).

    iii).the path name.

    Syntax:

    Initgraph(gd,gm,path)

    Putpixel ()

    The function putpixel() is used to place a pixel at particular coordinate

    Syntax:

    Putpixel(x,y,color)

    Algorithm:

    Step 1: Start

    Step 2: Get the center point as (xc,yc),get the radius as r.

    Step 3: Assign y=r,x=0

    Step 4: Calculate p=3-2r

    Step 5: If p

  • S.SAKTHI, AP/IT

    {

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

    int p=2*dy-dx;

    int twoDy=2*dy,twoDyDx=2*(dy-dx);

    int x,y,xEnd;

    /* Determine which point to use as start,which as end */

    if(xa>xb)

    {

    x=xb;

    y=yb;

    xEnd=xa;

    }

    else

    {

    x=xa;

    y=ya;

    xEnd=xb;

    }

    putpixel(x,y,15);

    while(x

  • S.SAKTHI, AP/IT

    {

    y;

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

    }

    circlePlotPoints(xCenter,yCenter,x,y);

    }

    }

    void circlePlotPoints(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);

    }//Ellipse drawing

    void ellipsemidpoint(int xcenter,int ycenter,int rx,int ry)

    {

    int rx2=rx*rx;

    int ry2=ry*ry;

    int tworx2=2*rx2;

    int twory2=2*ry2;

    int p,x=0,y=ry,px=0;

    int py=tworx2*y;

    void ellipseplotpoints(int,int,int,int);

    ellipseplotpoints(xcenter,ycenter,x,y);

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

    while(px

  • S.SAKTHI, AP/IT

    py-=tworx2;

    if(p>0)

    p+=rx2-py;

    else

    {

    x++;

    px+=twory2;

    p+=rx2-px+px;

    }

    ellipseplotpoints(xcenter,ycenter,x,y);

    }

    }

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

    {

    putpixel(xcenter+x,ycenter+y,5);

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

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

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

    }

    void main()

    {

    int ch,c;

    co:

    clrscr();

    printf(\n\t\tBRESENHAM BDRAWINGS\n);

    printf(\n\t\t1-Line drawing);

    printf(\n\t\t2-Circle drawing);

    printf(\n\t\t3-Ellipse drawing);

    printf(\n\t\t4-Exit);

    printf(\nEnter your choice :);

    scanf(%d,&ch);

    int gdriver = DETECT, gmode;

    initgraph(&gdriver, &gmode, );

    switch(ch)

    {

    case 1:

    int x1,y1,x2,y2;

    printf(Enter the starting co-ordinates: );

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

    printf(Enter the ending co-ordinates: );

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

    lineBres(x1,y1,x2,y2);

    getch();

    printf(\n\n\n\n\n\n\n\n\nDo u continue(1-yes,0-no):);

    scanf(%d,&c);

    if (c==1)

    goto co;

  • S.SAKTHI, AP/IT

    break;

    case 2:

    int xc1,yc1,r;

    printf(Enter the centre co-ordinates: );

    scanf(%d %d,&xc1,&yc1);

    printf(Enter the radius: );

    scanf(%d,&r);

    circleMidPoint(xc1,yc1,r);

    getch();

    printf(\n\n\n\n\n\n\n\n\nDo u continue(1-yes,0-no):);

    scanf(%d,&c);

    goto co;

    break;

    case 3:

    int xc,yc,rx,ry;

    printf(Enter the value of xcenter and ycenter co-ordinates: );

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

    printf(Enter the radius of x and y: );

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

    ellipsemidpoint(xc,yc,rx,ry);

    getch();

    printf(\n\n\n\n\n\n\n\n\nDo u continue(1-yes,0-no):);

    scanf(%d,&c);

    if (c==1)

    goto co;

    break;

    case 4: break;

    }

    }

    output

    Result:

    Thus the program to draw. Implementation of Line, Circle and ellipse Attributes was

    executed successfully.

  • S.SAKTHI, AP/IT

    2D TRANSFORMATION

    EX NO 3

    Aim: To perform the 2D transformation such as translation, rotation, scaling, shearing,

    Reflection

    Line() The function line() is used to draw a line from(x1,y1)to (x2,y2)

    Syntax: line (x1,y1,x2,y2)

    initgraph(). This function takes thee arguments and they are

    i).the video driver to be used (gd).

    ii).the graphics mode (gm).

    iii).the path name.

  • S.SAKTHI, AP/IT

    Syntax: Initgraph(gd,gm,path)

    Algorithm:

    Step1. Declare the variables xa,ya,xa1,ya1 of array type.

    Step2.Declare the variables gd,gm,n,i,op,tx,ty,xf,yf,rx,ry.

    Step3. Initialise the graphics function.

    Step4. Input the number of points.

    Step5. Input the value of co-ordinate according to number of points.

    Step6. Using switch statement selects the option to perform

    translation, rotation, scaling, reflection and shearing.

    Step7. Translation:

    a).input the translation vector

    b).add the translation vectors with the coordinates

    xa1[i]=xa[i]=tx, ya1[i]=ya[i]=ty,

    c).using the function line,display the object before and after translation.

    Step8. Rotation:

    a). input the rotation angle

    b). using formula theta=(theta*3.14)/180

    c).input the value of reference point

    d). calculate new coordinate point using formula

    xa1[i]=xf+(xa[i]-xf)*cos(theta)-(ya[i]-yf)*sin(theta),

    ya1[i]=yf+(xa[i]-xf)*sin(theta)-(ya[i]-yf)*cos(theta),

    e). using the function line,display the object before and after rotation.

    Step9. Scaling:

    a).input the scaling factor and reference point

    b).calculate new coordinate point using formula

    xa1[i]=(xa[i]*sx+rx*(1-sx),

    ya1 [i] = (ya[i]*sy+ry*(1-sy)

    c). using the function line, display the object before and after scaling.

    Step10. Shearing:

    a).input the shearing value and reference point.

    b). input the shear direction x or y

    i).if direction x

    xa1[i]=xa[i]+shx*(ya[i]-yref)

    ii).otherwise

    ya1[i]=ya[i]+shy*(xa[i]-xref)

    iii). using the function line, display the object before and after shearing.

    Step11. Reflection:

    a).display the object before reflection using the function line

    b). display the object after reflection using the function line

  • S.SAKTHI, AP/IT

    Step12. Stop.

    Program:

    #include

    #include

    #include

    #include

    #include

    void main()

    {

    int gd,gm,n,i,xa[10],ya[10],op,tx,ty,xa1[10],ya1[10],theta,xf,yf,rx,ry,sx,sy,shx,shy,xref,yref;

    char d;

    gd=DETECT;

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

    coutn;

    for(i=0;i

  • S.SAKTHI, AP/IT

    line(xa1[i],ya1[i],xa1[(i+1)%n],ya1[(i+1)%n]);

    }

    getch();

    cleardevice();

    break;

    case 2:

    couttheta;

    theta=(theta*3.14)/180;

    coutxf>>yf;

    for(i=0;i

  • S.SAKTHI, AP/IT

    for(i=0;i>shy;

    coutxref>>yref;

    coutd;

    if(d=='x')

    {

    for(i=0;i

  • S.SAKTHI, AP/IT

    cleardevice();

    break;

    case 6:

    exit(0);

    break;

    }

    }while(op!=6);

    }

    Input & Output: enter the no of points:3

    enter the coordinates 1:50 150

    enter the coordinates 2:50 50

    enter the coordinates 3:75 150

    menu

    1. translation

    2. rotation

    3. scaling

    4.shearing

    5.reflection

    6.exit1

    enter the translation vector:30 40

    Before translation After Translation

    menu

    1. translation

    2. rotation

    3. scaling

    4.shearing

    5.reflection

    6.exit 2

    enter the rotation angle:40

    enter the reference points:100 100

    before rotation after rotation

  • S.SAKTHI, AP/IT

    menu

    1. translation

    2. rotation

    3. scaling

    4.shearing

    5.reflection

    6.exit 3

    Enter the scaling factor: 3 4

    Enter the reference points: 30 40

    Before scaling after scaling

  • S.SAKTHI, AP/IT

    menu

    1. translation

    2. rotation

    3. scaling

    4.shearing

    5.reflection

    6.exit 4

    Enter the shear value: 3 4

    Enter the reference point: 20 30

    Enter the shear direction x or y: X

    Before shearing After shearing

    menu

    1. translation

    2. rotation

    3. scaling

    4.shearing

    5.reflection

    6.exit 5

    Before reflection after reflection

  • S.SAKTHI, AP/IT

    menu

    1. translation

    2. rotation

    3. scaling

    4.shearing

    5.reflection

    6.exit 6

    Result: Thus the program for 2D transformation was executed successfully

    Composite 2D Transformations

    Exno(4)

    Aim: To perform the Composite 2D Transformations

    Line() The function line() is used to draw a line from(x1,y1)to (x2,y2)

    Syntax: line (x1,y1,x2,y2)

    initgraph(). This function takes thee arguments and they are

    i).the video driver to be used (gd).

    ii).the graphics mode (gm).

    iii).the path name.

    Syntax: Initgraph(gd,gm,path)

    Algorithm:

    Step1. Declare the variables xa,ya,xa1,ya1 of array type.

    Step2.Declare the variables gd,gm,n,i,op,tx,ty,xf,yf,rx,ry.

  • S.SAKTHI, AP/IT

    Step3. Initialise the graphics function.

    Step4. Input the number of points.

    Step5. Input the value of co-ordinate according to number of points.

    Step6. Using switch statement selects the option to perform

    translation, rotation, scaling, reflection and shearing.

    Step7.

    a).input the translation vector

    b).add the translation vectors with the coordinates

    xa1[i]=xa[i]=tx, ya1[i]=ya[i]=ty,

    c).using the function line,display the object before and after translation.

    Step8.

    a). input the rotation angle

    b). using formula theta=(theta*3.14)/180

    c).input the value of reference point

    d). calculate new coordinate point using formula

    xa1[i]=xf+(xa[i]-xf)*cos(theta)-(ya[i]-yf)*sin(theta),

    ya1[i]=yf+(xa[i]-xf)*sin(theta)-(ya[i]-yf)*cos(theta),

    e). using the function line,display the object before and after rotation.

    Step9.

    a).input the scaling factor and reference point

    b).calculate new coordinate point using formula

    xa1[i]=(xa[i]*sx+rx*(1-sx),

    ya1 [i] = (ya[i]*sy+ry*(1-sy)

    c). using the function line, display the object before and after scaling.

    Step10.: Composite 2D Transformations

    a).input the shearing value and reference point.

    b). input the shear direction x or y

    i).if direction x

    xa1[i]=xa[i]+shx*(ya[i]-yref)

    ii).otherwise

    ya1[i]=ya[i]+shy*(xa[i]-xref)

    iii). using the function line, display the object before and after shearing.

    Step11.

    a).display the object before reflection using the function line

    b). display the object after reflection using the function line

    Step12. Stop.

    Program

  • S.SAKTHI, AP/IT

    #include

    #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

  • S.SAKTHI, AP/IT

    }

    }

    void scaling(int n,float c[][3],float sx,float sy)

    {

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

    int i=0,j;

    for(i=0;i

  • S.SAKTHI, AP/IT

    }

    do

    {

    clrscr();

    cleardevice();

    printf(\n\t\t\t * * * MENU * * *);

    printf(\n\t 1) TRANSLATION);

    printf(\n\t 2) SCALING );

    }while(cho!=4);

    getch();

    closegraph();

    }

    OUTPUT:

    Before scaling after scaling

    Result: Thus the program for Composite 2D transformation was executed successfully

  • S.SAKTHI, AP/IT

    Cohen-Sutherland Clipping& Windowing

    EXNO 5

    Aim: To implement Cohen-Sutherland clipping& WindowingAlgorithm.

    Functions used:

    Line() The function line() is used to draw a line from(x1,y1)to (x2,y2)

    Syntax: line (x1,y1,x2,y2)

    initgraph(). This function takes thee arguments and they are

    i).the video driver to be used (gd).

    ii).the graphics mode (gm).

    iii).the path name.

    Syntax: Initgraph(gd,gm,path)

    Setcolor(). This function changes the drawing colour.

    Syntax: Setcolor(value of the color)

    Settextstyle(). The function settextstyle() is used to change the style of the text.

  • S.SAKTHI, AP/IT

    Syntax: Settextstyle(font,direction,charsize)

    Where font is the constant value or the font filename, direction is the number either 0 or

    1, which makes the output to display in horizontal, or in vertical direction, charsize is the

    character size or magnification factor and it varies from 1 to 10.

    Outtext(). This function display a text message on upper left of the screen

    Syntax: Outtext(message);

    Algorithm:

    Step 1. Create a class sulc with functions drawwindow, drawline, setcode, visibility and

    reset endpoint.

    Step 2. Using the function line set the parameters to draw window.

    Step 3. Using the function defined in class sulc, setcode is used to save the line inside the

    window and to the line outside the window.

    Step 4. Using the function visibility

    i).check the code to know the points inside or outside the window.

    ii).if the code value is zero the point is inside the window.

    Step 5. Using the function reset end point

    i). if the code value for the line is outside the window.

    ii).reset the endpoint to the boundary of the window.

    Step 6. Initialize the graphics functions

    Step 7. Declare the variables x1, x2, y1, y2 of array type.

    Step 8. Get the value of two endpoints x1, y1 and x2, y2 to draw the line.

    Step 9. Using the object c, display the window before clipping.

    Step 10. Using the function setcode, visibility display the clipped window only with lines

    inside the window class was displayed after clipping.

    Program:

    #include

    #include

    #include

    #include

    typedef struct coord

    {

    int x,y;

    char code[4];

    }pt;

  • S.SAKTHI, AP/IT

    class sulc

    {

    public:

    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 sulc::drawwindow()

    {

    setcolor(WHITE);

    line(150,100,450,100);

    line(450,100,450,350);

    line(450,350,150,350);

    line(150,350,150,100);

    }

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

    {

    setcolor(c1);

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

    }

    pt sulc::setcode(pt p)

    {

    pt ptemp;

    if(p.y350)

    ptemp.code[1]='1';

    else

    ptemp.code[1]='0';

    if(p.y>450)

    ptemp.code[2]='1';

    else

    ptemp.code[2]='0';

    if(p.y

  • S.SAKTHI, AP/IT

    int i,flag=0;

    for(i=0;i

  • S.SAKTHI, AP/IT

    temp.code[i]=p1.code[i];

    if(temp.y=100)

    return(temp);

    }

    else

    return(p1);

    }

    void main()

    {

    int gd=DETECT,gm,v;

    sulc c1;

    pt p1,p2,ptemp;

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

    int x1[10],y1[10],x2[10],y2[10];

    cleardevice();

    int i,n;

    settextstyle(4,0,4);

    outtext("cohen sutherland line clipping");

    coutn;

    for(i=0;i>y1[i];

    coutx2[i]>>y2[i];

    }

    cleardevice();

    settextstyle(0,0,3);

    outtext("before clipping");

    c1.drawwindow();

    for(i=0;i

  • S.SAKTHI, AP/IT

    p1=c1.setcode(p1);

    p2=c1.setcode(p2);

    v=c1.visibility(p1,p2);

    switch(v)

    {

    case 0:

    c1.drawwindow();

    c1.drawline(p1,p2,15);

    break;

    case 1:

    c1.drawwindow();

    break;

    case 2:

    p1=c1.resetendpt(p1,p2);

    p2=c1.resetendpt(p2,p1);

    c1.drawwindow();

    c1.drawline(p1,p2,15);

    break;

    }

    }

    getch();

    closegraph();

    }

    Input:

    Enter the no.of lines: 1

    Enter end-point1(x1,y1):30 40

    Enter end-point1(x2,y2):300 400

    Output:

    Before clipping

    After clipping

  • S.SAKTHI, AP/IT

    Result: Thus the program to implement Cohen-Sutherland clipping& Windowing is executed

    successfully

    Sutherland Hodgeman Polygon clipping Algorithm

    EX NO. (6)

    Aim: To implement Sutherland Hodgeman Polygon clipping Algorithm

    Functions used:

    Line() The function line() is used to draw a line from(x1,y1)to (x2,y2)

    Syntax: line (x1,y1,x2,y2)

    initgraph(). This function takes thee arguments and they are

    i).the video driver to be used (gd).

    ii).the graphics mode (gm).

    iii).the path name.

    Syntax: Initgraph(gd,gm,path)

    Setcolor(). This function changes the drawing colour.

    Syntax: Setcolor(value of the color)

    Settextstyle(). The function settextstyle() is used to change the style of the text.

    Syntax: Settextstyle(font,direction,charsize)

  • S.SAKTHI, AP/IT

    Where font is the constant value or the font filename, direction is the number either 0 or

    1, which makes the output to display in horizontal, or in vertical direction, charsize is the

    character size or magnification factor and it varies from 1 to 10.

    Outtext(). This function display a text message on upper left of the screen

    Syntax: Outtext(message);

    Algorithm:

    Step 1. Create a class sulc with functions drawwindow, drawline, setcode, visibility and

    reset endpoint.

    Step 2. Using the function line set the parameters to draw window.

    Step 3. Using the function defined in class sulc, setcode is used to save the line inside the

    window and to the line outside the window.

    Step 4. Using the function visibility

    i).check the code to know the points inside or outside the window.

    ii).if the code value is zero the point is inside the window.

    Step 5. Using the function reset end point

    i). if the code value for the line is outside the window.

    ii).reset the endpoint to the boundary of the window.

    Step 6. Initialize the graphics functions

    Step 7. Declare the variables x1, x2, y1, y2 of array type.

    Step 8. Get the value of two endpoints x1, y1 and x2, y2 to draw the line.

    Step 9. Using the object c, display the window before clipping.

    Step 10. Using the function setcode, visibility display the clipped window only with lines

    inside the window class was displayed after clipping.

    Program:

    #include

    #include

    #include

    #include

    #include

    #include

    int pixels[2][4];

    float xn1,xn2,yn1,yn2,x3,y3,m;

    int xmin,ymin,xmax,ymax,x1,y1,x2,y2;

    int choice,ed[20],num;

    void su_co(int x1,int y1,int x2,int y2,int xmin,int ymin,int xmax,int ymax)

    {

    int i,j,f1;

  • S.SAKTHI, AP/IT

    for(i=0;i

  • S.SAKTHI, AP/IT

    {

    xn1=x1+(ymin-y1)/m;

    yn1=ymin;

    }

    if(pixels[0][2]==1)

    {

    yn1=y1+(xmax-x1)*m;

    xn1=xmax;

    }

    if(pixels[0][3]==1)

    {

    yn1=y1+(xmin-x1)*m;

    xn1=xmin;

    }

    if(pixels[1][0]==1)

    {

    xn2=x2+(ymax-y2)/m;

    yn2=ymax;

    }

    if(pixels[1][1]==1)

    {

    xn2=x2+(ymin-y2)/m;

    yn2=ymin;

    }

    if(pixels[1][2]==1)

    {

    yn2=y2+(xmax-x2)*m;

    xn2=xmax;

    }

    if(pixels[1][3]==1)

    {

    yn2=y2+(xmin-x2)*m;

    xn2=xmin;

    }

    line(320+xn1,240-yn1,320+xn2,240-yn2);

    break;

    }

    }

    void cohen()

    {

    clearviewport();

    line(320+xmin,240-ymin,320+xmin,240-ymax);

    line(320+xmin,240-ymax,320+xmax,240-ymax);

    line(320+xmax,240-ymax,320+xmax,240-ymin);

    line(320+xmax,240-ymin,320+xmin,240-ymin);

    line(320+x1,240-y1,320+x2,240-y2);

    getch();

  • S.SAKTHI, AP/IT

    cleardevice();

    line(320+xmin,240-ymin,320+xmin,240-ymax);

    line(320+xmin,240-ymax,320+xmax,240-ymax);

    line(320+xmax,240-ymax,320+xmax,240-ymin);

    line(320+xmax,240-ymin,320+xmin,240-ymin);

    su_co(x1,y1,x2,y2,xmin,ymin,xmax,ymax);

    getch();

    }

    void main()

    {

    int gd=DETECT,gm,i,j;

    initgraph(&gd,&gm,..\\bgi);

    printf(\n\n\t\t\Enter the coordinate of the Clipping Window\");

    printf(\n\n\t\t\Enter X(min),Y(min)\:=);

    scanf(%d%d,&xmin,&ymin);

    printf(\n\n\t\t\Enter X(max),Y(max)\:=);

    scanf(%d%d,&xmax,&ymax);

    printf(\n\n\t\tEnter the coordinates of the Line);

    printf(\n\n\t\t Enter X(1) & Y(1):);

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

    printf(\n\n\t\t Enter X(2) & Y(2):);

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

    clrscr();

    cohen();

    }

    Output:

    Result

    Sutherland Hodgeman Polygon clipping Algorithm is executed

  • S.SAKTHI, AP/IT

    3D- Transformation

    EXNO(7)

    Aim: To perform 3D transformations such as translation, rotation and scaling.

    Functions used:

    Line() The function line() is used to draw a line from(x1,y1)to (x2,y2)

    Syntax: line (x1,y1,x2,y2)

    initgraph(). This function takes thee arguments and they are

    i).the video driver to be used (gd).

    ii).the graphics mode (gm).

    iii).the path name.

    Syntax: Initgraph(gd,gm,path)

    Algorithm: Step 1. Create a class cube with function draw cube.

    Step 2. Use the function draw cube to draw a cube using eight points by means of

    functions line.

    Step 3. Declare the variables x1, y1, x2, y2, x3, y3, in array type which of data type int.

    Step 4.Declare the variables theta,op,ch,tx,ty,sx,sy,sz,lz+xy,zf,i,x,y,z.

    Step 5.Initialise graphics functions.

    Step 6.Input the first point in the cube.

    Step 7.Input the size of the edge.

    Step 8.Create an object to call the function.

    Step 9.Using switch operation select the operation to perform translation, rotation,scaling.

    Step 10.Translation

    a).input the translation vectortx,ty,tz.

    b).calculate points using formula

    x3[i]=x1[i]+tx.

    y3[i]=y1[i]+ty

    z3[i]=z1[i]+tz.

    x4[i]=x3[i]+z3[i]/2

    y4[i]=y3[i]+z3[i]/2

  • S.SAKTHI, AP/IT

    c).using the function line, display the object before and after translation.

    Step11. Rotation:

    a). input the rotation angle

    b). using formula theta=(theta*3.14)/180

    c).input the direction in x,y,z axis

    d). if the direction is along x axis,

    x3[i]=x1[i].

    y3[i]=y1[i]*cos(theta)-z1[i]*sin(theta),

    y3[i]=y1[i]*sin(theta)-z1[i]*cos(theta),

    if the direction is along yaxis,

    y3[i]=y1[i].

    z3[i]=z1[i]*cos(theta)-x1[i]*sin(theta),

    x3[i]=z1[i]*sin(theta)-x1[i]*cos(theta),

    if the direction is along z axis,

    z3[i]=z1[i].

    x3[i]=x1[i]*cos(theta)-y1[i]*sin(theta),

    y3[i]=x1[i]*sin(theta)-y1[i]*cos(theta),

    e).calculate the points using the formula

    x4[i]=x3[i]+z3[i]/2

    y4[i]=y3[i]+z3[i]/2

    f). using the function line,display the object before and after rotation.

    Step12. Scaling:

    a).input the scaling factor and reference point

    b).calculate coordinate point using formula

    x3[i]=xf+(x1[i]*sx+xf*(1-sx),

    y3 [i] =yf+ (y1[i]*sy+yf*(1-sy)

    z3 [i] =zf+ (z1[i]*sz+zf*(1-sz)

    c). calculate the points using the formula

    x4[i]=x3[i]+z3[i]/2

    y4[i]=y3[i]+z3[i]/2

    d). using the function line, display the object before and after scaling.

    Step13. Stop.

    Program: #include

    #include

    #include

    #include

    #include

    class cube

    {

    public:

    void drawcube(int x1[],int y1[])

  • S.SAKTHI, AP/IT

    {

    int i;

    for(i=0;i

  • S.SAKTHI, AP/IT

    }

    cube c;

    getch();

    cleardevice();

    do

    {

    cout

  • S.SAKTHI, AP/IT

    {

    x3[i]=x1[i];

    y3[i]=y1[i]*cos(theta)-z1[i]*sin(theta);

    z3[i]=y1[i]*sin(theta)+z1[i]*cos(theta);

    }

    }

    else

    if(op==2)

    {

    for(i=0;i

  • S.SAKTHI, AP/IT

    coutxf>>yf>>zf;

    for(i=0;i

  • S.SAKTHI, AP/IT

    Before translation After translation

    Result: Thus the program for 3D transformation was executed successfully.

    Composite 3D transformations

    EXno(8)

  • S.SAKTHI, AP/IT

    Aim: To perform. Composite 3D transformations

    Functions used:

    Line() The function line() is used to draw a line from(x1,y1)to (x2,y2)

    Syntax: line (x1,y1,x2,y2)

    initgraph(). This function takes thee arguments and they are

    i).the video driver to be used (gd).

    ii).the graphics mode (gm).

    iii).the path name.

    Syntax: Initgraph(gd,gm,path)

    Algorithm: Step 1. Create a class cube with function draw cube.

    Step 2. Use the function draw cube to draw a cube using eight points by means of

    functions line.

    Step 3. Declare the variables x1, y1, x2, y2, x3, y3, in array type which of data type int.

    Step 4.Declare the variables theta,op,ch,tx,ty,sx,sy,sz,lz+xy,zf,i,x,y,z.

    Step 5.Initialise graphics functions.

    Step 6.Input the first point in the cube.

    Step 7.Input the size of the edge.

    Step 8.Create an object to call the function.

    Step 9.Using switch operation select the operation to perform translation, rotation,scaling.

    Step 10.

    a).input the translation vectortx,ty,tz.

    b).calculate points using formula

    x3[i]=x1[i]+tx.

    y3[i]=y1[i]+ty

    z3[i]=z1[i]+tz.

    x4[i]=x3[i]+z3[i]/2

    y4[i]=y3[i]+z3[i]/2

    c).using the function line, display the object before and after translation.

    Step11.

    a). input the rotation angle

  • S.SAKTHI, AP/IT

    b). using formula theta=(theta*3.14)/180

    c).input the direction in x,y,z axis

    d). if the direction is along x axis,

    x3[i]=x1[i].

    y3[i]=y1[i]*cos(theta)-z1[i]*sin(theta),

    y3[i]=y1[i]*sin(theta)-z1[i]*cos(theta),

    if the direction is along yaxis,

    y3[i]=y1[i].

    z3[i]=z1[i]*cos(theta)-x1[i]*sin(theta),

    x3[i]=z1[i]*sin(theta)-x1[i]*cos(theta),

    if the direction is along z axis,

    z3[i]=z1[i].

    x3[i]=x1[i]*cos(theta)-y1[i]*sin(theta),

    y3[i]=x1[i]*sin(theta)-y1[i]*cos(theta),

    e).calculate the points using the formula

    x4[i]=x3[i]+z3[i]/2

    y4[i]=y3[i]+z3[i]/2

    f). using the function line,display the object before and after rotation.

    Step12.

    a).input the scaling factor and reference point

    b).calculate coordinate point using formula

    x3[i]=xf+(x1[i]*sx+xf*(1-sx),

    y3 [i] =yf+ (y1[i]*sy+yf*(1-sy)

    z3 [i] =zf+ (z1[i]*sz+zf*(1-sz)

    c). calculate the points using the formula

    x4[i]=x3[i]+z3[i]/2

    y4[i]=y3[i]+z3[i]/2

    d). using the function line, display the object before and after scaling.

    Step13. Stop.

    Program: #include

    #include

    #include

  • S.SAKTHI, AP/IT

    #include

    #include

    #include

    #include

    int main(void)

    {

    int gdriver=DETECT,gmode;

    int xmax,ymax,tx,ty,tz,i,n,x[20],y[20],z[20],a[20],b[20],choice;

    initgraph(&gdriver,&gmode,h:\cplus);

    xmax=getmaxx();

    ymax=getmaxy();

    line(xmax/2,0,xmax/2,ymax/2);

    line(xmax/2,ymax/2,xmax,ymax/2);

    line(xmax/2,ymax/2,0,ymax);

    coutn;

    void draw(int x[],int y[],int z[],int n);

    coutchoice;

    switch(choice)

    {

    case 1: cout>y[i]>>z[i];

    x[i]+=xmax/2;

    y[i]=ymax/2-y[i];

    a[i]=x[i]-z[i];

    b[i]=y[i]-z[i];

    }

    for(i=0;i>ty>>tz;

    for(i=0;i

  • S.SAKTHI, AP/IT

    {

    line(x[i],y[i],x[(i+1)%n],y[(i+1)%n]);

    line(a[i],b[i],x[(i+1)%n]-z[(i+1)%n],y[(i+1)%n]-z[(i+1)%n]);

    line(x[i],y[i],a[i],b[i]);

    }

    break;

    case 2: cout>z[i];

    x[i]+=xmax/2;

    y[i]=ymax/2-y[i];

    a[i]=x[i]-z[i];

    b[i]=y[i]-z[i];

    }

    for(i=0;i

  • S.SAKTHI, AP/IT

    }

    case 3: break;

    }

    getch();

    closegraph();

    return 0;

    }

    Output: Enter the choice:1

    Enter the translation vector: 5,10,25

    Before translation After translation

    Result:

    Thus the program for Composite 3D transformations as executed successfully.

    Drawing three dimensional objects and Scenes

    EXNO(9)

    Aim: To write program to visualize the Drawing three dimensional objects and Scenes

  • S.SAKTHI, AP/IT

    Functions used:

    Line() The function line() is used to draw a line from(x1,y1)to (x2,y2)

    Syntax: line (x1,y1,x2,y2)

    initgraph(). This function takes thee arguments and they are

    i).the video driver to be used (gd).

    ii).the graphics mode (gm).

    iii).the path name.

    Syntax: Initgraph(gd,gm,path)

    Algorithm: Step 1. Create a class parallel.

    Step 2. Create a constructor parallel

    i).initialize graphics

    Step 3. Create a function initialize

    i).draw the x,y,z axis

    Step 4. Create a function projection

    i).get the reference angle alpha as j.

    ii).assign the value of k as 45.

    iii).compute the following

    fi=(3.14/180)*k;

    a1=(3.14/180)*j;

    z=pz1[1];

    i=z/tan(a1);

    i1=floor(i*cos(fi));

    i2=floor(i*sin(fi));

    iv).Calculate

    px3[1]=px1[1]+i1;

    py3[1]=py1[1]+i2;

    px3[2]=px1[2]+i1;

    py3[2]=py1[2]+i2;

    px3[3]=px1[3]+i1;

    py3[3]=py1[3]+i2;

    px3[4]=px1[4]+i1;

    py3[4]=py1[4]+i2;

    v).compute the following

    z=pz1[5];

    i=z/tan(a1);

    i1=floor(i*cos(fi));

    i2=floor(i*sin(fi));

    vi). calculate

  • S.SAKTHI, AP/IT

    px3[5]=px1[5]+i1;

    py3[5]=py1[5]+i2;

    px3[6]=px1[6]+i1;

    py3[6]=py1[6]+i2;

    px3[7]=px1[7]+i1;

    py3[7]=py1[7]+i2;

    px3[8]=px1[8]+i1;

    py3[8]=py1[8]+i2;

    vii).compute the values to screen coordinate value.

    viii).join the coordinate using line function

    ix).display the projected object.

    Program #include

    #include

    #include

    #include

    #include

    #include

    class parallel

    {

    public:

    int a,k;

    int gd,gm;

    int px[8],py[8],pz[8],px1[8],py1[8],pz1[8],px3[8],py3[8];

    parallel();

    void initialize();

    void drawobj();

    void proj(int);

    };

    parallel::parallel()

    {

    gd=DETECT;

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

    }

    void parallel::initialize()

    {

    px1[1]=100,py1[1]=100,pz1[1]=0;

    px1[2]=200,py1[2]=100,pz1[2]=0;

    px1[3]=200,py1[3]=200,pz1[3]=0;

    px1[4]=100,py1[4]=200,pz1[4]=0;

    px1[5]=100,py1[5]=100,pz1[5]=100;

    px1[6]=200,py1[6]=100,pz1[6]=100;

    px1[7]=200,py1[7]=200,pz1[7]=100;

    px1[8]=100,py1[8]=200,pz1[8]=100;

    }

    void parallel::drawobj()

    {

  • S.SAKTHI, AP/IT

    setcolor(WHITE);

    line(px1[1],py1[1],px1[2],py1[2]);

    line(px1[2],py1[2],px1[3],py1[3]);

    line(px1[3],py1[3],px1[4],py1[4]);

    line(px1[4],py1[4],px1[8]+50,py1[8]+50);

    line(px1[8]+50,py1[8]+50,px1[5]+50,py1[5]+50);

    line(px1[5]+50,py1[5]+50,px1[6]+50,py1[6]+50);

    line(px1[6]+50,py1[6]+50,px1[7]+50,py1[7]+50);

    line(px1[7]+50,py1[7]+50,px1[8]+50,py1[8]+50);

    line(px1[1],py1[1],px1[5]+50,py1[5]+50);

    line(px1[2],py1[2],px1[6]+50,py1[6]+50);

    line(px1[3],py1[3],px1[7]+50,py1[7]+50);

    line(px1[4],py1[4],px1[1],py1[1]);

    getch();

    }

    void parallel::proj(int j)

    {

    cleardevice();

    int z,xp,yp;

    int dx,dy;

    float a1,fi,i,i1,i2;

    k=45;

    fi=(3.14/180)*k;

    a1=(3.14/180)*j;

    z=pz1[1];

    i=z/tan(a1);

    i1=floor(i*cos(fi));

    i2=floor(i*sin(fi));

    px3[1]=px1[1]+i1;

    py3[1]=py1[1]+i2;

    px3[2]=px1[2]+i1;

    py3[2]=py1[2]+i2;

    px3[3]=px1[3]+i1;

    py3[3]=py1[3]+i2;

    px3[4]=px1[4]+i1;

    py3[4]=py1[4]+i2;

    z=pz1[5];

    i=z/tan(a1);

    i1=floor(i*cos(fi));

    i2=floor(i*sin(fi));

    px3[5]=px1[5]+i1;

    py3[5]=py1[5]+i2;

    px3[6]=px1[6]+i1;

    py3[6]=py1[6]+i2;

    px3[7]=px1[7]+i1;

    py3[7]=py1[7]+i2;

    px3[8]=px1[8]+i1;

  • S.SAKTHI, AP/IT

    py3[8]=py1[8]+i2;

    cout

  • S.SAKTHI, AP/IT

    Result:

    Thus the program to visualize the Drawing three dimensional objects and Scenes

    was executed successfully.

    Generating Fractal images

    EX NO(10)

    Aim:

  • S.SAKTHI, AP/IT

    To write program to draw the Generating Fractal images

    Functions used:

    Line() The function line() is used to draw a line from(x1,y1)to (x2,y2)

    Syntax: line (x1,y1,x2,y2)

    initgraph(). This function takes thee arguments and they are

    i).the video driver to be used (gd).

    ii).the graphics mode (gm).

    iii).the path name.

    Syntax: Initgraph(gd,gm,path)

    Algorithm: Step1:Pres()

    i).Initialise graphics.

    Step 2: Initialize()

    i).Draw x,y,z axis

    ii).Number all axis with regular interval 1 to 8

    step 3:Draw-obj

    i). Get the value of each p6 of 3D object

    ii).Convert the value to screen coordinate values

    iii).Join the coordinate using the function.

    Step 4:Proj()

    i).Get the reference point in z-axis as j

    ii).Get the position of the view plane as k

    iii).Assign the values for Zprp=j and Zrp=k

    iv).Compute

    z=pz1[1];

    u=(zprp-zvp)/(zprp-z);

    v).Calculate

    px3[1]=px1[1]*u;

    py3[1]=py1[1]*u;

    px3[2]=px1[2]*u;

    py3[2]=py1[2]*u;

    px3[3]=px1[3]*u;

    py3[3]=py1[3]*u;

    px3[4]=px1[4]*u;

    py3[4]=py1[4]*u;

    vi).Compute

    z=pz1[5];

  • S.SAKTHI, AP/IT

    u=(zprp-zvp)/(zprp-z);

    vii).Calculate

    px3[5]=px1[5]*u;

    py3[5]=py1[5]*u;

    px3[6]=px1[6]*u;

    py3[6]=py1[6]*u;

    px3[7]=px1[7]*u;

    py3[7]=py1[7]*u;

    px3[8]=px1[8]*u;

    py3[8]=py1[8]*u;

    viii).Compute the values to screen coordinate value.

    ix).Join the coordinate using line function.

    x).Display the projected object.

    Program: #include.

    #include

    #include

    #include

    #include

    #include

    class pres

    {

    public:

    int a,k;

    int gd,gm;

    int px[8],py[8],pz[8],px1[8],py1[8],pz3[8],px3[8],py3[8],pz1[8];

    pres();

    void initialize();

    void drawobj();

    void proj(int,int);

    };

    pres::pres()

    {

    gd=DETECT;

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

    }

    void pres::initialize()

    {

    px1[1]=200,py1[1]=200,pz1[1]=100;

    px1[2]=300,py1[2]=200,pz1[2]=100;

    px1[3]=300,py1[3]=300,pz1[3]=100;

    px1[4]=200,py1[4]=300,pz1[4]=100;

    px1[5]=200,py1[5]=200,pz1[5]=200;

    px1[6]=300,py1[6]=200,pz1[6]=200;

    px1[7]=300,py1[7]=300,pz1[7]=200;

  • S.SAKTHI, AP/IT

    px1[8]=200,py1[8]=300,pz1[8]=200;

    }

    void pres::drawobj()

    {

    setcolor(WHITE);

    line(px1[1],py1[1],px1[2],py1[2]);

    line(px1[2],py1[2],px1[3],py1[3]);

    line(px1[3],py1[3],px1[4],py1[4]);

    line(px1[4],py1[4],px1[8]+50,py1[8]+50);

    line(px1[8]+50,py1[8]+50,px1[5]+50,py1[5]+50);

    line(px1[5]+50,py1[5]+50,px1[6]+50,py1[6]+50);

    line(px1[6]+50,py1[6]+50,px1[7]+50,py1[7]+50);

    line(px1[7]+50,py1[7]+50,px1[8]+50,py1[8]+50);

    line(px1[1],py1[1],px1[5]+50,py1[5]+50);

    line(px1[2],py1[2],px1[6]+50,py1[6]+50);

    line(px1[3],py1[3],px1[7]+50,py1[7]+50);

    line(px1[4],py1[4],px1[1],py1[1]);

    getch();

    }

    void pres::proj(int j,int k)

    {

    cleardevice();

    int z,xp,yp;

    float zprp,zvp,u;

    zprp=j;zvp=k;

    z=pz1[1];

    u=(zprp-zvp)/(zprp-z);

    z=pz1[1];

    px3[1]=px1[1]*u;

    py3[1]=py1[1]*u;

    px3[2]=px1[2]*u;

    py3[2]=py1[2]*u;

    px3[3]=px1[3]*u;

    py3[3]=py1[3]*u;

    px3[4]=px1[4]*u;

    py3[4]=py1[4]*u;

    z=pz1[5];

    u=(zprp-zvp)/(zprp-z);

    px3[5]=px1[5]*u;

    py3[5]=py1[5]*u;

    px3[6]=px1[6]*u;

    py3[6]=py1[6]*u;

    px3[7]=px1[7]*u;

    py3[7]=py1[7]*u;

    px3[8]=px1[8]*u;

    py3[8]=py1[8]*u;

    cleardevice();

  • S.SAKTHI, AP/IT

    cout

Recommended

View more >