C语言图形显示课件.ppt
图形显示从文本状态转换成图形状态:#include Option Linker Libraries 激活Graphics Libraryinitgraph和closegraph图形显示的坐标与象素xy(0,0)分辨率:640 480使用图形方式时先进行图形系统初始化,检测或设置指定的图形模式图形系统的初始化图形系统初始化函数void initgraph(int*driver,int*mode,char*pathtodriver)initgraph(驱动程序地址,显示模式地址,驱动程序文件的路径);其中;driver显示适配器驱动程序的枚举变量mode图形模式(模式号或模式标识符)pathtodriver适配器驱动程序的寻找路径,“”表示在当前目录下(*.BGI)enum graphics_driverDETECT,CGA,MCGA,EGA,EGA64,VGA,PC3270;DETECT自动检测所用显示适配器的类型,将相应的驱动程序装入,并将其最高显示模式作为当前显示模式#include void main()int gdriver=DETECT,gmode;initgraph(&gdriver,&gmode,”d:borlandcbgi”);closegraph();初始化:关闭图形方式:清屏和恢复显示方式函数void cleardevice(void);void closegraph(void);基本图形函数画点:putpixel()getpixel()坐标移动:moveto()moverel()getx()gety()画线:line()lineto()linerel()画矩形和条形图:rectangle()bar()椭圆、圆、圆弧和扇形:ellipse()circle()arc()pieslice()颜色控制函数设调色板:setpalette()设背景色:setbkcolor()设绘图色:setcolor()关闭图形系统回到文本模式几个常用的函数原型void setcolor(int color);void setbkcolor(int color);void line(int x1,int y1,int x2,int y2);void rectangle(int left,int top,int right,int bottom);void circle(int x,int y,int radius);void putpixel(int x,int y,int color);颜色值颜色名颜色 颜色值颜色名颜色0123456789101112131415BLACKBLUEGREENCYANREDMAGENTABROWNLIGHTGRAYDARKGRAYLIGHTBLUELIGHTGREENLIGHTCYANLIGHTREDLIGHTMAGENTAYELLOWWHITE黑蓝绿青红洋红棕浅灰深灰淡蓝淡绿淡青淡红淡洋红黄白设定线型函数:setlinestyle()填充函数设定填充模式:setfillstyle()常用函数:bar3d()sector()fillellipse()fillpoly()屏幕操作函数屏幕图象存储与显示:getimage()putimage()imagesize()视口函数setviewport()clearviewport()图形方式下的文本输出文本输出:outtext()outtextxy()定义文本字型:settextstyle()#include stdio.h#include graphics.hmain()int i,j,driver=VGA,mode=VGAHI;initgraph(&driver,&mode,);setbkcolor(YELLOW);for(i=50;i=230;i+=20)for(j=50;j=230;j+)putpixel(i,j,1);for(j=50;j=230;j+=20)for(i=50;i=230;i+)putpixel(i,j,1);例1:画图,学用circle画圆形#include graphics.hmain()int driver,mode,i;float j=1,k=1;driver=VGA;mode=VGAHI;initgraph(&driver,&mode,);setbkcolor(YELLOW);for(i=0;i=25;i+)setcolor(8);circle(310,250,k);k=k+j;j=j+0.3;例2:画图,学用line画直线#include graphics.hmain()int driver,mode,i;float x0,y0,y1,x1;float j=12,k;driver=VGA;mode=VGAHI;initgraph(&driver,&mode,);setbkcolor(GREEN);x0=263;y0=263;y1=275;x1=275;for(i=0;i=18;i+)setcolor(5);line(x0,y0,x0,y1);x0=x0-5;y0=y0-5;x1=x1+5;y1=y1+5;j=j+10;x0=263;y1=275;y0=263;for(i=0;i=20;i+)setcolor(5);line(x0,y0,x0,y1);x0=x0+5;y0=y0+5;y1=y1-5;例3:画图,综合例子#define PAI 3.1415926#define B 0.809#include graphics.h#include math.hmain()int i,j,k,x0,y0,x,y,driver,mode;float a;driver=CGA;mode=CGAC0;initgraph(&driver,&mode,);setcolor(3);setbkcolor(GREEN);x0=150;y0=100;circle(x0,y0,10);circle(x0,y0,20);circle(x0,y0,50);for(i=0;i16;i+)a=(2*PAI/16)*i;x=ceil(x0+48*cos(a);y=ceil(y0+48*sin(a)*B);setcolor(2);line(x0,y0,x,y);setcolor(3);circle(x0,y0,60);/*Make 0 time normal size letters*/settextstyle(DEFAULT_FONT,HORIZ_DIR,0);outtextxy(10,170,press a key);getch();setfillstyle(HATCH_FILL,YELLOW);floodfill(202,100,WHITE);getch();for(k=0;k=500;k+)setcolor(3);for(i=0;i=16;i+)a=(2*PAI/16)*i+(2*PAI/180)*k;x=ceil(x0+48*cos(a);y=ceil(y0+48+sin(a)*B);setcolor(2);line(x0,y0,x,y);for(j=1;j=50;j+)a=(2*PAI/16)*i+(2*PAI/180)*k-1;x=ceil(x0+48*cos(a);y=ceil(y0+48*sin(a)*B);line(x0,y0,x,y);restorecrtmode();