当前位置:免费学习网考试资料计算机类内容页

如何实现C语言画图教程

2020-12-25 16:00:03 计算机类 访问手机版

  C语言有丰富的数据结构和运算符。包含了各种数据结构,如整型、数组类型、指针类型和联合类型等,用来实现各种数据结构的运算。以下是小编为大家搜索整理的C语言如何实现画图教程,希望能给大家带来帮助!更多精彩内容请及时关注我们考试网!

  程序中定义了几个特殊键:

  "V”:画笔提起

  "W”:开始画图

  "R”:开始擦图

  "S”:当前图形存入文件

  "E”:调出已有文件

  "C”:画圆

  程序一运行,屏幕上出现一个黄色的边框来设定画图的区域,区域中间出现提起的画笔符号 ,当按下”W“键时,画笔符号变为 ,此时可移动方向键上、下、左、右、左上、左下、右上、右下来画图;当按下”R“键时,画笔符号变为 ,此时可移动方向键来擦图;在画图过程中,按下“C”键,可画出一个半径为20个象素点的圆;当结束画图时,按下“S”键,将画好的图形存盘;按下“E”键可调出已有的图形进行编辑。

  3.源程序清单

  # include "graphics.h"

  # include "stdio.h"

  # include "fcntl.h"

  # include "stdlib.h"

  main

  void save,load;

  void *wg,*rg,*vg,*fy;

  int driver,mode;

  int c=RED;

  int x=320,y=225;

  int x1,y1,x2,y2;

  int k,k1,k2;

  /* initialize grapher */

  detectgraph&driver,&mode;

  initgraph&driver,&mode,"c: c";

  /* write the pen */

  bar200,10,206,16;

  line203,7,200,10;

  line203,7,206,10;

  line243,7,240,16;

  line243,7,246,16;

  line283,7,280,10;

  line283,7,286,10;

  line283,7,283,16;

  /* save the pen */

  wg=mallocimagesize200,7,206,16;

  rg=mallocimagesize240,7,246,16;

  vg=mallocimagesize280,7,286,16;

  fy=mallocimagesize200,7,206,16;

  getimage200,7,206,16,wg;

  getimage240,7,246,16,rg;

  getimage280,7,286,16,vg;

  cleardevice;

  /* write the box */

  setcolorYELLOW;

  rectangle4,19,637,447;

  x1=x-3;

  y1=y+1;

  x2=x+3;

  y2=y+10;

  getimagex1,y1,x2,y2,fy;

  putimagex1,y1,vg,XOR_PUT;

  /* receive the command */

  for ;;

  while bioskey1==0;

  k=bioskey0;

  putimagex1,y1,fy,AND_PUT;

  if k&0x00ff|0x00==0

  k1=k&0xff?0:k>>8; /* k1 is the specialkey value */

  else

  k2=k&0x00ff; /* k2 is the non-specialkey value */

  if k&0x00ff|0x00==0 /* Special key */

  switchk1

  case 45:

  restorecrtmode;

  exit0;

  case 72:

  if y>20

  y=y-1;

  break;

  case 75:

  if x>5

  x=x-1;

  break;

  case 77:

  if x<636

  x=x+1;

  break;

  case 80:

  if y<446

  y=y+1;

  break;

  case 71:

  if x>5&&y>20

  x=x-1;

  y=y-1;

  break;

  case 79:

  if x>5&&y<446

  x=x-1;

  y=y+1;