CS2405 Computer Graphics Lab - Computer Graphics Lab - Manual ... Implementation of Line, Circle and ellipse Attributes ... To write a program to draw a Line, Circle and ellipse Attributes .

  • Published on
    16-Mar-2018

  • View
    215

  • Download
    3

Embed Size (px)

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=g...

Recommended

View more >