C语言图形库函数docx.docx
《C语言图形库函数docx.docx》由会员分享,可在线阅读,更多相关《C语言图形库函数docx.docx(13页珍藏版)》请在冰豆网上搜索。
![C语言图形库函数docx.docx](https://file1.bdocx.com/fileroot1/2022-12/31/5956b3b8-d5b3-45c1-a775-c2e65101f5d4/5956b3b8-d5b3-45c1-a775-c2e65101f5d41.gif)
C语言图形库函数docx
1•初始化图形系统
函数名:
initgraph
功能:
初始化图形系统
用法:
voidfarinitgraph(intfar*graphdriver,intfar*grciphmode,charfar*pathtodriver);
程序例:
^include
★include
^include#includointmain(void)
{
/*requestautodetection*/
intgdriver=DETEC1\gmode,errorcode;
/*initializegraphicsmode*/
initgraph(&gdriver,&gmode,"“);
/*readresultofinitialization*/
errorcode二graphresult():
if(errorcode!
=grOk)/*anerroroccurred*/
(
printf(''Graphicserror:
%s\n:
grapherrormsg(errorcode)):
printf("Pressanykeytohalt:
"):
getch();
exit
(1);/*returnwitherrorcode*/
/*drawaline*/
line(0,0,gctmaxx0,gctmaxy());
/*cleanup*/
getch();
closegraph();
return0;
2.
函数名:
drawpoly
功能:
画多边形
用法:
voidfardrawpoly(intnumpoints,intfar*polypoints);
程序例:
#include〈graphics・h>
★include
^include
#includeirHmain(void)
{
/*requestautodetection*/
intgdriver=DETECT,gmode,errorcode;intmaxx,mcixy;
/*ourpolygonarray*/
intpolyL1OJ;
/*initialhegraphicsandlocalvariables*/
initgraph(&gdriver,&gmode,;
/*readresultofinitialization*/errorcode=graphresult();
if(errorcode!
=grOk)
/*anerroroccurred*/
{
printf(^Graphicserror:
%s\n",\grapherrormsg(orrorcode));
prin订("Pressanykeytohalt/);getch();
/♦terminatewithanerrorcode*/exit
(1);
}
meixx=getmeixx();
mcixy=getmcixy0;
poly[0]=20;/*1stvertext*/
poly[1]=maxy/2;
poly[2]=maxx-20;/*2nd*/poly[3]=20;
poly[4]=maxx-50;/*3rd*/poly[5]=maxy-20;
poly[6]=maxx/2;/*4th*/
poly[7]=maxy/2;
/*
drawpolydoesn'tautomaticallyclosethepolygon,sowecloseil・
poly[8]=poly[0]:
poly[9]=poly[l]:
/*drawthepolygon*/drawpoly(5,poly);
/*cleanup*/getchO;elosegraph();
return0;
}
函数名:
ellipse
功能:
画一椭圆
用法:
voidfarellipse(intx,inty,intstangle,intendangle,
int
程序例:
xradius,intyradius);
#include#include#includo^includeintmain(void)
/*requestautodetection*/
intgdriver=DETECT,gmode,errorcode;
intmidx,midy;
inistangle=0,endangle=360;
intxradius=100,yradius=50;
/*initidli/egraphics,localvariables*/inilgraph(&gdrivg&gmode,"“);
/*readresultofinitialization*/errorcode=graphrcsult();
if(errorcode!
=grOk)
/*anerroroccurred*/
(
prin(『("Graphicserror:
%s\r)zz,grapherrormsg(errorcode));
printf("Pressanykeytohalt:
");
getch();
exit
(1);
/*terminatewithanerrorcode*/
}
midx=getmaxx()/2;
midy=getmaxy()/2;
setcolor(getmtixcolor());
/*drawellipse*/
ellipse(midx,midy,stangle,endangle,xradius,yradius);
/*cleanup*/
getch();
closegraph();
return0;
}
函数名:
fillellipse
功能:
画出并填充一椭恻用法:
voidfarfillellipse(intx,inty,intxradius,intyradius);程序例:
#includo〈graphics・h>
#include
intmain(void)
{
intgdriver二DETECT,gmode;
intxcenter,ycenter,i;
initgraph(&gdriver,&gmode,"“);xcenter=getmaxxO/2;
ycenter=getmaxy()/2;
for(i=0;i<13;i++)
{
setfillstyle(i,WHITE);
fillellipse(xcenter,ycenter,100,50);getch();
}
closegraph();
return0;
}
函数名功能用法程序例
getbkcolor
返回当前背景颜色
intfargetbkcolor(void);
#include#include#includo^include^include
intmain(void)
/*requestautodetection*/
intgdriver=DETECT,gmode,errorcode;
intbkcolor,midx,midy;charbknamc[35];
/*initializegraphicsandlocalvariables*/initgraph(&gdrivor,&gmode,"”);
/*roadresultofinitializalion*/
errorcode=graphresult():
/*anerroroccurred*/
if(errorcode!
=grOk)
{
printf(z,Grciphicserror:
%s\n",grapherrormsg(errorcode));printf("Pressanykeytohalt:
z/);getch0;
/*terminatewithanerrorcode*/
exit
(1);
}
midx=getmaxx()/2;
midy二gelmtixy()/2;setcolor(getmaxcolor());
/*forcenteringtextonthedisplay*/settextjustify(CENTER_TEXT,CENTER_TEXT);
/*getthecurrentbackgroundcolor*/bkcolor=getbkcolor0;
/*convertcolorvalueintoastring*/itoa(bkcolor,bkname,10);
strcat(bknamc,
"isthecurrentbackgroundcolor・“);
/*displayamessage*/
outtextxy(midx,midy,bkntune);
/*cleanup*/
getch();
closegraph();
return0;
函数名:
line功能:
在指定两点间画一直线用法:
voidfarline(intxO,intyO,intxl,intyl);
程序例:
#include
^include#includo
^includeintmain(void)
{
/*requestautodetection*/
intgdriver二DETECT,gmode,errorcode;
intxmax,ymax;
/*iniliali/egraphicsand1ocalvariables*/initgraph(&gdriver,&gmode,"”):
/*readresultofinitialization*/
cri^orcodc=graphrcsult();
/*anerroroccurred*/
if(errorcode!
=grOk)
{
prin订("Graphicserror:
%s\n",grapherrormsg(errorcode));
printf("Pressanykeytohalt:
");getch();
exit
(1);
setcolor(getmcixcolor());
xmax=getmeixx();
ymeix二getmtixy();
/*drawadiagonalline*/line(0,0,xmax,ymax);
/*cleanup*/getchO;closegraph();
return0;
}
函数名:
rectangle
功能:
画一个矩形
)\]法:
voidfarrectangle(intleft,inttop,intright,intbottom);
函数名:
sctbkcolor
功能:
用调色板设置当前背景颜色
用法:
voidfarsetbkcolor(intcolor):
程序例:
ttinclude
tfinclude
ttinclude
ttinclude
intmain(void)
{
/*selectadriverandmodethatsupports*/
/*multiplebackgroundcolors・*/
intgdrivcr二EGA,gmodc=EGAHI,errorcode;intbkcol,maxcolor,x,y;
charmsg[80];
/*initializegraphicsandlocalvariables*/initgraph(&gdriver,&gmode,"”);
/*readresultofinitialization*/errorcode=graphresult();
if(errorcode!
=grOk)/*ane门occurred*/
prinlf("Graphicserror:
%s\n/z,grapherrormsg(errorcode));printf("Pressanykeytohalt/);
getch0;
exit
(1);/*terminatewithanerrorcode*/}
/*maximumcolorindexsupported*/maxcolor=getmtixcolor();
/*forcenteringtextmessages*/
seitextjustify(CENTERTEXT,CENTERTEXT);
x二getmaxxO/2;
y=getmaxyO/2;
/*loopthroughtheavailablecolors*/
for(bkcol~0;bkcol<=maxcolor;bkcol++)
{
/*clearthescreen*/
cleardevice();
/*selectanewbackgroundcolor*/sctbkcolor(bkcol);
/*outputamesssage*/
if(bkcol==WHITE)
setcolor(EGA_BLUE):
sprintf(msg,''Backgroundcolor:
bkcol);
outtextxy(x,y,msg);
getch();
}
/*cleanup*/
closcgraphO;
return0;
}
函数名:
setcolor
功能:
设置当前画线颜色
用法:
voidfarsetcolor(intcolor);
程序例:
ttinclude
^include
ftinclude
#includc
intm3in(void)
{
/*selectadriverandmodethatsupports*/
/*multipledrawingcolors・*/
intgdriver=EGA,gmode=EGAI11,errorcode;
intcolor,maxcolor,x,y;
charmsg[80];
/*initializegraphicsandlocalvariables*/initgraph(&gdrivcr,&gmode,;
/*readresultofinitialization*/
errorcode=graphresult();
if(errorcode!
=grOk)/*anerroroccurred*/
{
printf(''Graphicserror:
%s\n",grapherrormsg(errorcode));printf("Pressanykeytohalt:
,z);
gctchO;
exit
(1);/*terminatewithanerrorcode*/
}
/*maximumcolorindexsupported*/mtixcolor二getmtixcolor();
/*forcenteringtextmessages*/
settextjustify(CENTER_TEXT,CENTER_TEXT);
x=getmaxxO/2;
y=getmaxyO/2;
/*loopthroughtheavailablecolors*/
for(color=i;color<-maxcolor;color++)
{
/*clearthescreen*/
cleardevice();
/*selectanewbackgroundcolor*/setcolor(color);
/*outputamesssage*/
sprintf(msg,"Color:
color):
outtextxy(x,y,msg);getch();
}
/*cleanup*/closcgraph();
return0;
}
函数名:
setfillstyle
功能:
设登填充模式和颜色
用法:
voidfarsetfi1lstyle(intpattern,intcolor);程序例:
^include廿include^include〈string.h>^include#inelude
/*thenamesofthefillstylessupported*/char*fname[]={"EMPTY_FILL",
"SOLIDFILL",
"LINEFII丄",
"LTSLASHFILI/;
"SLASII_F1LL","BKSLASII_FILL",
"LTBKSLASH_FILL",
"HATCHFILL",
"XHATCH_FILL",
"INTERLEAVE_FILL",
"WIDEDOTFILL",
"CLOSEDOTFILI/;
"USERFILL"intmain(void)
/*requestautodetection*/
intgdriver=DETECT,gmode,errorcode;
intstyle,midx,midy;
charstylestr[40];
/*initializegraphicsand1ocalvariables*/initgraph(&gdriver,&gmode,");
/*readresultofinitialization*/
errorcodc=graphresult();
if(errorcode!
=grOk)/*anerroroccurred*/
{
printf("Graphicserror:
%s\n〃,grapherrormsg(errorcode));printf("Pressanykeytohalt/):
getchO;
exit
(1);/*terminatewithanerrorcode*/
}
midx=getmtixx()/2;
midy=getmtixy()/2;
for(style=EMPTY_FILL;style{
/*selectthefillstyle*/
setfillstyle(style,getmaxcolor());
/*convertstyleintoastring*/
strepy(stylestr,fname[style]);
/*fillabar*/
bar3d(0,0,midx-10,midy,0,0);
/*outputamessage*/
outtextxy(midx,midy,stylestr);
/*waitforakey*/
getch();
cleardevice();
}
/*cleanup*/
getchO;
closegraph();
return0;
函数名:
setlinestyle
功能:
设置当前画线宽度和类型
用法:
voidfarsetlinestyle(intlinestype,unsigncdupattern);程序例:
#include
#include#include
^includeh>
ttinclude/*thenamesofthelinestylessupported*/charamc[]={
,,SOL1D_L1NE,\
,,DOTTED_LINE,,I
"CENTERLTNE\
"DASHED_LINE",
"USERBnjLINE"
};
intmain(void)
{
/*requestautodetection*/
intgdriver=DETECT,gmode,errorcode;
intstyle,midx,midy,userpat;
charstylestr[40];
/*initializegraphicsandlocalvariables*/
initgraph(&gdrivg&gmodc,"”);
/*readresultofinitialization*/
errorcode二graphrcsult():
if(errorcode!
=grOk)/*anerroroccurred*/
{
printf("Graphicserror:
grapherrormsg(errorcode));
printf(”Pressanykeytohalt:
");
getchO;
exit
(1);/*terminatewithanerrorcode*/
midx=getmaxx()/2;
midy=getmaxy()/2;
/*auser