0% found this document useful (0 votes)
9 views10 pages

Graphics Programming Examples in C

The document contains multiple C programming examples using the graphics.h library to create various graphical shapes and animations, including keyboard interactions, 2D pictures, triangles, points, cubes, smiley faces, and a pie chart. Each example demonstrates different graphics functions such as drawing lines, circles, and filling shapes with colors. The programs are designed to run in a Turbo C++ environment and utilize basic graphics programming concepts.

Uploaded by

RKING
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)
9 views10 pages

Graphics Programming Examples in C

The document contains multiple C programming examples using the graphics.h library to create various graphical shapes and animations, including keyboard interactions, 2D pictures, triangles, points, cubes, smiley faces, and a pie chart. Each example demonstrates different graphics functions such as drawing lines, circles, and filling shapes with colors. The programs are designed to run in a Turbo C++ environment and utilize basic graphics programming concepts.

Uploaded by

RKING
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

1)Keyboard interaction

#include<graphics.h>

#include<stdio.h>

int main()

int gd=DETECT,gm,x,y,r=40;

char ch;

initgraph(&gd,&gm,"C:/TURBOC 3/BGI");

setbkcolor(3);

x = getmaxx()/2;

y = getmaxy()/2;

setfillstyle(1,RED);

circle(x,y,r);

floodfill(x,y,getmaxcolor());

while((ch=getch())!=13)

switch(ch)

case 75 :if(x>=r+1)

cleardevice();

circle(x-=10,y,r);

floodfill(x,y,getmaxcolor());

break;

case 72:if(y>=r+1)

cleardevice();
circle(x,y=10,r);

floodfill(x,y,getmaxcolor());

break;

case 77:if(x<=(getmaxx()-r-10))

cleardevice();

circle(x+=10,y,r);

floodfill(x,y,getmaxcolor());

break;

case 80:if(y<=(getmaxy()-r-10))

cleardevice();

circle(x,y+=10,r);

floodfill(x,y,getmaxcolor());

getch();

2) 2D picture

#include<graphics.h>

#include<conio.h>

int main()

int gd=DETECT,gm,i,x,y;

initgraph(&gd,&gm,"C://TC//BGI");
x = getmaxx()/3;

y = getmaxy()/3;

setbkcolor(WHITE);

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);

getch();

closegraph();

3)Triangle

#include<graphics.h>

int main()

int gd = DETECT ,gm;

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

line(300,100,200,200);

line(300,100,400,200);

line(200,200,400,200);

getch();

closegraph();

}
4)Points

#include <conio.h>

#include <graphics.h>

#include <stdio.h>

int main()

int gd = DETECT, gm, color;

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

putpixel(85, 35, WHITE);

putpixel(115, 50, WHITE);

putpixel(135, 50, WHITE);

putpixel(145, 60, WHITE);

putpixel(120, 100, WHITE);

putpixel(200, 100, WHITE);

putpixel(150, 100, WHITE);

putpixel(120, 70, WHITE);

getch();

5)Cube

#include <stdio.h>

#include <conio.h>

#include <graphics.h>

#include <dos.h>

int main()

int gdriver = DETECT, gmode;

initgraph(&gdriver, &gmode, (char *)" ");


rectangle(100, 100, 200, 200);

rectangle(150, 150, 250, 250);

line(100, 100, 150, 150);

line(200, 100, 250, 150);

line(100, 200, 150, 250);

line(200, 200, 250, 250);

getch();

closegraph();

return 0;

6)smiley face

#include <conio.h>

#include <dos.h>

#include <graphics.h>

#include <stdio.h>

int main()

int gd = DETECT, gm;

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

setcolor(YELLOW);

circle(300, 100, 40);

setfillstyle(SOLID_FILL, YELLOW);

floodfill(300, 100, YELLOW);

setcolor(BLACK);

setfillstyle(SOLID_FILL, BLACK);

fillellipse(310, 85, 2, 6);

fillellipse(290, 85, 2, 6);


ellipse(300, 100, 205, 335, 20, 9);

ellipse(300, 100, 205, 335, 20, 10);

ellipse(300, 100, 205, 335, 20, 11);

getch();

closegraph();

return 0;

7)Objects with 4 colours

#include <graphics.h>

#include <stdio.h>

int main()

int gd = DETECT, gm, i, maxx, cy;

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

setbkcolor(WHITE);

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(140 + i, cy - 20, 140 + i, cy + 15);

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(132 + 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);

floodfill(51 + i, cy - 15, RED);

setfillstyle(1, LIGHTBLUE);

floodfill(52 + i, cy - 60, RED);

delay(10);

getch();

closegraph();

return 0;

8)Sierpenski gasket

#include <graphics.h>

#include <stdlib.h>

#include <time.h>

#include <conio.h>

void initgraph(int *gd, int *gm)

*gd = DETECT;
initgraph(gd, gm, "");

int main()

int gd, gm;

int x, y;

int vertices[3][2] = { {300, 50}, {50, 450}, {590, 450} };

int i;

initgraph(&gd, &gm);

srand(time(NULL));

x = rand() % getmaxx();

y = rand() % getmaxy();

int iterations = 10000;

for (i = 0; i < iterations; i++)

int randVertex = rand() % 3;

x = (x + vertices[randVertex][0]) / 2;

y = (y + vertices[randVertex][1]) / 2;

putpixel(x, y, WHITE);

getch();

closegraph();

return 0;
}

9)Line Loop

#include <stdio.h>

#include <conio.h>

#include <graphics.h>

int main()

int i, gd = DETECT, gm;

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

setcolor(GREEN);

setbkcolor(RED);

for (i = 1; i <= 100; i = i + 2)

line(30 + i, 50, 30 + i, 100);

line(500, 30 + i, 600, 30 + i);

getch();

return 0;

}
10) PIE CHART

#include<graphics.h>

#include<stdio.h>

int main()

int gd = DETECT,gm,x,y;

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

settextstyle(BOLD_FONT,HORIZ_DIR,2);

outtextxy(220,10,"PIE_CHART");

x=getmaxx()/2;

y=getmaxy()/2;

settextstyle(SANS_SERIF_FONT,HORIZ_DIR,1);

setfillstyle(SOLID_FILL,RED);

pieslice(x,y,0,60,120);

outtextxy(x+140,y-70,"FOOD");

setfillstyle(SOLID_FILL,BLUE);

pieslice(x,y,60,160,120);

outtextxy(x-30,y-170,"RENT");

setfillstyle(SOLID_FILL,GREEN);

pieslice(x,y,160,220,120);

outtextxy(x-250,y,"ELECTRICITY");

setfillstyle(SOLID_FILL,YELLOW);

pieslice(x,y,220,360,120);

outtextxy(x,y+160,"SAVINGS");

getch();

closegraph();

return 0;

You might also like