Computer Graphics
Computer Graphics
#include <graphics.h>
#include <conio.h>
main()
getch();
cleardevice();
outtextxy(100,100,"Press any key to exit...");
getch();
closegraph();
return 0;
Drawpoly
#include <graphics.h>
#include <conio.h>
main()
int gd=DETECT,gm,points[]={320,150,420,300,250,300,320,150};
drawpoly(4, points);
getch();
closegraph();
return 0;
}
Fillelipse
#include <graphics.h>
#include <conio.h>
int main()
getch();
closegraph();
return 0;
fillpoly
#include <graphics.h>
#include <conio.h>
main()
int gd=DETECT,gm,points[]={320,150,420,300,250,300,320,150};
fillpoly(4, points);
getch();
closegraph();
return 0;
fillArc
Fills a wedge-shaped area of an elliptical arc. The parameters are interpreted in the same way as those
for drawArc.
Usage:
fillRect
Usage:
fillRect(bounds);
fillRect(x, y, width, height);
setFont:
Sets a new font. The font parameter is a string in the form family-style-size. In this string, family is the name
of the font family; style is either missing (indicating a plain font) or one of the strings Bold, Italic,
or BoldItalic; and size is an integer indicating the point size. If any of these components is specified as an
asterisk, the existing value is retained. The font parameter can also be a sequence of such specifications
separated by semicolons, in which the first available font on the system is used.
Usage: setFont(font);
getFont:
#include<graphics.h>
#include<conio.h>
#include<stdio.h>
main()
struct arccoordstype a;
char arr[100];
initgraph(&gd, &gm,"C:\\TC\\BGI");
arc(250,200,0,90,100);
getarccoords(&a);
sprintf(arr,"(%d, %d)",a.xstart,a.ystart);
outtextxy(360,195,arr);
sprintf(arr,"(%d, %d)",a.xend,a.yend);
outtextxy(245,85,arr);
getch();
closegraph();
return 0;
getbkcolor
#include <graphics.h>
#include <conio.h>
#include<stdio.h>
int main()
char a[100];
initgraph(&gd,&gm,"C:\\TC\\BGI");
bkcolor = getbkcolor();
getch();
closegraph();
return 0;
getColor:
#include<graphics.h>
#include<conio.h>
#include<stdio.h>
main()
char a[100];
initgraph(&gd,&gm,"C:\\TC\\BGI");
drawing_color = getcolor();
getch();
closegraph();
return 0;
setColor:
#include<graphics.h>
#include<conio.h>
main()
initgraph(&gd,&gm,"C:\\TC\\BGI");
setcolor(RED);
getch();
closegraph();
return 0;
}
Pause
Pauses for the indicated number of milliseconds. This function is useful for animation where the motion would
otherwise be too fast.
Usage:
pause(milliseconds);
waitForClick
Usage:
waitForClick();
settextstyle
#include <graphics.h>
#include <conio.h>
main()
initgraph(&gd,&gm,"C:\\TC\\BGI");
y = y + 25;
}
getch();
closegraph();
return 0;
Setlinestyle
#include <graphics.h>
int main()
setlinestyle(c, 0, 2);
y = y + 25;
getch();
closegraph();
return 0;
Setfillstyle
#include<graphics.h>
#include<conio.h>
main()
setfillstyle(XHATCH_FILL, RED);
getch();
closegraph();
return 0;
}
Pieslice
#include <graphics.h>
#include <conio.h>
main()
getch();
closegraph();
return 0;
#include <graphics.h>
#include <stdio.h>
// C program to draw a moving car. This
#include <graphics.h>
#include <stdio.h>
void draw_moving_car(void) {
setcolor(RED);
// body of car
delay(100);
setcolor(BLACK);
getch();
closegraph();
// Driver code
int main()
draw_moving_car();
return 0;
}
4. Write a program to design a smiley face using graphics.h functions.
#include <conio.h>
#include <dos.h>
#include <graphics.h>
#include <stdio.h>
// Driver Code
int main()
setcolor(YELLOW);
setfillstyle(SOLID_FILL, YELLOW);
setfillstyle(SOLID_FILL, BLACK);
getch();
// graphics system
closegraph();
return 0;
}
5. Write a program to create circles inside various circles using graphics.h functions.
#include <graphics.h>
#include <conio.h>
int main()
//variable of graphics
int graphicdriver=DETECT,graphicmode;
initgraph(&graphicdriver,&graphicmode,"c:\\turboc3\\bgi");
setcolor(RED);
circle(200,200,100);
setcolor(BLUE);
circle(200,200,80);
setcolor(YELLOW);
circle(200,200,60);
setcolor(BROWN);
circle(200,200,40);
getch();
return 0;
#include <graphics.h>
#include <conio.h>
int main()
initgraph(&graphicdriver,&graphicmode,"c:\\turboc3\\bgi");
//initilizing variables
middlex = getmaxx()/2;
middley = getmaxy()/2;
setcolor(WHITE);
rectangle(middlex-30,middley-80,middlex+30,middley+80);
setfillstyle(SOLID_FILL,RED);
floodfill(middlex, middley-50,WHITE);
setcolor(WHITE);
outtextxy(middlex-15,middley-50,"STOP");
rectangle(middlex-30,middley-80,middlex+30,middley+80);
setfillstyle(SOLID_FILL,YELLOW);
floodfill(middlex, middley,WHITE);
setcolor(WHITE);
outtextxy(middlex-18,middley-3,"READY");
setcolor(WHITE);
rectangle(middlex-30,middley-80,middlex+30,middley+80);
setfillstyle(SOLID_FILL,GREEN);
floodfill(middlex, middley+50,WHITE);
setcolor(WHITE);
outtextxy(middlex-7,middley+48,"GO");
setcolor(RED);
getch();
return 0;