CS2405 - COMPUTER GRAPHICS LAB - - COMPUTER GRAPHICS LAB 1. ... Line, Circle, Ellipse. 3. Implementation of Line, Circle and ellipse Attributes 4

  • Published on
    16-Mar-2018

  • View
    215

  • Download
    2

Embed Size (px)

Transcript

  • CS2405 - COMPUTER GRAPHICS LAB

    1. Output primitives. 2. Implementation of Bresenhams Algorithm Line, Circle, Ellipse.

    3. Implementation of Line, Circle and ellipse Attributes 4. Two Dimensional transformations - Translation, Rotation, Scaling,

    Reflection, Shear.

    5. Composite 2D Transformations 6. Cohen Sutherland 2D line clipping and Windowing

    7. Sutherland Hodgeman Polygon clipping Algorithm 8. Three dimensional transformations - Translation, Rotation, Scaling

    9. Composite 3D transformations 10. Drawing three dimensional objects and Scenes

    11. Generating Fractal images

  • BASIC GRAPHICS FUNCTION

    1) Initgraph ()

    initgraph() function initializes the graphics mode and clears the screen.

    Declaration:

    void far initgraph(int far *driver, int far *mode, char far *path)

    2) Detectgraph ()

    Detectgraph function determines the graphics hardware in the system, if the

    function finds a graphics adapter then it returns the highest graphics mode that the

    adapter supports.

    Declaration:

    void far detectgraph(int far *driver, int far *mode)

    Integer that specifies the graphics driver to be used. You can give graphdriver a value

    using a constant of the graphics_drivers enumeration type.

    3) Closegraph ()

    closegraph() function switches back the screen from graphcs mode to text mode.

    It clears the screen also.

    A graphics program should have a closegraph function at the end of graphics.

    Otherwise DOS screen will not go to text mode after running the program.

    4) Getpixel ()

    getpixel function returns the color of pixel present at location(x, y).

    Declaration:-

    int getpixel(int x, int y);

  • 5) Putpixel ()

    putpixel function plots a pixel at location (x, y) of specified color.

    Declaration:-

    void putpixel(int x, int y, int color);

    For example if we want to draw a GREEN color pixel at (35, 45) then we

    will write putpixel(35, 35, GREEN); in our c program, putpixel function

    can be used to draw circles, lines and ellipses using various algorithms.

    6) line()

    line function is used to draw a line from a point(x1,y1) to point(x2,y2) i.e.

    (x1,y1) and (x2,y2) are end points of the line.

    Declaration :-

    void line(int x1, int y1, int x2, int y2);

    7) lineto()

    lineto function draws a line from current position(CP) to the point(x,y),

    you can get current position using getx and gety function.

    8) circle()

    circle function is used to draw a circle with center (x,y) and third

    parameter specifies the radius of the circle.

    Declaration :-

    void circle(int x, int y, int radius);

  • 9)ellipse()

    Ellipse is used to draw an ellipse (x,y) are coordinates of center of the

    ellipse, stangle is the starting angle, end angle is the ending angle, and fifth

    and sixth parameters specifies the X and Y radius of the ellipse. To draw a

    complete ellipse strangles and end angle should be 0 and 360 respectively.

    Declaration :-

    void ellipse(int x, int y, int stangle, int endangle, int xradius, int yradius);

    10) drawpoly()

    drawpoly function is used to draw polygons i.e. triangle, rectangle,

    pentagon, hexagon etc.

    Declaration :-

    void drawpoly( int num, int *polypoints );

    num indicates (n+1) number of points where n is the number of vertices

    in a polygon, polypoints points to a sequence of (n*2) integers . Each pair of

    integers gives x and y coordinates of a point on the polygon. We specify (n+1)

    points as first point coordinates should be equal to (n+1)th to draw a complete

    figure.

    To understand more clearly we will draw a triangle using drawpoly,

    consider for example the array :-

    int points[] = { 320, 150, 420, 300, 250, 300, 320, 150};

    points array contains coordinates of triangle which are (320, 150), (420,

    300) and (250, 300). Note that last point(320, 150) in array is same as first.

  • 11) outtext ()

    outtext function displays text at current position.

    Declaration :-

    void outtext(char *string);

    12) outtextxy ()

    outtextxy function display text or string at a specified point(x,y) on the

    screen.

    Declaration :-

    void outtextxy(int x, int y, char *string);

    x, y are coordinates of the point and third argument contains the address

    of string to be displayed.

    13)rectangle()

    Rectangle function is used to draw a rectangle. Coordinates of left top

    and right bottom corner are required to draw the rectangle. left specifies the X-

    coordinate of top left corner, top specifies the Y-coordinate of top left corner,

    right specifies the X-coordinate of right bottom corner, bottom specifies the Y-

    coordinate of right bottom corner.

    Declaration :-

    void rectangle(int left, int top, int right, int bottom);

  • 14) floodfill()

    floodfill function is used to fill an enclosed area. Current fill pattern and

    fill color is used to fill the area.(x, y) is any point on the screen if (x,y) lies inside

    the area then inside will be filled otherwise outside will be filled,border specifies

    the color of boundary of area.

    Declaration :-

    void floodfill(int x, int y, int border);

    15)fillpoly()

    f illpoly function draws and fills a polygon. It require same arguments as

    drawpoly.

    Declaration :-

    void drawpoly( int num, int *polypoints );

    16)fillellipse()

    f illellipse function draws and fills a polygon.

    Declaration:-

    void fillellipse(int x, int y, int xradius, int yradius);

    x and y are coordinates of center of the ellipse, xradius and yradius are x

    and y radius of ellipse respectively.

  • An Example program using the basic graphic functions

    #include

    #include

    #include

    #include

    void main()

    {

    int gd=DETECT, gm;

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

    circle(100,100,50);

    outtextxy(75,170, "Circle");

    rectangle(200,50,350,150);

    outtextxy(240, 170, "Rectangle");

    ellipse(500, 100,0,360, 100,50);

    outtextxy(480, 170, "Ellipse");

    line(100,250,540,250);

    outtextxy(300,260,"Line");

    getch();

    closegraph();

    }

  • Ex.No:1

    Program:

    #include

    #include

    #include

    #include

    void main()

    {

    char ch='y';

    int gd=DETECT,gm,x1,y1,x2,y2,rad,sa,ea,xrad,yrad,i;

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

    while(ch=='y')

    {

    cleardevice();

    setbkcolor(9);

    outtextxy(100,150,"Enter 1 to get line");

    outtextxy(100,170,"2.Circle");

    outtextxy(100,190,"3.Box");

    outtextxy(100,210,"4.Arc");

    outtextxy(100,230,"5.Ellipse");

    outtextxy(100,250,"6.Rectangle");

    outtextxy(100,270,"7.Exit");

    ch=getch();

  • cleardevice();

    switch(ch)

    {

    case '1':

    line(100,200,300,400);

    break;

    case '2':

    circle(200,200,100);

    break;

    case '3':

    setfillstyle(5,4);

    bar(100,300,200,100);

    break;

    case '4':

    setfillstyle(5,4);

    arc(200,200,100,300,100);

    break;

    case '5':

    setfillstyle(5,4);

    fillellipse(100,100,50,100);

    break;

    case '6':

    settextstyle(DEFAULT_FONT,0,2);

  • outtextxy(120,140,"VEL TECH");

    line(100,100,100,300);

    line(300,300,100,300);

    line(100,100,300,100);

    line(300,100,300,300);

    break;

    case '7':

    closegraph();

    return;

    }

    ch='y';

    getch();

    }

    }

  • Output:

  • Ex.No:2a

    Program:

    #include #include

    #include #include

    void main()

    { int gd = DETECT, gm;

    int x1, y1, x2, y2,dx,dy,steps,k; float xincrement,yincrement,x,y;

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

    printf("Enter the Starting Point of x axis : "); scanf("%d", &x1);

    printf("Enter the Starting of y axis : "); scanf("%d", &y1);

    printf("Enter the End Point of x axis : "); scanf("%d", &x2);

    printf("Enter the End Point of y axis : ");

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

    dx = x2 x1; dy = y2 y1;

    x=x1;

    y=y1; if(abs(dx) > abs(dy))

    steps=abs(dx); else

    steps=abs(dy); xincrement=dx/(float)steps;

    yincrement=dy/(float)steps;

    putpixel(ceil(x), ceil(y), RED); for(k=1;k

  • Output

    Enter the Starting Point of x axis : 100 Enter the Starting of y axis : 100

    Enter the End Point of x axis : 200 Enter the End Point of y axis : 200

  • Ex.No:2b

    Program

    #include

    #include #include

    #include

    void main() {

    int gd = DETECT, gm; int x,y,x1,y1,x2,y2,p,dx,dy,twody,twodydx,xend;

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

    printf("\nEnter the x-coordinate of the starting point :"); scanf("%d",&x1);

    printf("\nEnter the y-coordinate of the starting point :"); scanf("%d",&y1);

    printf("\nEnter the x-coordinate of the Ending point :"); scanf("%d",&x2);

    printf("\nEnter the y-coordinate of the ending point :");

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

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

    p=2*dy-dx;

    twody=2*dy; twodydx=2*(dy-dx);

    if (x1>x2) {

    x=x2; y=y2;

    xend=x1;

    } else

    { x=x1;

    y=y1;

    xend=x2; }

    putpixel(x,y,RED); while(x

  • x++;

    if (p

  • Output

    Enter the x-coordinate of the starting point : 100

    Enter the y-coordinate of the starting point : 100 Enter the x-coordinate of the Ending point : 200

    Enter the y-coordinate of the ending point : 200

  • Ex.No:2c

    Program

    #include

    #include #include

    #include

    int x,y,r,p,xcenter, ycenter; void circleplot(int,int,int,int);

    void main() {

    int gd = DETECT, gm;

    initgraph(&gd,&gm,"..\\BGI:"); printf("\nEnter the x-coordinate of the centre point :");

    scanf("%d",&xcenter); printf("\nEnter the y-coordinate of the centre point :");

    scanf("%d",&ycenter); printf("\nEnter the radius :");

    scanf("%d",&r);

    x=0; y=r;

    p=1-r; while (x

  • putpixel(xcenter+x,ycenter-y,10);

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

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

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

  • Output

    Enter the x-coordinate of the centre point : 100

    Enter the y-coordinate of the centre point : 100 Enter the radius : 50

  • Ex.No:2d

    Program

    #include

    #include #include

    #include int x,y,r,p,xcenter, ycenter;

    void circleplot(int,int,int,int); void main()

    {

    int gd = DETECT, gm; initgraph(&gd,&gm,"..\\BGI:");

    printf("\nEnter the x-coordinate of the centre point :"); scanf("%d",&xcenter);

    printf("\nEnter the y-coordinate of the centre point :");

    scanf("%d",&ycenter); printf("\nEnter the radius :");

    scanf("%d",&r); x=0;

    y=r; p=3-(2*r);

    while (x

  • putpixel(xcenter+x,ycenter-y,10);

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

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

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

  • Output

    Enter the x-coordinate of the centre point : 100 Enter the y-coordinate of the centre point : 100

    Enter the radius : 50

  • Ex.No:2e

    Program

    #include #include

    #include #include

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

    void ellipseplot(int,int,int,int); int xcenter,yc,rx,ry;

    void main() {

    int gd=DETECT,gm;

    int xcenter,ycenter,rx,ry; long rx2,ry2,tworx2,twory2,p,x,y,px,py;

    initgraph(&gd,&gm,"..\\BGI:"); printf("\nEnter the x-coordinate of the centre point :");

    scanf("%d",&xcenter); printf("\nEnter the y-coordinate of the centre point :");

    scanf("%d",&ycenter);

    printf("\nEnter the x-coordinate of the radius :"); scanf("%d",&rx);

    printf("\nEnter the y-coordinate of the radius :"); scanf("%d",&ry);

    rx2=rx*rx;

    ry2=ry*ry; twory2=2*ry2;

    tworx2=2*rx2; x=0;

    y=ry; py=tworx2*y;

    px=0;

    ellipseplot(xcenter,ycenter,x,y); p=round(ry2-(rx2*ry)+(0.25*rx2));

    while(px

  • y--;

    py=py-tworx2; p=p+ry2+px-py;

    } ellipseplot(xcenter,ycenter,x,y);

    } p=round(ry2*(x+0.5)*(x+0.5)+rx2*(y-1)*(y-1)-rx2*ry2);

    while(y>0)

    { y--;

    py=py-tworx2; if(p>0)

    p=p+rx2-py;

    else {

    x++; px=px+twory2;

    p=p+rx2-py+px; }

    ellipseplot(xcenter,ycenter,x,y);

    } getch();

    closegraph(); }

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

    { putpixel(xcenter +x,ycenter +y,20);

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

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

  • Output

    Enter the x-coordinate of the centre point : 100

    Enter the y-coordinate of the centre point : 200 Enter the x-coordinate of the radius : 50

    Enter the y-coordinate of the radius : 60

  • Ex.No:2f

    Program

    #include

    #include #include

    #include

    void ellipseplot(int,int,int,int); int xcenter,yc,rx,ry;

    void main() {

    int gd=DETECT,gm;

    long xcenter,ycenter,rx,ry; long rx2,ry2,tworx2,twory2,d1,d2,x,y,dx,dy;

    initgraph(&gd,&gm,"..\\BGI:"); printf("\nEnter the x-coordinate of the centre point :");

    scanf("%ld",&xcenter); printf("\nEnter the y-coordinate of the centre point :");

    scanf("%ld",&ycenter);

    printf("\nEnter the x-coordinate of the radius :"); scanf("%ld",&rx);

    printf("\nEnter the y-coordinate of the radius :"); scanf("%ld",&ry);

    clrscr();

    rx2=rx*rx; ry2=ry*ry;

    twory2=2*ry2; tworx2=2*rx2;

    x=0; y=ry;

    d1=ry2-rx2*ry+(0.25*rx2);

    dx=twory2*x; dy=tworx2*y;

    do {

    ellipseplot(xcenter,ycenter,x,y);

    if(d1

  • d1=d1+dx+ry2;

    } else

    { x=x+1;

    y=y-1; dx=dx+twory2;

    dy=dy-tworx2;

    d1=d1+dx-dy+ry2; }

    } while(dx0)

    { x=x;

    y=y-1;

    dy=dy-tworx2; d2=d2-dy+rx2;

    } else

    {

    x=x+1; y=y-1;

    dy=dy-tworx2; dx=dx+twory2;

    d2=d2+dx-dy+rx2; }

    }

    while(y>0); getch();

    closegraph(); }

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

    { putpixel(xcenter+x,ycenter+y,15);

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

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

  • }

    Output

    Enter the x-coordinate of the centre point : 100

    Enter the y-coordinate of the centre point : 200 Enter the x-coordinate of the radius : 50

    Enter the y-coordinate of the radius : 60

  • Ex. No. 3a :

    Program

    #include #include

    #include

    #include #include

    void main() {

    int gd=DETECT,gm;

    int ch; clrscr();

    while(1) {

    printf("******Line Styles*******\n"); printf("\n1.Solid Line");

    printf("\n2.Dotted Line");

    printf("\n3.Center Line"); printf("\n4.Dashed Line");

    printf("\n5.Userbit Line"); printf("\n6.Exit");

    printf("\n\nEnter your choice:\n");

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

    { case 1:

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

    setlinestyle(0,1,3);

    line(100,30,250,250); getch();

    cleardevice();closegraph(); break;

    case 2:

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

    clrscr(); setlinestyle(1,1,3);

    line(100,30,250,250);

  • getch();

    cleardevice();closegraph(); break;

    case 3: clrscr();

    initgraph(&gd,&gm," "); setlinestyle(2,1,3);

    line(100,30,250,250);

    getch(); cleardevice();closegraph();

    break; case 4:

    clrscr();

    initgraph(&gd,&gm," "); setlinestyle(3,1,3);

    line(100,30,250,250); getch();

    cleardevice(); closegraph();

    break;

    case 5: clrscr();

    initgraph(&gd,&gm," "); setlinestyle(4,1,3);

    line(100,30,250,250);

    getch(); cleardevice();closegraph();

    break; case 6:

    exit(0); }

    }

    }

  • OUTPUT

    ******Line Styles******* 1.Solid Line

    2.Dotted Line

    3.Center Line 4.Dashed Line

    5.Userbit Line 6.Exit

    Enter Ur Choice: 1

    Solid Line

    Entrer ur Choice: 2

    Dotted Line

    ..

  • Enter ur choice : 3

    Enter ur Choicre: 6

  • Ex.No:3b

    Program

    #include #include

    #include #include

    #include void main()

    {

    int gd=DETECT,gm; int ch;

    clrscr(); while(1)

    {

    printf("******Circle Attributes*******\n"); printf("\n1.Empty Fill");

    printf("\n2.Soild Fill"); printf("\n3.Line Fill");

    printf("\n4.Wide dot Fill"); printf("\n5.close dot Fill");

    printf("\n6.User Fill");

    printf("\n7.Exit"); printf("\n\nEnter your choice:\n");

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

    {

    case 1: clrscr();

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

Recommended

View more >