




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、第 9 章图形函数 1计算机图形是由像素表示的,一个像素通常被视为图像的最小完整采样。计算机显示的图像一般有两种形式:矢量图和位图。矢量图使用点、直线和曲线来描述图形,它具有颜色、形状、轮廓、大小和屏幕位置等属性,矢量图形最大的优点是无论放大、缩小或旋转等失真;最大的缺点是难以表现色彩层次丰富的逼真图像效果。位图亦称为点阵图,是有无数个色彩点排列组成的图像。扩大位图的效果是增大单个像素,从而使线条和形状显得参差不齐。然而,如果从稍远的位置它,位图图像的颜色和形状又显得是连续的。用 TC 2.0 写程序时,经常碰到这个函数无法(bgi)初始化,怎么办?只需 tc20目录下的*.bgi 文件拷贝到
2、 c 程序目录中即可通过。当然用进行图形化操作,看选用哪中模式了,cga、ega、vga 等,它们都对应某个.bgi 文件,当然如果不知道,将所有的.bgi 文件拷过去即可!9.1点线型函数本节介绍点、线、曲线相关的函数,计算机绘图是绘制点,点可以组本的线型加上颜色又可以各种各样的图像。直线、曲线等,而这些基9.1.1置像素的颜色 getpixel()获取指【函数原型】int far getpixel(int x,int y)【功能讲解】获取指定像素的颜色。【参数说明】x:X 坐标,y:Y 坐标。【程序示例】在屏幕各个像素显示随机颜色。/*函数 getpixel()示例 TurboC 运行*/
3、 #include <graphics.h>#include <stdlib.h> #include <stdio.h> #include <conio.h> #include <dos.h>#define PIXEL_COUNT 1000#define DELAY_TIME100/*in milliseconds*/ int main(void)/*请求自检*/int gdriver = DETECT, gmode, errorcode;int i, x, y, color, maxx, maxy, maxcolor, seed;/
4、*初始化图形系统*/ initgraph(&gdriver, &gmode, "");/*初始化结果*/errorcode = graphresult();if (errorcode != grOk)/*如果不正确*/运行程序,结果如图 9-1 所示。图 9-1 获取指置像素的颜色printf("Graphics error: %sn", grapherrormsg(errorcode); printf("Press any key to halt:");getch();exit(1); /*异常*/maxx = ge
5、tmaxx() + 1; /*获取屏幕的最大 X 坐标*/ maxy = getmaxy() + 1; /*获取屏幕的最大 Y 坐标*/ maxcolor = getmaxcolor() + 1;while (!kbhit()/*初始化随机函数*/ seed = random(32767); srand(seed);for (i=0; i<PIXEL_COUNT; i+)x = random(maxx); y = random(maxy);color = random(maxcolor);putpixel(x, y, color); /*在指定的位置绘制像素*/delay(DELAY_T
6、IME);/*延时*/ srand(seed);for (i=0; i<PIXEL_COUNT; i+)x = random(maxx); y = random(maxy);color = random(maxcolor);if (color = getpixel(x, y)/*获取颜色值并与随机颜色比较*/putpixel(x, y, 0); /*在指定的位置绘制像素*/getch();closegraph(); /*关闭图形系统*/ return 0;【】示例程序运行结果为实际效果图的一个剪切部分。程序开始部分是使用函数 initgraph()初始化图形系统,然后函数 graphre
7、sult(),根据返回值判断是否初始化成功,如果失败则提示出错本程序。初始化成功则继续往下进行。函数 getmaxx()和 getmaxy()分别获取屏幕的最大 X、Y 坐标, getmaxcolor()则是回去能使用的最大颜色值。再往下是个关键的 while 循环,这个循环直到有按键按下才终止,循环内部先是初始化随机函数,第一个 for 循环是 PIXEL_COUNT 次产生随机的 xy 坐标,在该坐标处使用 putpixel()绘制像素。循环结束使用 delay 延时,再次 for 循环,只是这次产生随机像素的时候先调用 getpixel(x,y)获取该坐标像素颜色,然后判断是否该坐标的像
8、素颜色是否跟产生的随机颜色一致,如果一样则使用 putpixel(x,y,0)重新上色。如果用户按下任意键盘按键则形系统。while 循环,关闭图9.1.2置绘制像素 putpixel()在指【函数原型】void putpixel(int x,int y,int pixelcolor)【功能讲解】在指定的位置绘制像素。【参数说明】x:X 坐标,y:Y 坐标,pixelcolor:颜色值。【程序示例】在屏幕各个像素显示随机颜色/*函数 getpixel()示例 TurboC 运行*/ #include <graphics.h>#include <stdlib.h> #in
9、clude <stdio.h> #include <conio.h> #include <dos.h>#define PIXEL_COUNT 1000#define DELAY_TIME100/*in milliseconds*/ int main(void)/*请求自检*/int gdriver = DETECT, gmode, errorcode;int i, x, y, color, maxx, maxy, maxcolor, seed;/*初始化图形系统*/ initgraph(&gdriver, &gmode, "&quo
10、t;);/*初始化结果*/errorcode = graphresult();if (errorcode != grOk)/*如果不正确*/printf("Graphics error: %sn", grapherrormsg(errorcode); printf("Press any key to halt:");getch();exit(1); /*异常*/maxx = getmaxx() + 1; /*获取屏幕的最大 X 坐标*/ maxy = getmaxy() + 1; /*获取屏幕的最大 Y 坐标*/ maxcolor = getmaxcol
11、or() + 1;while (!kbhit()/*初始化随机函数*/运行程序,结果如图 9-2 所示。图 9-2 在指置像素的颜色【】示例程序运行结果为实际效果图的一个剪切部分。程序开始部分是使用函数 initgraph()函数 graphresult(),根据返回值判断是否初始化成功,如果失败则提示出错初始化图形系统,然后本程序。初始化成功则继续往下进行。函数 getmaxx()和 getmaxy()分别获取屏幕的最大 X、Y 坐标, getmaxcolor()则是回去能使用的最大颜色值。再往下是个关键的 while 循环,这个循环直到有按键按下才终止,循环内部先是初始化随机函数,第一个
12、for 循环是 PIXEL_COUNT 次产生随机的 xy 坐标,在该坐标处使用 putpixel()绘制像素。循环结束使用 delay 延时,再次 for 循环,只是这次产生随机像素的时候先调用 getpixel(x,y)获取该坐标像素颜色,然后判断是否该坐标的像素颜色是否跟产生的随机颜色一致,如果一样则使用 putpixel(x,y,0)重新上色。如果用户按下任意键盘按键则形系统。while 循环,关闭图seed = random(32767); srand(seed);for (i=0; i<PIXEL_COUNT; i+)x = random(maxx); y = random(
13、maxy);color = random(maxcolor);putpixel(x, y, color); /*在指定的位置绘制像素*/delay(DELAY_TIME);/*延时*/ srand(seed);for (i=0; i<PIXEL_COUNT; i+)x = random(maxx); y = random(maxy);color = random(maxcolor);if (color = getpixel(x, y)/*获取颜色值并与随机颜色比较*/putpixel(x, y, 0); /*在指定的位置绘制像素*/getch();closegraph(); /*关闭图形
14、系统*/ return 0;9.1.3在指定的两点之间画一直线 line()【函数原型】void far line(int x0,int y0,int x1,int y1)【功能讲解】在指定的两点之间画一直线。【参数说明】x0、y0:为点 0 的 XY 坐标值,x1、y1:为点 1 的 XY 坐标值。【程序示例】在屏幕的最小坐标和最大坐标之间画一直线。运行程序,结果如图 9-3 所示。/*函数 line()示例 TurboC 运行*/ #include <graphics.h>#include <stdlib.h> #include <stdio.h> #i
15、nclude <conio.h> int main(void)/*请求自动检测*/int gdriver = DETECT, gmode, errorcode; int xmax, ymax;/*初始化图形系统*/initgraph(&gdriver, &gmode, "");/*初始化结果*/ errorcode = graphresult();/*如果有错误发生*/if (errorcode != grOk)printf("Graphics error: %sn", grapherrormsg(errorcode);pri
16、ntf("Press any key to halt:"); getch();exit(1);setcolor(getmaxcolor(); xmax = getmaxx(); ymax = getmaxy();/*在最小坐标和最大坐标之间画一直线*/line(0, 0, xmax, ymax);getch(); closegraph(); return 0;图 9-3 两点之间画直线【】示例程序运行结果为实际效果图的一个剪切部分。程序开始部分是使用函数 initgraph()函数 graphresult(),根据返回值判断是否初始化成功,如果失败则提示出错初始化图形系统,
17、然后本程序。初始化成功则继续往下进行。函数 getmaxx()和 getmaxy()分别获取屏幕的最大 X、Y 坐标,getmaxcolor()是获取能使用的最大颜色值,setcolor()以此为参数设置为当前画线颜色,line()在最小坐标和最大坐标直接画一条直线,如运行结果所示。最后是关闭图形系统程序。9.1.4从当前点到有一定相对距离的点画直线 linerel()【函数原型】void far linerel(int dx,int dy)【功能讲解】从当前点到有一定相对距离的点画直线。【参数说明】dx:相对当前点 x 坐标相差的距离值,dy:相对当前点 y 坐标相差的距离值。【程序示例】从
18、当前点到有一定相对距离的点画直线。/*函数 linerel()示例 TurboC 运行*/ #include <graphics.h>#include <stdlib.h> #include <stdio.h> #include <conio.h> int main(void)/*请求自动检测*/int gdriver = DETECT, gmode, errorcode; char msg80;/*初始化图形系统*/initgraph(&gdriver, &gmode, "");/*初始化结果*/ error
19、code = graphresult();/*如果有错误发生*/if (errorcode != grOk)printf("Graphics error: %sn", grapherrormsg(errorcode);printf("Press any key to halt:"); getch();exit(1);/*移动当前点至(20, 30)*/ moveto(20, 30);/*创建并输出信息*/运行程序,结果如图 9-4 所示。图 9-4 从当前点到一相对距离的点画直线【】示例程序运行结果为实际效果图的一个剪切部分。程序开始部分是使用函数 in
20、itgraph()初始化图形系统,然后函数 graphresult(),根据返回值判断是否初始化成功,如果失败则提示出错本程序。初始化成功则继续往下进行,moveto(20, 30)把当前点移动到坐标(20,30)处,sprintf 函数格式化输出信息,调用函数 outtextxy()在坐标位置输出该信息。然后使用 linerel()函数在当前点相对(100,100)的点之间画一直线,同样在当前位置输出信息。有效果图可见当前位置已经变成(120,130)了。最后是关闭图形系统程序。9.1.5设置当前线的宽度和类型 setlinestyle()【函数原型】void far setlinestyl
21、e(int linestype, unsigned upattern,int width)【功能讲解】设置当前线的宽度和类型。【参数说明】linestype:线型,upattern:用户自定义类型时使用,如果使用系统预定义的线型则为0。width:线宽。【程序示例】使用用户定义的线型画线。/*函数 setlinestyle()示例 TurboC 运行*/ #include <graphics.h>#include <stdlib.h>#include <stdio.h> #include <conio.h>sprintf(msg, "
22、(%d, %d)", getx(), gety(); outtextxy(20, 30, msg);/*从当前位置点(CP)到与 CP 有一给定相对距离的点画一直线. */linerel(100, 100);/*创建并输出信息在当前点*/ sprintf(msg, " (%d, %d)", getx(), gety(); outtext(msg);/*clean up*/ getch(); closegraph(); return 0;运行程序,结果如图 9-5 所示。#include <string.h>/*线型名字*/ char *lname =
23、"SOLID_LINE", "DOTTED_LINE", "CENTER_LINE", "DASHED_LINE", "USERBIT_LINE"int main(void)/*请求自动检测*/int gdriver = DETECT, gmode, errorcode; int style, midx, midy, userpat;char stylestr40;initgraph(&gdriver, &gmode, "");/*初始化图形系统*/ erro
24、rcode = graphresult();/*初始化结果*/*如果有错误发生*/if (errorcode != grOk)printf("Graphics error: %sn", grapherrormsg(errorcode);printf("Press any key to halt:"); getch();exit(1);midx = getmaxx() / 2; midy = getmaxy() / 2;/*用户定义线型*/*binary: "0000000000000001" */userpat = 1;for (st
25、yle=SOLID_LINE; style<=USERBIT_LINE; style+)setlinestyle(style, userpat, 1);/*选择线型*/strcpy(stylestr, lnamestyle);/*转换线型为字符*/line(0, 0, midx-10, midy);/*画线*/ rectangle(0, 0, getmaxx(), getmaxy();/*绘制矩形*/ outtextxy(midx, midy, stylestr);/*输出信息*/getch();/*等待按键*/cleardevice();/*clean up*/ closegraph(
26、); return 0;图 9-5 设置当前线的宽度和类型】示例程序运行结果为实际效果图的一个剪切部分。程序开始部分是使用函数 initgraph()【初始化图形系统,然后函数 graphresult(),根据返回值判断是否初始化成功,如果失败则提示出错本程序。初始化成功则继续往下进行,getmaxx()和 getmaxy()函数分别获取屏幕上 xy 坐标的最大值, 除以 2 之后赋值给 midx、midy,因为使用了用户自定义线型所以需要把参数 userpat 指定为 1。for 循环把各线型逐个使用一遍,在循环中首先使用 setlinestyle 函数把当前线型设置为当前循环线型,转换线型
27、字符名并画一个斜线和一个矩形,再在中间输出线型字符信息,等待按任意键循环下个线型。for 循环结束后关闭图形系统程序。9.1.6获取可以传递给函数 setcolor 的最大颜色值 getmaxcolor()【函数原型】int far getmaxcolor(void)【功能讲解】获取可以传递给函数 setcolor 的最大颜色值。【参数说明】无。【程序示例】在屏幕各个像素显示随机颜色。/*函数 getmaxcolor ()示例 TurboC 运行*/ #include <graphics.h>#include <stdlib.h> #include <stdio.
28、h> #include <conio.h> #include <dos.h>#define PIXEL_COUNT 1000#define DELAY_TIME100/*in milliseconds*/ int main(void)/*请求自检*/int gdriver = DETECT, gmode, errorcode;int i, x, y, color, maxx, maxy, maxcolor, seed;/*初始化图形系统*/ initgraph(&gdriver, &gmode, "");/*初始化结果*/err
29、orcode = graphresult();if (errorcode != grOk)/*如果不正确*/运行程序,结果如图 9-6 所示。printf("Graphics error: %sn", grapherrormsg(errorcode); printf("Press any key to halt:");getch();exit(1); /*异常*/maxx = getmaxx() + 1; /*获取屏幕的最大 X 坐标*/ maxy = getmaxy() + 1; /*获取屏幕的最大 Y 坐标*/ maxcolor = getmaxco
30、lor() + 1;while (!kbhit()/*初始化随机函数*/ seed = random(32767); srand(seed);for (i=0; i<PIXEL_COUNT; i+)x = random(maxx); y = random(maxy);color = random(maxcolor);putpixel(x, y, color);/*在指定的位置绘制像素*/delay(DELAY_TIME);/*延时*/ srand(seed);for (i=0; i<PIXEL_COUNT; i+)x = random(maxx); y = random(maxy)
31、;color = random(maxcolor);if (color = getpixel(x, y)/*获取颜色值并与随机颜色比较*/putpixel(x, y, 0);/*在指定的位置绘制像素*/*clean up*/ getch();closegraph(); /*关闭图形系统*/return 0;图 9-6 获取颜色最大值【】示例程序运行结果为实际效果图的一个剪切部分。程序开始部分是使用函数 initgraph()初始化图形系统,然后函数 graphresult(),根据返回值判断是否初始化成功,如果失败则提示出错本程序。初始化成功则继续往下进行。函数 getmaxx()和 getm
32、axy()分别获取屏幕的最大 X、Y 坐标, getmaxcolor()则是回去能使用的最大颜色值。再往下是个关键的 while 循环,这个循环直到有按键按下才终止,循环内部先是初始化随机函数,第一个 for 循环是 PIXEL_COUNT 次产生随机的 xy 坐标,在该坐标处使用 putpixel()绘制像素。循环结束使用 delay 延时,再次 for 循环,只是这次产生随机像素的时候先调用 getpixel(x,y)获取该坐标像素颜色,然后判断是否该坐标的像素颜色是否跟产生的随机颜色一致,如果一样则使用 putpixel(x,y,0)重新上色。如果用户按下任意键盘按键则形系统。while
33、 循环,关闭图9.1.7设置当前画线颜色 setcolor()【函数原型】void far setcolor(int color)【功能讲解】设置当前画线颜色。【参数说明】color:颜色值。【程序示例】设置当前画线颜色并把颜色值显示在屏幕中间。/*函数setcolor()示例 TurboC 运行*/ #include <graphics.h>#include <stdlib.h> #include <string.h> #include <stdio.h> #include <conio.h> int main(void)/*请求系
34、统自检*/int gdriver = DETECT, gmode, errorcode; int color, midx, midy;char colname35;/*初始化图形系统*/ initgraph(&gdriver, &gmode, "");/*初始化结果*/errorcode = graphresult();/*如果有错误*/if (errorcode != grOk)printf("Graphics error: %sn", grapherrormsg(errorcode);printf("Press any ke
35、y to halt:"); getch();/*异常终止*/exit(1);midx = getmaxx() / 2;midy = getmaxy() / 2; setcolor(getmaxcolor();运行程序,结果如图 9-7 所示。图 9-7 设置当前画线颜色【】示例程序运行结果为实际效果图的一个剪切部分。程序开始部分是使用函数 initgraph()初始化图形系统,然后函数 graphresult(),根据返回值判断是否初始化成功,如果失败则提示出错本程序。初始化成功则继续往下进行。函数 getmaxx()和 getmaxy()分别获取屏幕的最大 X、Y 坐标,所以 mi
36、dx、midy 保存的值即是屏幕的中间坐标。getmaxcolor()获取能设置的最大颜色(一般为白色),setcolor 把获取的最大颜色设置为当前画线颜色,函数 settextjustify()设置字置为中间,然后调用getcolor()获取当前颜色保存在变量 color 中,再使用 itoa()转为字符串,使用 outtextxy()把颜色名字输出在屏幕中间。最后是关闭图形系统程序。9.1.8获取当前画线颜色 getcolor()【函数原型】int far getcolor(void)【功能讲解】获取当前画线颜色【参数说明】无【程序示例】把当前画线的颜色显示在屏幕中间。/*函数 getc
37、olor()示例 TurboC 运行*/ #include <graphics.h>#include <stdlib.h> #include <string.h> #include <stdio.h> #include <conio.h> int main(void)/*请求系统自检*/int gdriver = DETECT, gmode, errorcode;settextjustify(CENTER_TEXT, CENTER_TEXT);/*设置中间显示*/ color = getcolor();/*获取现在使用颜色*/itoa
38、(color, colname, 10);/*转换颜色为字符串*/ strcat(colname," is the current drawing color.");outtextxy(midx, midy, colname);/*显示颜色名字*/getch();/*/closegraph(); return 0;运行程序,结果如图 9-8 所示。图 9-8 设置当前画线颜色【】示例程序运行结果为实际效果图的一个剪切部分。程序开始部分是使用函数 initgraph()初始化图形系统,然后函数 graphresult(),根据返回值判断是否初始化成功,如果失败则提示出错本程序
39、。初始化成功则继续往下进行。函数 getmaxx()和 getmaxy()分别获取屏幕的最大 X、Y 坐标,所以 midx、midy 保存的值即是屏幕的中间坐标。getmaxcolor()获取能设置的最大颜色(一般为白色),setcolor 把获取的最大颜色设置为当前画线颜色,函数 settextjustify()设置字置为中间,然后调用getcolor()获取当前颜色保存在变量 color 中,再使用 itoa()转为字符串,使用 outtextxy()把颜色名字输出在屏幕中间。最后是关闭图形系统程序。9.1.9获取当前线型信息 getlinesettings()【函数原型】void far
40、 getlinesettings(struct linesettingstype far* lininfo)int color, midx, midy; char colname35;initgraph(&gdriver, &gmode, "");/*初始化图形系统*/errorcode = graphresult();/*初始化结果*/if (errorcode != grOk)/*如果有错误*/printf("Graphics error: %sn", grapherrormsg(errorcode);printf("Pre
41、ss any key to halt:"); getch();exit(1);/*异常终止*/midx = getmaxx() / 2; midy = getmaxy() / 2; setcolor(getmaxcolor();settextjustify(CENTER_TEXT, CENTER_TEXT);/*设置中间显示*/color = getcolor();/*获取现在使用颜色*/itoa(color, colname, 10);/*转换颜色为字符串*/ strcat(colname," is the current drawing color.");ou
42、ttextxy(midx, midy, colname);/*显示颜色名字*/getch();/*/closegraph(); return 0;【功能讲解】获取当前线型、模式和宽度。【参数说明】lininfo:线型信息结构体指针【程序示例】获取当前线型信息并显示。/*函数 getlinesettings()示例 运行于 TurboC 下*/ #include <graphics.h>#include <stdlib.h> #include <stdio.h> #include <conio.h>/*线型名字*/char *lname = &qu
43、ot;SOLID_LINE","DOTTED_LINE", "CENTER_LINE", "DASHED_LINE", "USERBIT_LINE"int main(void)/*请求系统自检*/int gdriver = DETECT, gmode, errorcode; struct linesettingstype lineinfo;int midx, midy;char lstyle80, lpattern80, lwidth80;initgraph(&gdriver, &gmod
44、e, "");/*初始化图形系统*/ errorcode = graphresult();/*初始化结果*/if (errorcode != grOk)/*如果有错误*/printf("Graphics error: %sn", grapherrormsg(errorcode); printf("Press any key to halt:");getch();exit(1); /*异常*/midx = getmaxx() / 2; midy = getmaxy() / 2;getlinesettings(&lineinfo)
45、;/*获取当前线型信息*/sprintf(lstyle, "%s is the line style.",/*转换线型信息为字符串*/ lnamelineinfo.linestyle);sprintf(lpattern, "0x%X is the user-defined line pattern.", lineinfo.upattern);sprintf(lwidth, "%d is the line thickness.", lineinfo.thickness);/*显示出来*/settextjustify(CENTER_TEX
46、T, CENTER_TEXT); outtextxy(midx, midy, lstyle);outtextxy(midx, midy+2*textheight("W"), lpattern); outtextxy(midx, midy+4*textheight("W"), lwidth); getch();closegraph(); return 0;运行程序,结果如图 9-9 所示。图 9-9 获取当前线型信息】示例程序运行结果为实际效果图的一个剪切部分。程序开始部分是使用函数 initgraph()【函数 graphresult(),根据返回值判断
47、是否初始化成功,如果失败则提示出错初始化图形系统,然后本程序。初始化成功则继续往下进行。函数 getmaxx()和 getmaxy()分别获取屏幕的最大 X、Y 坐标, 所以 midx、midy 保存的值即是屏幕的中间坐标。然后 getlinesettings()获取线型信息保存在结构体 lineinfo中,下面的 sprintf 是转换线型信息为字符串,再使用 settextjustify()设置文字对齐方式,outtextxy()是输出线型信息在指定的坐标处。最后关闭图形显示系统,程序。9.1.10绘制矩形rectangle()【函数原型】void far rectangle(int le
48、ft, int top, int right, int bottom)【功能讲解】绘制矩形。【参数说明】left:矩形左上角顶点 X,top:矩形左上角 Y 坐标。right:矩形右下角 X 坐标,bottom: 矩形右下角 Y 坐标。【程序示例】绘制一个矩形。/*函数 rectangle()示例 运行于 TurboC 下*/ #include <graphics.h>#include <stdlib.h> #include <stdio.h> #include <conio.h>int main(void)int gdriver = DETEC
49、T, gmode, errorcode; int left, top, right, bottom;initgraph(&gdriver, &gmode, "");/*初始化图形显示系统*/errorcode = graphresult();/*初始化结果*/if (errorcode != grOk)/*如果有错误发生*/printf("Graphics error: %sn", grapherrormsg(errorcode); printf("Press any key to halt:");getch();ex
50、it(1);/*异常*/left = getmaxx() / 2 - 50; top = getmaxy() / 2 - 50;运行程序,结果如图 9-10 所示。图 9-10 获取当前线型信息【】程序开始部分是使用函数 initgraph()初始化图形系统,然后函数 graphresult(),根据返回值判断是否初始化成功,如果失败则提示出错本程序。初始化成功则继续往下进行。函数getmaxx()和getmaxy()分别获取屏幕的最大 X、Y 坐标,变量 left、top、right、bottom 通过在屏幕中间位置加减 50 指定一个矩形区域,之后调用函数 rectangle()绘制矩形出
51、来,最后是关闭图形系统程序。9.1.11绘制多边形 drawpoly()【函数原型】void far drawpoly(int numpoints, int far* polypoints);【功能讲解】绘制多边形。【参数说明】numpoints:顶点个数。polypoints:顶点坐标数组。每一个顶点的坐标都定义为 x,y, 并且 x 在前。值得注意的是当画一个封闭的多边形时,numpoints 的值取实际多边形的顶点数加一,并且数组 polypoints 中第一个和最后一个点的坐标相同。【程序示例】绘制一个闭合的多边形。/*函数 drawpoly()示例 运行于 TurboC 下*/ #i
52、nclude <graphics.h>#include <stdlib.h> #include <stdio.h> #include <conio.h> int main(void)int gdriver = DETECT, gmode, errorcode; int maxx, maxy;int poly10;initgraph(&gdriver, &gmode, "");/*初始化图形显示系统*/ errorcode = graphresult();/*初始化结果*/if (errorcode != grO
53、k)/*如果有错误发生*/right = getmaxx() / 2 + 50; bottom = getmaxy() / 2 + 50;rectangle(left,top,right,bottom);/*绘制矩形*/getch(); closegraph(); return 0;运行程序,结果如图 9-11 所示。图 9-11 绘制多边形】程序开始部分是使用函数 initgraph()初始化图形系统,然后函数 graphresult(),根【据返回值判断是否初始化成功,如果失败则提示出错本程序。初始化成功则继续往下进行。函数getmaxx()和 getmaxy()分别获取屏幕的最大 X、Y
54、 坐标,然后根据这两个数据指定四个顶点,由于是绘制闭合的四边形所以顶点个数要比实际多一个。然后使用 drawpoly()绘制多边形,之后关闭图形系统退printf("Graphics error: %sn", grapherrormsg(errorcode); printf("Press any key to halt:");getch();exit(1);/*异常*/maxx = getmaxx(); maxy = getmaxy();poly0 = 20;/*1st vertext*/ poly1 = maxy / 2;poly2 = maxx -
55、20;/*2nd*/ poly3 = 20;poly4 = maxx - 50;/*3rd*/ poly5 = maxy - 20;poly6 = maxx / 2;/*4th*/ poly7 = maxy / 2;/*最后一个坐标点与第一个相同,这样才能闭合*/poly8 = poly0;poly9 = poly1;drawpoly(5, poly);/*绘制多边型*/ getch();closegraph(); return 0;出程序。9.1.12绘制多边形并填充 fillpoly()【函数原型】void far fillpoly(int numpoints, int far* poly
56、points)【功能讲解】绘制并填充一个多边形。【参数说明】numpoints:顶点个数。polypoints:顶点坐标数组。每一个顶点的坐标都定义为 x, y, 并且 x 在前。numpoints 的值取实际多边形的顶点数加一, 并且数组 polypoints 中第一个和最后一个点的坐标相同。【程序示例】获取数字键盘锁定键状态并设置其为开。/*函数 fillwpoly()示例 运行于 TurboC 下*/ #include <graphics.h>#include <stdlib.h> #include <stdio.h> #include <con
57、io.h> int main(void)int gdriver = DETECT, gmode, errorcode; int i,maxx, maxy;int poly10;initgraph(&gdriver, &gmode, "");/*初始化图形显示系统*/ errorcode = graphresult();/*初始化结果*/if (errorcode != grOk)/*如果有错误发生*/printf("Graphics error: %sn", grapherrormsg(errorcode); printf(&quo
58、t;Press any key to halt:");getch();exit(1);/*异常*/maxx = getmaxx(); maxy = getmaxy();poly0 = 20;/*1st vertext*/ poly1 = maxy / 2;poly2 = maxx - 20; /*2nd*/ poly3 = 20;poly4 = maxx - 50; /*3rd*/ poly5 = maxy - 20;/*最后一个坐标点与第一个相同,这样才能闭合*/poly6 = maxx / 2; poly7 = maxy / 2;/*循环填充类型*/for (i=EMPTY_FILL; i<USER_FILL; i+)setfillstyle(i, getmaxcolor();/*设
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
评论
0/150
提交评论