Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
44 views24 pages

Guru Nanak Institute of Management & Technology: Computer Graphics (Software Lab-VII)

Download as docx, pdf, or txt
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 24

Guru Nanak Institute Of Management

& Technology

Practical File
Of
Computer Graphics
(Software Lab-VII)

Submitted To : Submitted By:


Ms Amanpreet kaur Anand Maurya
(A.P., GNIMT) (1716771)
1|Page
SR NO. PARTICULAR PAGE NO.

1. PRINT RESOLUTION IN COMPUTER GRAPHICS. 3

2. DRAW PARALLEL LINES. 4

3. DRAW CONCENTRIC CIRCLESIN COMPUTER GRAPHICS. 5

4. USING DRAWPOLYFUNCTIONIN COMPUTER GRAPHICS. 6

5. DISPLAYING TEXT IN DIFFERENT FONTS IN COMPUTER 7


GRAPHICS.

6. DRAW FACE IN COMPUTER GRAPHICS. 8

7. ANIMATION (BOUNCING BALL) COMPUTER GRAPHICS. 9

8. DDA LINE DRAWING ALGORITHM. 10

9. BRESENHAM’S LINE DRAWING ALGORITHM. 11

10. DRAW DOTTED LINE USING BRESENHAM’S ALGORITHM. 12-13

11. BRESENHAM’S CIRCLE DRAWING ALGORITHM. 14-15

12. MIDPOINT CIRCLE ALGORITHM. 16

13. BOUNDARY FILL ALGORITHM TO FILL DIFFERENT SHAPES. 17-18

14. FLOOD FILL ALGORITHM TO FILL DIFFERENT SHAPES. 19-23

15. SCALING, ROTATION AND TRANSLATION IN COMPUTER 20-24


GRAPHICS.

2|Page
Program 1: Print resolution in computer graphics.
#include<stdio.h>
#include<graphics.h>
int main()
{
    intgdriver=DETECT, gmode;
    initgraph(&gdriver, &gmode, "c:\\turboc\\bgi");
int x=getmaxx();
int y=getmaxy();
printf(“Resolution is : %d x %d”, x,y);
getch();
}

3|Page
Program 2: Draw Parallel Lines.
/*C graphics program to draw a line.*/
#include <graphics.h>
#include <conio.h>
main()
{
intgd = DETECT, gm;
//init graphics
initgraph(&gd, &gm, "C:/TURBOC3/BGI");
line(100,100,200,100); //will draw a horizontal line
line(10,10,200,10); //will draw another horizontal line
getch();
closegraph();
return 0;
}

Output:

4|Page
Program 3: Draw concentric circlesin Computer Graphics.
#include<stdio.h>

#include<graphics.h>

#include<conio.h>

