0% found this document useful (0 votes)
21 views28 pages

Mographic Notes

graphic notes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views28 pages

Mographic Notes

graphic notes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 28

~1~

SESSION 2022-2023

PRACTICAL FILE

COMPUTER GRAPHICS & MULTIMEDIA APPLICATION


SUB CODE – 401
ROLL NO-R200995106015

SUBMITTED BY:
SUBMITTED TO:

MOHIT CHAUHAN MRS. PRIYANKA DHAKA

BCA 2RD YEAR


~2~

INDEX
S.NO PROGRAMS REMARKS

1 Write A C Graphics Program of Line.


2 Write a C Graphics Program of rectangle.
3 Write a C Graphics Program of square.
4 Write a C Graphics Program of triangle using floodfill.
5 Write a C Graphics Program of pentagon.
6 Write a C Graphics Program of hut with men.
7 Write a C Graphics Program of pixel.
8 Write a C Graphics Program of moving car.
9 Write a C Graphics Program of circle.
10 Write a C Graphics Program of circle using mid-point.
11 Write a C Graphics Program of triangle.
12 Write a C Graphics Program of setlinstyle.
13 Write a c graphics program of ellipse.
14 Write a c graphics program to draw a kite by using any line
function.
15 Write a c graphics program to draw arc.
16 Write a c graphics program to draw a fish.
17 Write a c graphics to draw a straight line in the given
position.
18 Write a program to draw a line using DDA’s line drawing
algorithm.
19 Write a program to draw a line using Bresenham’s line
drawing algorithm.
20 Write a program to draw a circle using Bresenham’scircle
drawing algorithm.
21 Write a Program to implement Digital Clock.
22 Write a Program to implement traffic signal.
23 Write a program to draw a stick man.

BCA 2RD YEAR


~3~

1.Write A C Graphics Program of Line.


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");
printf(" ------line------- ");
line(100,100,500,100);
getch();
closegraph();
}
output

2.Write a C Graphics Program of rectangle.


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");
printf(" ------rectangle------- ");
setfillstyle(SOLID_FILL,RED);
line(50,100,600,100);
line(50,100,50,300);
line(600,100,600,300);
line(50,300,600,300);
floodfill(51,110,WHITE);
getch();
closegraph();

BCA 2RD YEAR


~4~

}
Output

3.Write a C Graphics Program of square.


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");
printf(" ------square------- ");
line(100,100,400,100);
line(100,100,100,400);
line(100,400,400,400);
line(400,100,400,400);
getch();
closegraph();
}
Output:

BCA 2RD YEAR


~5~

4.Write a C Graphics Program of triangle using floodfill.


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");
printf(" ------triangle------- ");
setfillstyle(SOLID_FILL,BLUE);
line(100,400,500,400);
line(100,400,300,100);
line(300,100,500,400);
floodfill(301,110,WHITE);
getch();
closegraph();
}
Output:

5.Write a C Graphics Program of pentagon.


BCA 2RD YEAR
~6~

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");
printf(" ------pentagon------- ");
line(200,400,500,400);
line(200,400,150,250);
line(150,250,350,100);
line(350,100,550,250);
line(550,250,500,400);
getch();
closegraph();
}
Output:

6.Write a C Graphics Program of hut with men.


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");
printf(" ------hut------- ");
line(100,200,200,150);
line(200,150,300,200);

BCA 2RD YEAR


~7~

circle(200,200,20);
line(100,200,100,400);
line(300,200,300,400);
line(100,250,300,250);
line(100,400,300,400);
line(200,150,500,150);
line(500,150,600,200);
line(300,200,600,200);
line(600,200,600,400);
line(300,400,600,400);
line(150,400,150,300);
circle(200,320,15);
line(200,335,200,370);
line(200,345,175,360);
line(200,345,225,360);
line(200,370,175,400);
line(200,370,225,400);

line(150,300,250,300);
line(250,300,250,400);
line(400,250,500,250);
line(400,250,400,330);
line(400,330,500,330);
line(500,250,500,330);
line(450,250,450,330);
line(400,290,500,290);

circle(560,70,50);
getch();
closegraph();
}
Output:

BCA 2RD YEAR


~8~

7.Write a C Graphics Program of pixel.


