0% found this document useful (0 votes)
7 views33 pages

Siddhant

The document contains a series of C programs that demonstrate various computer graphics techniques using the graphics.h library. Each program is designed to draw different shapes or animations, such as lines, circles, rectangles, and more complex figures like cars and houses. The output of each program is attributed to Siddhant Gahlot.

Uploaded by

sg9407176
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)
7 views33 pages

Siddhant

The document contains a series of C programs that demonstrate various computer graphics techniques using the graphics.h library. Each program is designed to draw different shapes or animations, such as lines, circles, rectangles, and more complex figures like cars and houses. The output of each program is attributed to Siddhant Gahlot.

Uploaded by

sg9407176
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/ 33

Program: - 01

01: Write a program to draw a simple line using Computer


Graphics.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
int main () {
int gd = DETECT, gm;
initgraph (&gd, &gm, "c:\\turboc3\\bgi");
setcolor (BLUE);
line (0, 20, 600, 20);
printf ("\n\n\n This output is produced by Siddhant Gahlot");
getch ();
closegraph ();
return 0; }

Output:

Program: - 02
[1]
02: Write a program to draw a circle using CG.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
int main () {
int gd = DETECT, gm;
int x = 250, y = 250, radius = 100;
initgraph (&gd, &gm, "c:\\turboc3\\bgi");
setcolor (RED);
circle (x, y, radius);
printf ("\n\n\n This output is produced by Siddhant Gahlot");
getch (); closegraph ();
return 0; }

Output:
Program: - 03

[2]
03: Write a program to draw a rectangle using CG.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
int main () {
int gd = DETECT, gm;
initgraph (&gd, &gm, "c:\\turboc3\\bgi");
setcolor (YELLOW);
rectangle (150, 100, 450, 350);
printf (“\n\n\n This output is produced by Siddhant Gahlot”);
getch (); closegraph ();
return 0; }

Output:

[3]
Program: - 04
04: Write a program to draw a shape of car using CG.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
int main ()
{
int gd = DETECT, gm;
initgraph (&gd, &gm,"c://turboc3//bgi");

cleardevice ();

line (150, 100, 242, 100);


ellipse (242, 105, 0, 90, 10, 5);
line (150, 100, 120, 150);
line (252, 105, 280, 150);
line (100, 150, 320, 150);
line (100, 150, 100, 200);
line (320, 150, 320, 200);
line (100, 200, 110, 200);
line (320, 200, 310, 200);
arc (130, 200, 0, 180, 20);
arc (290, 200, 0, 180, 20);
line (270, 200, 150, 200);
circle (130, 200, 17);
circle (290, 200, 17);

printf (“\n\n\n This output is produced by Siddhant Gahlot”);


getch ();
closegraph ();
return 0;
[4]
}

Output:

[5]
Program: - 05
05: Write a program to draw a flag using CG.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
int main () {
int gd = DETECT, gm;
initgraph (&gd, &gm,"C:\\turboc3\\bgi");

setcolor (5);

line (20, 50, 20, 180);


line (20, 50, 100, 50);
line (20, 70, 100, 70);
line (20, 90, 100, 90);
line (20, 110, 100, 110);
line (100, 50, 100, 110);
circle (60, 80, 10);
line (52, 80, 68, 80);
line (60, 72, 60, 88);

setfillstyle (SOLID_FILL, 22);


floodfill (21, 51, 5);

setfillstyle (SOLID_FILL, 7);


floodfill (21, 75, 5);

setfillstyle (SOLID_FILL, 2);


floodfill (21, 95, 5);

setfillstyle (SOLID_FILL, 7);


[6]
floodfill (81, 75, 5);

setfillstyle (SOLID_FILL, 7);


floodfill (61, 81, 5);

printf (“\n This output is produced by Siddhant Gahlot”);


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

Output:

Program: - 06
06: Write a program to draw a bar graph using CG.

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

[7]
int main () {
int gd = DETECT, gm;
initgraph (&gd, &gm, "c:\\turboc 3\\bgi");

outtextxy (150, 10, "This is program to show bar graph");

bar (100, 100, 150, 250);


bar (160, 150, 210, 250);
bar (220, 180, 270, 250);
bar (280, 200, 330, 250);
bar (100, 250, 330, 250);

outtextxy (100, 260, "2007, 2008, 2009, 2010");


outtextxy (200, 280, "Year");
outtextxy (20, 180, "Rate of");
outtextxy (20, 190, "Illiteracy");

outtextxy (100, 300, "This output is produced by Siddhant Gahlot");

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

Output:

[8]
Program: - 07
07: Write a program to draw an ellipse using CG.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
int main () {
int gd = DETECT, gm;
initgraph (&gd, &gm, "c:\\turboc3\\bgi");
[9]
setcolor (YELLOW);
ellipse (320, 200, 0, 360, 50, 20);
printf (“\n\n\n This output is produced by Siddhant Gahlot”);
getch ();
closegraph ();
return 0; }

Output:

Program: - 08
08: Write a program to draw an arc using CG.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>

int main ()
{
int gd = DETECT, gm;
[10]
initgraph (&gd, &gm, "c:\\turboc3\\bgi");
setcolor (YELLOW);
arc (200, 200, 180, 360, 40);
printf (“\n\n\n This output is produced by Siddhant Gahlot”);
getch ();
closegraph ();
}

Output:

Program: - 09
[11]
09: Write a program to draw a 2-D bar graph using CG.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
int main () {
int gd = DETECT, gm;
int left, top, right, bottom;
int i, bar_width = 40, gap = 20;
int values [] = {100, 200, 150, 180};
int n = sizeof (values) / sizeof (values [0]);

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

line (50, 50, 50, 400);


line (50, 400, 400, 400);

for (i = 0; i <= 5; i++) {


char label [10];
sprint (label, "%d", i * 50);
outtextxy (20, 400 – (i * 50), label); }

left = 60;

for (i = 0; i < n; i++) {


top = 400 - values [i];
right = left + bar_width;
bottom = 400;
setfillstyle (SOLID_FILL, i + 1);
bar (left, top, right, bottom);
char year [10];
sprint (year, "%d", 2000 + i);
left = right + gap; }
[12]
printf ("\n\t This output is produced by Siddhant Gahlot");
getch ();
closegraph ();
return 0; }

Output:

Program: - 10

[13]
10: Write a program to draw an ellipse with different colors
using CG.

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
int main () {

int gd = DETECT, gm;

int i, x_center = 300, y_center = 200;

int start_angle = 0, end_angle = 360;

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

outtextxy (200, 20, "Ellipses with Different Colors")

for (i = 1; i <= 7, i++) {


setcolor (i);
ellipse (x_center, y_center, start_angle, end_angle, 50 + (i * 10), 30 +
(i * 5)); }

outtextxy (150, 400, "This output is produced by Siddhant Gahlot.")

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

Output:

[14]
Program: - 11
11: Write a program to draw a house using CG.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
int main () {
[15]
int gd = DETECT, g;
initgraph (&gd, &gm, "c:\\turboc3\\bgi");

line (100, 100, 150, 50);


line (150, 50, 200, 100);
line (150, 50, 350, 50);
line (350, 50, 400, 100);

rectangle (100, 100, 200, 200);


rectangle (200, 100, 400, 200);
rectangle (130, 130, 170, 200);
rectangle (250, 120, 350, 180);

setfillstyle (2, 3);


floodfill (131, 131, WHITE);
floodfill (201, 101, WHITE);

setfillstyle (11, 7);


floodfill (101, 101, WHITE);
floodfill (150, 52, WHITE);
floodfill (163, 55, WHITE);
floodfill (251, 121, WHITE);

printf (“\n\n\n This output is produced by Siddhant Gahlot”);


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

Output:

[16]
Program: - 12
12: Write a program to draw a kite using CG.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
int main () {

[17]
int gd = DETECT, gm;
initgraph (&gd, &gm, "c:\\turboc3\\bgi");

line (100, 100, 50, 180);


line (100, 100, 150, 180);
line (50, 180, 100, 250);
line (150, 180, 100, 250);

line (100, 100, 100, 250);


line (50, 180, 150, 180);
line (100, 250, 70, 300);
line (100, 250, 130, 300);

line (70, 300, 130, 300);


line (100, 300, 120, 320);
line (120, 320, 80, 340);
line (80, 340, 120, 360);
line (120, 360, 80, 380);

setcolor (4);

printf (“\n\t This output is produced by Siddhant Gahlot”);


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

Output:

[18]
Program: - 13
13: Write a program to draw a fish using CG.
#include<stdio.h>
#include<conio.h>

[19]
#include<graphics.h>
int main () {
int gd = DETECT, gm;
initgraph (&gd, &gm, "c:\\turboc3\\bgi");
ellipse (200, 200, 0, 360, 50, 30);
line (250, 200, 280, 170);
line (280, 170, 280, 230);
line (280, 230, 250, 200);
circle (170, 190, 3);
printf (“\n\n\n This output is produced by Siddhant Gahlot”);
getch ();
closegraph ();
return 0; }

Output:

[20]
Program: - 14
14: Write a program to draw a line using DDA algorithm.
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#include <math.h>

// Define round manually if not supported


int round (float num) {
return (int) (num + 0.5); }

int main () {
int gd = DETECT, gm, i;
float x, y, dx, dy, steps;
int x0, x1, y0, y1;

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

setbkcolor (GREEN);

x0 = 100; y0 = 200;
x1 = 500; y1 = 300;
dx = (float) (x1 - x0);
dy = (float) (y1 - y0);

steps = (fabs (dx) >= fabs (dy))? fabs (dx): fabs (dy);

dx = dx / steps;
dy = dy / steps;
x = x0;
y = y0;
[21]
for (i = 0; i <= steps; i++) {
putpixel (round(x), round(y), RED);
x += dx;
y += dy; }

printf ("\n\n This output is produced by Siddhant Gahlot");


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

Output:

[22]
Program: - 15
15: Write a program to draw a line using Bresenham’s
algorithm.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>

int 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; }

else {
putpixel (x, y, 7);
p = p+2*dy; }
x = x+1; }}

int main () {
int gd = DETECT, gm, error, x0, y0, x1, y1;
initgraph (&gd, &gm, "c:\\turboc3\\bgi");
printf (“\t This output is produced by Siddhant Gahlot”);
[23]
printf ("\n Enter co-ordinate of first point:");
scanf ("%d %d", &x0, &y0);

printf ("Enter co-ordinate of second point:");


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

drawline (x0, x1, y0, y1);

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

Output:

[24]
Program: - 16
16: Write a program to draw animation using increasing
circles filled with different colors and patterns.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
int main () {
int gd = DETECT, gm, i, x, y;
initgraph (&gd, &gm, "c:\\turboc3\\bgi");
x = getmaxx ()/3; y = getmaxx ()/3;
setcolor (BLUE);
for (i = 1; i <= 8; i++) {
setfillstyle (i, i); delay (20);
circle (x, y, i*20);
floodfill (x-2+i*20, y, BLUE); }
printf (“\n\n\n This output is produced by Siddhant Gahlot”);
getch (); closegraph ();
return 0; }

Output:

[25]
Program: - 17
17: Write a Program to make a moving-colored car using
inbuilt functions.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
int main () {
int gd = DETECT, gm, i, maxx, cy;
initgraph (&gd, &gm, "c:\\turboc3\\bgi");
setcolor (RED);

maxx = getmaxx ();


cy = getmaxy ()/2;

for (i=0; i<maxx-140; i++) {


cleardevice ();
line (0+i, cy-20, 0+i, cy+15);
line (0+i, cy-20, 25+i, cy-20);
line (25+i, cy-20, 40+i, cy-70);
line (40+i, cy-70, 100+i, cy-70);
line (100+i, cy-70, 115+i, cy-20);
line (115+i, cy-20, 140+i, cy-20);
line (0+i, cy+15, 18+i, cy+15);
circle (28+i, cy+15, 10);
line (38+i, cy+15, 102+i, cy+15);
circle (112+i, cy+15, 10);
line (122+i, cy+15 ,140+i, cy+15);
line (140+i, cy+15, 140+i, cy-20);
rectangle (50+i, cy-62, 90+i, cy-30);
setfillstyle (1, BLUE);
[26]
floodfill (5+i, cy-15, RED);
setfillstyle (1, LIGHTBLUE);
floodfill (52+i, cy-60, RED);
delay (10); }

printf (“\n\n\n This output is produced by Siddhant Gahlot”);


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

Output:

[27]
Program: - 18
18: Write a Program to implement Digital Clock.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>

int main () {
int gd = DETECT, gm, hr, min, sec;
struct time t;
char str [10];
clrscr ();

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


printf (“This output is produced by Siddhant Gahlot”);

setcolor (GREEN);

while (! kbhit ()) {


gettime (&t);
hr = t.ti_hour;
min = t.ti_min;
sec = t.ti_sec;
printf (str, "%2d: %2d: %2d", hr, min, sec);
rectangle (150, 190, 460, 310);
rectangle (160, 200, 450, 300);
settextstyle (4, 0, 5);
outtextxy (180, 220, str);
sound (600);
settextstyle (4, 0, 8);
outtextxy (100, 320, "Digital Clock");
[28]
sound (400); delay (30);
nosound (); delay (930);
clearviewport (); }

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

Output:

[29]
Program: - 19
19: Write a program to draw basic shapes.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>

int main () {
int gd = DETECT, gm;
int poly [12] = {350, 450, 350, 410, 430, 400, 350, 350, 400, 310, 350,
450};
initgraph (&gd, &gm, "c:\\turboc3\\bgi");
setbkcolor (BLUE);
setcolor (YELLOW);

circle (100, 100, 50);


outtextxy (75, 170, "Circle");

rectangle (200, 50, 350, 150);


outtextxy (240, 170, "Rectangle");

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


outtextxy (480, 170, "Ellipse");

line (100, 250, 540, 250);

sector (150, 400, 30, 200, 90, 50);


outtextxy (120, 460, "Sector");

drawpoly (6, polu);


[30]
fillpoly (6, poly);
outtextxy (340, 460, "Polygon");
arc (520, 410, 0, 150, 60);
outtextxy (500, 420, "Arc");

printf (“This output is produced by Siddhant Gahlot”);


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

Output:

[31]
Program: - 20
20: Write a program to draw a pie chart using CG.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>

int main () {
int gd = DETECT, gm, midx, midy;
initgraph (&gd, &gm,"c:\\turboc3\\bgi");

settextstyle (SANS_SERIF_FONT, HORIZ_DIR, 2);

setcolor (WHITE);
outtextxy (275, 80, "PIE Chart");

midx = getmaxx ()/2;


midy = getmaxy ()/2;

setfillstyle (LINE_FILL, BLUE);


pieslice (midx, midy, 0, 75, 100);

setfillstyle (XHATCH_FILL, RED);


pieslice (midx, midy, 75, 227, 100);

setfillstyle (WIDE_DOT_FILL, GREEN);


pieslice (midx, midy, 225, 360, 100);

[32]
printf (“This output is produced by Siddhant Gahlot”);
getch ();
closegraph ();
return 0; }

Output:

[33]

You might also like