int main(){

   intgd = DETECT,gm;

   int x ,y;

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

x = getmaxx()/2;

   y = getmaxy()/2;

   outtextxy(240, 50, "Concentric Circles");

   setcolor(RED);

   circle(x, y, 30);setcolor(GREEN);

   circle(x, y, 50);setcolor(YELLOW);

   circle(x, y, 70);setcolor(BLUE);

   circle(x, y, 90);getch();closegraph();

   return 0;

Output:

5|Page
Program 4: Using DrawPolyfunctionin Computer Graphics.
#include<graphics.h>

using namespace std;

int main()

{ intgd=DETECT, gm;

int triangle[8]={20,150, 60,70, 110,150, 20,150};

intrect[10]={150,60, 280,60, 280,150, 150,150, 150,60};

int pentagon[12]={340,150, 320,110, 360,70, 400,110, 380,150, 340,150};

int hexagon[14] ={ 360,260, 340,240, 360,220, 400,220, 420,240, 400,260, 360,260};

int octagon[18]={450,150, 430,120, 430,100, 450,70, 500,70, 520,100, 520,120, 500,150, 450,150};

int poly1[10]={50,400, 50,300, 150,400, 250,300, 250,400 };

int poly2[12]={350,430, 350,390, 430,350, 350,310, 300,410, 350,430 };

initgraph(&gd, &gm,NULL);

outtextxy(150,15, "Polygon Drawing in Graphics.h using drawpoly() &fillpoly().");

drawpoly(4,triangle); outtextxy(30,160, "Triangle");

drawpoly(5,rect); outtextxy(190, 160, "Rectangle"); drawpoly(6,pentagon);

outtextxy(330, 160, "Pentagon"); drawpoly(9,octagon); outtextxy(450, 160, "Octagon");

fillpoly(7,hexagon);outtextxy(362, 262, "Hexagon"); drawpoly(5,poly1);

drawpoly(6,poly2); outtextxy(400, 400, "Polygon");

getch();closegraph();

return 0;}

Output:

6|Page
Program 5: Displaying text in different fonts in Computer Graphics.
#include <graphics.h>

int main()

intgd = DETECT, gm;

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

int x = 150;

    int y = 150;

int font = 8;

int direction = 0;

intfont_size = 5;

settextstyle(font, direction, font_size);

outtextxy(x, y, "Geeks For Geeks");

getch();

closegraph();

return 0;

Output:

7|Page
Program 6: Draw Face in Computer Graphics.
#include<graphics.h>

#include<stdio.h>

#include<conio.h>

void main()

{intgd = DETECT,gm;

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

//for head

circle(200,200,30);

//for left eye

circle(190,190,5);

arc(190,190,50,130,10);

//for right eye

circle(210,190,5);

arc(210,190,50,130,10);

//for smiley lips

arc(200,210,180,360,10); line(187,210,193,210); line(207,210,213,210);

//for nose

line(198,195,195,200); line(202,195,205,200);line(195,200,200,205);

line(205,200,200,205);

getch();closegraph();

Output:

8|Page
Program 7: Animation (Bouncing Ball) Computer Graphics.
#include <stdio.h>

#include <conio.h>

#include <graphics.h>

#include <dos.h>

int main() {

intgd = DETECT, gm; int i, x, y, flag=0;

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

x = getmaxx()/2;

y = 30;

while (!kbhit()) {

if(y >= getmaxy()-30 || y <= 30)

flag = !flag; setcolor(RED);

setfillstyle(SOLID_FILL, RED); circle(x, y, 30); floodfill(x, y, RED);

/* delay for 50 milli seconds */

delay(50);

/* clears screen */

cleardevice();

if(flag){

y = y + 5; } else {y = y - 5;

}} getch(); closegraph(); return 0; }

Output:

9|Page
Program 8: DDA Line Drawing Algorithm.
#include <graphics.h>
#include <stdio.h>
#include <math.h>
#include <dos.h>
void main( )
{
    float x,y,x1,y1,x2,y2,dx,dy,step;
    inti,gd=DETECT,gm;
    initgraph(&gd,&gm,"c:\\turboc3\\bgi");
    printf("Enter the value of x1 and y1 : ");
    scanf("%f%f",&x1,&y1);
    printf("Enter the value of x2 and y2: ");
    scanf("%f%f",&x2,&y2);
    dx=abs(x2-x1);
    dy=abs(y2-y1);
    if(dx>=dy)
        step=dx;
    else
        step=dy;
    dx=dx/step;
    dy=dy/step;
    x=x1;
    y=y1;
    i=1;
    while(i<=step)
    {
        putpixel(x,y,5);
        x=x+dx;
        y=y+dy;
        i=i+1;
        delay(100);
    }
closesgraph();
}

Output:

10 | P a g e
Program 9: Bresenham’s Line Drawing Algorithm.
#include<stdio.h>
#include<graphics.h>
 voiddrawline (int x0, int y0, int x1, int y1)
{
    int dx, dy, p, x, y;
 
    dx=x1-x0;
    dy=y1-y0;
    x=x0;
    y=y0;
p=2*dy-dx;
    while(x<x1)
    { if(p>=0)
        {
            putpixel(x,y,7);
            y=y+1;
            p=p+2*dy-2*dx;     }
        else
        { putpixel(x,y,7);
            p=p+2*dy;  }
        x=x+1  } }
int main()
{
    intgdriver=DETECT, gmode, error, x0, y0, x1, y1;
    initgraph(&gdriver, &gmode, "c:\\turboc\\bgi");
    printf("Enter co-ordinates of first point: ");
    scanf("%d%d", &x0, &y0);
    printf("Enter co-ordinates of second point: ");
    scanf("%d%d", &x1, &y1);
    drawline(x0, y0, x1, y1);
    return 0;
}

Output:

11 | P a g e
Program 10: Draw Dotted Line using Bresenham’s Algorithm.
#include<stdio.h>

#include<graphics.h>

#include<math.h>

void main()

intgd=DETECT,gm,x,y,x1,y1,x2,y2,dx,dy,i,e;

floatxinc,yinc;

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

cleardevice();

printf("Enter x1,y1,x2,y2:\n");

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

dx=x2-x1;

dy=y2-y1;

if(x1<x2)

xinc=1;

else

xinc=-1;

if(y1<y2)

yinc=1;

else

yinc=-1;

x=x1; y=y1;

if(dx>=dy)

{e=(2*dy)-dx;

while(x!=x2)

{if(e<0)

e=e+(2*dy); else

{e=e+(2*(dy-dx));

y=y+yinc;

y=y+yinc; }

12 | P a g e
x=x+xinc;

x=x+xinc;

putpixel(x,y,WHITE);

} }

else

{e=(2*dx)-dy;

while(y!=y2)

{ if(e<0)

e=e+(2*dx);

else

{ e=e+(2*(dx-dy));

x=x+xinc;

x=x+xinc; }

y=y+yinc;

y=y+yinc;

putpixel(x,y,WHITE);

}}getch();closegraph();restorecrtmode(); }

Output:

13 | P a g e
Program 11: Bresenham’s Circle Drawing Algorithm.
#include<graphics.h>

#include<conio.h>

#include<stdio.h>

void main()

intgd=DETECT,gm;

intd,r,x,y,xc,yc;

clrscr();

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

printf("Enter Radius\n");

scanf("%d",&r);

printf("Enter Center of circle\n");

scanf("%d",&xc);

scanf("%d",&yc);

d=3-2*r;

x=0;

y=r;

while(x<=y)

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

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

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

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

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

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

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

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

if(d<=0)

d=d+4*x+6;

14 | P a g e
}