#include <graphics.h>
#include <stdio.h>
#include<conio.h>
void main()
{
int gd = DETECT, gm, color;
initgraph(&gd, &gm,"c:\\tc\\bgi");
printf(" colorfull doted pixel ");
putpixel(85, 35, GREEN);
putpixel(30, 40, RED);
putpixel(115, 50, YELLOW);
putpixel(135, 50, CYAN);
putpixel(45, 60, BLUE);
putpixel(20, 100, WHITE);
putpixel(200, 100, LIGHTBLUE);
putpixel(150, 100, LIGHTGREEN);
putpixel(200, 50, YELLOW);
putpixel(120, 70, RED);
getch();
closegraph();
getch();
}
Output :

BCA 2RD YEAR


~9~

8.Write a C Graphics Program of moving car.


#include<conio.h>
#include<dos.h>
#include<graphics.h>
void main()
{
int d=0,m,x;
clrscr();
initgraph(&d,&m,"c:\\tc\\bgi");
for(x=1;x<200;x++)
{
//road
line(0,400,639,400);
//car body
line(50+x,370,90+x,370);
line(130+x,370,220+x,370);
line(260+x,370,300+x,370);
line(300+x,370,300+x,350);
line(300+x,350,240+x,330);
line(240+x,330,200+x,300);
line(200+x,300,110+x,300);
line(110+x,300,80+x,330);
line(80+x,330,50+x,340);

line(50+x,340,50+x,370);
//car window
line(165+x,305,165+x,330);
line(165+x,330,230+x,330);
line(230+x,330,195+x,305);
line(195+x,305,165+x,305);

BCA 2RD YEAR


~ 10 ~

line(160+x,305,160+x,330);
line(160+x,330,95+x,330);
line(95+x,330,120+x,305);
line(120+x,305,160+x,305);

//wheel
circle(110+x,370,17);
circle(240+x,370,17);

setfillstyle(SOLID_FILL,BLUE);
floodfill(201+x,310,WHITE);

setfillstyle(SOLID_FILL,BLUE);
floodfill(121+x,315,WHITE);
delay(10);
cleardevice();
}
getch();
}
Output:

9.Write a C Graphics Program of circle.


// C Implementation for drawing circle
#include <graphics.h>

//driver code
int main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TC\\bgi");
// circle function

BCA 2RD YEAR


~ 11 ~

printf("----------------draw circle------------");
circle(150, 100, 50);
getch();
closegraph();
}
Output:

10.Write a C Graphics Program of circle using mid-point.


#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>

int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int midx, midy;
int radius = 100;

/* initialize graphics and local variables */


initgraph(&gdriver, &gmode, "C:\\TC\\bgi");

/* read result of initialization */


errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}

BCA 2RD YEAR


~ 12 ~

midx = getmaxx() / 2;
midy = getmaxy() / 2;
setcolor(getmaxcolor());

/* draw the circle */


circle(midx, midy, radius);

/* clean up */
getch();
closegraph();
return 0;
}
Output:

11.Write a C Graphics Program of triangle.


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");
printf(" ------triangle------- ");
line(100,400,500,400);
line(100,400,300,100);
line(300,100,500,400);
getch();
closegraph();
}

BCA 2RD YEAR


~ 13 ~

Output:

12.Write a C Graphics Program of setlinstyle.

// driver code
int main()
{
int gd = DETECT, gm;

// variable to change the


// line styles
int c;

// initial coordinate to
// draw line
int x = 200, y = 100;

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


printf(" ------------draw setlinstyle---------");
// To keep track of lines
for ( c = 0 ; c < 5 ; c++ )
{
// setlinestyle function
setlinestyle(c, 0, 1);

// Drawing line
line(x, y, x+200, y);
y = y + 25;
BCA 2RD YEAR
~ 14 ~

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

13.Write a c graphics program of ellipse.


#include<stdio.h>
#include<conio.h>
#include <graphics.h>

int main()
{

int gd = DETECT, gm;

int x = 250, y = 200;

int start_angle = 0;
int end_angle = 360;

int x_rad = 100;


int y_rad = 50;

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

ellipse(x, y, start_angle,

BCA 2RD YEAR


~ 15 ~

end_angle, x_rad, y_rad);

getch();

return 0;
}
Output:

14.Write a c graphics program to draw a kite by using any line


function.

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd,gm;
detectgraph(&gd,&gm);

initgraph(&gd,&gm,"c:\\tc\\bgi");
line(200,200,300,100);
line(200,200,300,300);
line(300,100,400,200);
line(400,200,300,300);
line(300,100,300,300);
arc(300,300,45,135,140);

setfillstyle(SOLID_FILL,BLUE);

BCA 2RD YEAR


~ 16 ~

floodfill(301,105,WHITE);
setfillstyle(SOLID_FILL,LIGHTRED);

floodfill(299,105,WHITE);
setfillstyle(SOLID_FILL,BLUE);

floodfill(299,275,WHITE);
setfillstyle(SOLID_FILL,LIGHTRED);
floodfill(301,275,WHITE);
line(300,300,250,350);
line(250,350,350,350);
line(300,300,350,350);
setfillstyle(SOLID_FILL,YELLOW);
floodfill(300,310,WHITE);
getch();
closegraph();
}
Output:

15.Write a c graphics program to draw arc.


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");

BCA 2RD YEAR


~ 17 ~

printf(" -----------draw arc--------");


arc(200,150,0,150,60);
getch();
closegraph();
}
Output:

