C - Program for Graphics Editor
-------------------------------------------------------------------------------------------------------------
Title: assignment
to understand functions available in graphics liabrary such as,
1. Text & graphics mode,initialization of
graphics mode, graphics drivers, switching between text & graphics
mode,error handling.
2. Colour,colour
palette,aspect ratio,text:fonts,alignments,size,orientation &justification.
3. Graphics
primitives: pixel,line,circle,ellipse,polygons,line styles,bar graphs,pie
charts,histogram,filling a polygon, windowing.
4. Writing a
graphics editor.
------------------------------------------------------------------------------------------------------------
#include
<graphics.h>
#include
<stdlib.h>
#include
<stdio.h> //include header
file
#include
<conio.h>
#include<iostream.h>
class
graph_edit //class declraration
{
int x1,y1,x,y;
public:
void drawcircal();
void drawline();
void drawellips();
void draw_poly();
void fill_poly();
void barchar();
void piechart();
void pixel();
void text();
graph_edit();
};
graph_edit::graph_edit() //constructor declaration
{
x1=y1=0;
}
void graph_edit::piechart()
//function for piechart
{
x=getmaxx()/2;
y=getmaxy()/2;
setfillstyle(SOLID_FILL, 5);
pieslice(x,y,0,60,150);
setfillstyle(SOLID_FILL, 10);
pieslice(x,y,61,180,150);
setfillstyle(SOLID_FILL, 12);
pieslice(x,y,181,360,150);
}
void
graph_edit::barchar() //function for barchart
{
x=getmaxx();
y=getmaxy();
setfillstyle(BKSLASH_FILL, getmaxcolor());
line(50,20,50,y-20);
line(20,y-50,x-20,y-50);
bar(100,y-50,x-500,200);
bar(200,y-50,x-400,150);
bar(300,y-50,x-300,250);
bar(400,y-50,x-200,350);
outtextxy(40,22,"Y");
outtextxy(x-30,y-40,"x");
outtextxy(10,y-30,"(0,0)");
}
void
graph_edit::drawcircal() //function for circal
{
int rad;
x=getmaxx()/2;
y=getmaxy()/2;
cout<<"\nEnter the
radius";
cin>>rad;
setcolor(getmaxcolor());
clrscr();
circle(x,y,rad);
}
void
graph_edit::draw_poly() //function for
polygon
{
int poly[15];
x=getmaxx();
y=getmaxy();
poly[0] = 20;
poly[1] = x/2;
poly[2] = x-20;
poly[3] = 20;
poly[4] = x-50;
poly[5] = y-20;
poly[6] = x/2;
poly[7] = y/2;
poly[8] = poly[0];
poly[9] = poly[1];
clrscr();
drawpoly(5, poly);
}
void
graph_edit::text() //function for
textstyle
{
char str[20];
int si;
y=getmaxy()/2;
cout<<"\nEnter the size of
Text:";
cin>>si;
settextstyle(SANS_SERIF_FONT,HORIZ_DIR,
si);
sprintf(str,"Graphics");
clrscr();
outtextxy(0,y,str);
}
void
graph_edit::fill_poly() //function for fillpolygon
{
int poly[15];
x=getmaxx();
y=getmaxy();
poly[0] = 20;
poly[1] = x/2;
poly[2] = x-20;
poly[3] = 20;
poly[4] = x-50;
poly[5] = y-20;
poly[6] = x/2;
poly[7] = y/2;
poly[8] = poly[0];
poly[9] = poly[1];
clrscr();
drawpoly(5, poly);
setfillstyle(SLASH_FILL,12);
fillpoly(5,poly);
getch();
}
void
graph_edit::drawellips() //function for ellips
{
int angle_s,angle_e,x_rad,y_rad;
angle_s=0;
angle_e=360;
x=getmaxx()/2;
y=getmaxy()/2;
cout<<"\n Enter x and y
radius:";
cin>>x_rad>>y_rad;
clrscr();
ellipse(x,y,angle_s,angle_e,x_rad,y_rad);
cout<<x1;
}
void
graph_edit::pixel() //function for draw
pixel
{
int x,y;
cout<<"
Enter X and Y Position";
cin>>x>>y;
putpixel(x,y,10);
}
void
graph_edit::drawline() //function for
draw line
{
int x2,y2;
cout<<"Enter starting point
(x1,y1)";
cin>>x1>>y1;
cout<<"Enter ending point
(x2,y2)";
cin>>x2>>y2;
clrscr();
line(x1,y1,x2,y2);
}
int
main(void) //main function
declaration
{
graph_edit g;
int ch; //initialize graphic mode
int gdriver =
DETECT, gmode, errorcode;
initgraph(&gdriver,
&gmode, "c:\\tc\\bgi");
errorcode = graphresult();
if(errorcode !=
grOk)
{
printf("Graphics error: %s\n",
grapherrormsg(errorcode));
printf("Press any key to
halt:");
getch();
exit(1);
}
do //display menu
{
cout<<"\n\n\t\tMENU";
cout<<"\n\n\t1:Circle\n\t2:Line\n\t3:ellips";
cout<<"\n\t4:polygon\t\n\t5:FillPolygon\n\t6:Text";
cout<<"\n\t7:Pixel\n\t8:Barchart\n\t9:Pie
Chart\n\t10:Exit";
cout<<"\n\nEnter your
choice:";
cin>>ch;
switch(ch)
{
case 1:
g.drawcircal();
getch(); //case for circal drawing
cleardevice();
break;
case 2:
g.drawline();
cleardevice(); //case for line drawing
getch();
break;
case 3:
g.drawellips();
getch(); //case for ellips drawing
cleardevice();
break;
case 4:
g.draw_poly();
getch(); //case for polygon drawing
cleardevice();
break;
case 5:
g.fill_poly();
cleardevice(); //case for polygon filling
getch();
break;
case 6: g.text();
getch(); //case for text style
cleardevice();
break;
case 7:
cleardevice();
g.pixel(); //case for pixel drawing
getch();
break;
case 8: cleardevice();
g.barchar(); //case for barchart
getch();
break;
case 9: cleardevice();
g.piechart(); //case for barchart
getch();
case 10:exit(0);
getch();
}
cleardevice();
}while(ch!=10);
getch();
//closing graph
closegraph();
return 0;
}