else

d=d+4*x-4*y+10;

y=y-1;

x=x+1;

getch();

Output:

15 | P a g e
Program 12: Midpoint Circle Algorithm.
#include<stdio.h>
#include<graphics.h>
voiddrawcircle(int x0, int y0, int radius)
{
    int x = radius;
    int y = 0;
    int err = 0;
    while (x >= y)
    {
    putpixel(x0 + x, y0 + y, 7);putpixel(x0 + y, y0 + x, 7);
    putpixel(x0 - y, y0 + x, 7);    putpixel(x0 - x, y0 + y, 7);
    putpixel(x0 - x, y0 - y, 7);putpixel(x0 - y, y0 - x, 7);
    putpixel(x0 + y, y0 - x, 7);putpixel(x0 + x, y0 - y, 7);
    if (err <= 0)
    {     y += 1;
        err += 2*y + 1;
    }
    if (err > 0)
    { x -= 1;
        err -= 2*x + 1;}    } }
int main()
{   intgdriver=DETECT, gmode, error, x, y, r;
    initgraph(&gdriver, &gmode, "c:\\turboc3\\bgi");
    printf("Enter radius of circle: ");
    scanf("%d", &r);
    printf("Enter co-ordinates of center(x and y): ");
    scanf("%d%d", &x, &y);
    drawcircle(x, y, r);
return 0;
}

Output:

16 | P a g e
Program 13: Boundary Fill Algorithm to fill different Shapes.
#include<stdio.h>

#include<conio.h>

#include<graphics.h>

void BoundaryFill4(int,int,int,int);

void BoundaryFill8(int,int,int,int);

void main()

intgd=DETECT,gm;

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

setbkcolor(15);

setcolor(BLUE);

outtextxy(0,0,"Boundary Filling algorithm using 4-connected and 8-connected");

rectangle(50,50,100,100);

circle(50,50,30);

BoundaryFill8(56,56,5,BLUE);

BoundaryFill4(40,40,14,BLUE);

getch();

}void BoundaryFill4(intx,inty,intfill,int boundary)

{int current;

current=getpixel(x,y);

if((current!=boundary) && (current!=fill))

{putpixel(x,y,fill);

BoundaryFill4(x+1,y,fill,boundary);

BoundaryFill4(x-1,y,fill,boundary);

BoundaryFill4(x,y-1,fill,boundary);

BoundaryFill4(x,y+1,fill,boundary);

} }

void BoundaryFill8(intx,inty,intfill,int boundary)