16.Write a c graphics program to draw a fish .

#include<stdio.h>
#include<conio.h>
#include<graphics.h>

void main()
{
int gd,gm;
detectgraph(&gd,&gm);

initgraph(&gd,&gm,"C:\\TC\\BGI");
printf(" --------------draw fish---------------");
ellipse(200,200,0,360,130,50);
setfillstyle(LINE_FILL,CYAN);
floodfill(201,201,15);

line(325,185,390,155);
line(390,155,360,200);
line(395,235,325,215);
line(395,235,360,200);
setfillstyle(LINE_FILL,LIGHTRED);
floodfill(330,190,15);

BCA 2RD YEAR


~ 18 ~

ellipse(100,200,315,45,50,60);
circle(120,180,3);
setfillstyle(SOLID_FILL,YELLOW);
floodfill(120,180,3);

line(170,150,260,90);
line(260,90,220,150);
setfillstyle(LINE_FILL,LIGHTRED);
floodfill(200,140,15);

line(170,250,260,290);
line(260,290,230,250);
setfillstyle(LINE_FILL,LIGHTRED);
floodfill(200,255,15);

arc(220,185,270,90,6);
arc(200,185,270,90,6);
arc(180,185,270,90,6);
arc(200,215,270,90,6);
arc(220,215,270,90,6);
arc(180,215,270,90,6);
arc(240,200,270,90,6);

getch();
closegraph();
}
Output:

17.Write a c graphics to draw a straight line in the given position.


BCA 2RD YEAR
~ 19 ~

#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#include <stdlib.h>
int main() /* Main function begins */
{
/* Declare the graphic variables */
int gdriver, gmode, userpattern;
int x, y, x_max, y_max;
/* Initialize the graphics driver to autodetect the graphics drivers */
gdriver = DETECT;
userpattern = 1;
/* Initialization of graph and path set to graphics interface */
initgraph(&gdriver, &gmode, "c:\\tc\\bgi");
/* Set the line style */
setlinestyle(DOTTED_LINE,userpattern, 3);
/* Draw the line */
x = 100;
y = 70;
x_max = 350;
y_max = 70;
line(x, y, x_max, y_max);
/* close the graph */
getch();
closegraph();
return 0;
}

Output:

BCA 2RD YEAR


~ 20 ~

18.Write a program to draw a line using DDA’s 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;
int i,gd=DETECT,gm;

initgraph(&gd,&gm,"c:\\tc\\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;

BCA 2RD YEAR


~ 21 ~

while(i<=step)
{
putpixel(x,y,5);
x=x+dx;
y=y+dy;
i=i+1;
delay(100);
}

closegraph();
}
Output:

19.Write a program to draw a line using Bresenham’s line