{ int current;

current=getpixel(x,y);

17 | P a g e
if((current!=boundary) && (current!=fill))

{ putpixel(x,y,fill);

BoundaryFill8(x+1,y,fill,boundary);

BoundaryFill8(x-1,y,fill,boundary);

BoundaryFill8(x,y-1,fill,boundary);

BoundaryFill8(x,y+1,fill,boundary);

BoundaryFill8(x+1,y+1,fill,boundary);

BoundaryFill8(x-1,y+1,fill,boundary);

BoundaryFill8(x-1,y-1,fill,boundary);

BoundaryFill8(x+1,y-1,fill,boundary);

} }

Output:

Program 14: Flood Fill Algorithm to fill different Shapes.


18 | P a g e
#include<stdio.h>

#include<graphics.h>

#include<dos.h>

#include<conio.h>

voidfloodfill(intx,inty,intold,intnewcol)

{ int current;

current=getpixel(x,y);

if(current==old)

{ delay(5);

putpixel(x,y,newcol);

floodfill(x+1,y,old,newcol); floodfill(x-1,y,old,newcol);

floodfill(x,y+1,old,newcol); floodfill(x,y-1,old,newcol);

floodfill(x+1,y+1,old,newcol); floodfill(x-1,y+1,old,newcol);

floodfill(x+1,y-1,old,newcol); floodfill(x-1,y-1,old,newcol); } }

void main()

{ intgd=DETECT,gm; initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");

rectangle(50,50,150,150); floodfill(70,70,0,15); getch();

closegraph();

Output:

Program 15:Scaling, Rotation and Translation in Computer Graphics.

19 | P a g e
#include<stdio.h>

#include<conio.h>

#include<graphics.h>

#include <math.h>

void translate(int,int);

void scale(int,int);

void rotate(float);

void main()

intch;

intgd=DETECT,gm;

inttx,ty,sx,sy;

float theta;

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

setbkcolor(WHITE);

setcolor(6);

outtextxy (100,88,"Object.");

rectangle(100,250,150,200);

printf("---MENU---");

printf("\n 1)Translate\n 2)Scale\n 3)Rotate");

printf("\nEnter your choice: ");

scanf("%d",&ch);

cleardevice();

switch(ch)

case 1:

outtextxy(10,45,"Enter value of tx and ty:");

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

translate(tx,ty);

break;

case 2:

20 | P a g e
outtextxy(10,45,"Enter the value of sx and sy:");

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

scale(sx,sy);

break;

case 3:

outtextxy(10,50,"Enter the angle for rotation: ");

scanf("%f",&theta);

rotate(theta);

break;

default: printf("you have enterd wrong choice");

break;

getch();

closegraph();

void translate(inttx,intty)

setcolor(2);

outtextxy(240,10,"TRANSLATION");

outtextxy(238,20,"------------");

rectangle(100,250,150,200);

rectangle(100+tx,250+ty,150+tx,200+ty);

void scale(intsx,intsy)

setcolor(2);

outtextxy(240,10,"SCALING");

outtextxy(238,20,"--------");

rectangle(100,250,150,200);

rectangle(100*sx,250*sy,150*sx,200*sy);

21 | P a g e
void rotate(float theta)

int x1,x2,x3,x4;

int y1,y2,y3,y4;

int ax1,ax2,ax3,ax4,ay1,ay2,ay3,ay4;

intrefx,refy;

theta=theta*(3.14/180);

setcolor(2);

outtextxy(240,10,"ROTATE");

outtextxy(238,20,"-------");

refx=100;

refy=100;

x1=100;

y1=100;

x2=150;

y2=100;

x3=150;

y3=150;

x4=100;

y4=150;

ax1=refy+(x1-refx)*cos(theta)-(y1-refy)*sin(theta);

ay1=refy+(x1-refx)*sin(theta)+(y1-refy)*cos(theta);

ax2=refy+(x2-refx)*cos(theta)-(y2-refy)*sin(theta);

ay2=refy+(x2-refx)*sin(theta)+(y2-refy)*cos(theta);

ax3=refy+(x3-refx)*cos(theta)-(y3-refy)*sin(theta);

ay3=refy+(x3-refx)*sin(theta)+(y3-refy)*cos(theta);

ax4=refy+(x4-refx)*cos(theta)-(y4-refy)*sin(theta);

ay4=refy+(x4-refx)*sin(theta)+(y4-refy)*cos(theta);

rectangle(100,150,150,100);

line(ax1,ay1,ax2,ay2);

line(ax2,ay2,ax3,ay3);

22 | P a g e
line(ax3,ay3,ax4,ay4);

line(ax4,ay4,ax1,ay1);

Output:

23 | P a g e
24 | P a g e

You might also like