drawing algorithm.
#include<stdio.h>
#include<graphics.h>
void drawline(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;

BCA 2RD YEAR


~ 22 ~

}
else
{
putpixel(x,y,7);
p=p+2*dy;}
x=x+1;
}
}
int main()
{
int gdriver=DETECT, gmode, error, x0, y0, x1, y1;
initgraph(&gdriver, &gmode, "c:\\tc\\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:

20.Write a program to draw a circle using Bresenham’s circle.


drawing algorithm.
#include <stdio.h>
#include<conio.h>
#include <dos.h>
#include <graphics.h>
// Function to put pixels at subsequence points
void drawCircle(int xc, int yc, int x, int y)
{
putpixel(xc+x, yc+y, RED);
putpixel(xc-x, yc+y, RED);

BCA 2RD YEAR


~ 23 ~

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


putpixel(xc-x, yc-y, RED);
putpixel(xc+y, yc+x, RED);
putpixel(xc-y, yc+x, RED);
putpixel(xc+y, yc-x, RED);
putpixel(xc-y, yc-x, RED);
}
// Function for circle-generation using Bresenham's algorithm
void circleBres(int xc, int yc, int r)
{
int x = 0, y = r;
int d = 3 - 2 * r;
drawCircle(xc, yc, x, y);
while (y >= x)
{
// for each pixel we will draw all eight pixels
x++;
// check for decision parameter and correspondingly update d, x, y
if (d > 0)
{
y--;
d = d + 4 * (x - y) + 10;
}
else
d = d + 4 * x + 6;
drawCircle(xc, yc, x, y);
delay(100); }}
void main()
{
int xc, yc , r2;
int gd = DETECT, gm;
initgraph(&gd, &gm, "c:\\tc\\bgi"); // initialize graph
printf("Enter the values of xc and yc :");
scanf("%d%d",&xc,&yc);
printf("Enter the value of radius :");
scanf("%d",&r2);
circleBres(xc, yc, r2); // function call
}
Output:

BCA 2RD YEAR


~ 24 ~

21.Write a Program to implement Digital Clock.


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>

struct time t;
void display(int,int,int);
void main()
{
int i=0,gd=DETECT,gm,hr,min,sec;
clrscr();
initgraph(&gd,&gm,"c:\\tc\\bgi");
setcolor(GREEN);
settextstyle(4,0,7);

while(!kbhit())
{
gettime(&t);
hr=t.ti_hour;
min=t.ti_min;
sec=t.ti_sec;
i++;

display(100,100,hr);
display(200,100,min);
display(300,100,sec);
sound(400);
delay(30);
nosound();
delay(930);

BCA 2RD YEAR


~ 25 ~

cleardevice();
}
getch();
}
void display(int x,int y,int num)
{

char str[3];
itoa(num,str,10);

settextstyle(4,0,7);

outtextxy(180,100,":");
outtextxy(280,100,":");
outtextxy(x,y,str);

rectangle(90,90,380,200);
rectangle(70,70,400,220);

outtextxy(90,250,"Digital Clock");
}
Output:

22.Write a Program to implement traffic signal.


#include <graphics.h>
#include <conio.h>

int main()
{

BCA 2RD YEAR


~ 26 ~

//initilizing graphic driver and


//graphic mode variable
int graphicdriver=DETECT,graphicmode;

//calling initgraph with parameters


initgraph(&graphicdriver,&graphicmode,"c:\\tc\\bgi");

//Printing message for user


outtextxy(50, 50 + 50, "Program to create traffic signal in C graphics");

//initilizing variables
int middlex, middley;

//getting middle x and y


middlex = getmaxx()/2;
middley = getmaxy()/2;

//setting color as white for the outline


setcolor(WHITE);
settextstyle(DEFAULT_FONT, HORIZ_DIR, 1);
rectangle(middlex-30,middley-80,middlex+30,middley+80);
circle(middlex, middley-50, 22);

//filling red color to signify stop sign


setfillstyle(SOLID_FILL,RED);
floodfill(middlex, middley-50,WHITE);
setcolor(BLUE);
outtextxy(middlex-15,middley-50,"STOP");

//setting color as white for the outline


setcolor(WHITE);
rectangle(middlex-30,middley-80,middlex+30,middley+80);
circle(middlex, middley, 20);

//filling yellow color to signify ready sign


setfillstyle(SOLID_FILL,YELLOW);
floodfill(middlex, middley,WHITE);
setcolor(BLUE);
outtextxy(middlex-18,middley-3,"READY");

//setting white color for outline

BCA 2RD YEAR


~ 27 ~

setcolor(WHITE);
rectangle(middlex-30,middley-80,middlex+30,middley+80);
circle(middlex, middley+50, 22);

//filling green color to signify go sign


setfillstyle(SOLID_FILL,GREEN);
floodfill(middlex, middley+50,WHITE);
setcolor(BLUE);
outtextxy(middlex-7,middley+48,"GO");
setcolor(RED);
settextstyle(SCRIPT_FONT, HORIZ_DIR, 4);

getch();
return 0;
}
Output:

23.Write a program to draw a stick man.


#include<math.h>
#include<conio.h>
#include<graphics.h>
void main()
{

int gd=DETECT,gm;
int x,y,r,c1;
initgraph(&gd,&gm,"c:\\tc\\bgi");
circle(150,70,70);
circle(120,50,10);

BCA 2RD YEAR


~ 28 ~

circle(190,50,10);
line(155,60,155,80);
arc(155,100,180,360,20);
line(130,140,130,170);
line(170,140,170,170);
rectangle(80,170,230,260);
line(110,260,110,360);
line(205,260,205,360);
line(80,190,55,240);
line(230,190,255,240);
getch();
closegraph();
}
Output:

BCA 2RD YEAR

You might also like