画图软件C程序代码.docx
《画图软件C程序代码.docx》由会员分享,可在线阅读,更多相关《画图软件C程序代码.docx(45页珍藏版)》请在冰豆网上搜索。
画图软件C程序代码
#include
#include
#include
#include
#include
#include
#include
#include
/*定义常量*/
/*向上翻页移键*/
#definePAGEUP0x4900
/*向下翻页移键*/
#definePAGEDOWN0x5100
/*Escape键*/
#defineESC0x011b
/*左移键*/
#defineLEFT0x4b00
/*右移键*/
#defineRIGHT0x4d00
/*下移键*/
#defineDOWN0x5000
/*上移键*/
#defineUP0x4800
/*空格键*/
#defineSPACE0x3920
#defineNO_PRESSED0
#defineLEFT_PRESSED1
#defineRIGHT_PRESSED2
#definepi3.1415926
/*定义全局变量*/
intRx,Ry,R;
intTOPx,TOPy,BOTTOMx,BOTTOMy;
intCentx,Centy;
intlineStartx,lineStarty,lineEndx,lineEndy;
intlinePoint_x[20],linePoint_y[20];
/*这里的字模数组均由“点阵字模工具”生成,你可以用你自己需要的点阵信息来
替换示例中的字模信息,注意字模大小要一致,否则显示会出问题。
*/
charzhi16K[]={
/*以下是'直'的16点阵楷体字模,32byte*/
0x01,0x00,0x01,0x00,0x01,0xF0,0x1E,0x00,
0x02,0x00,0x07,0xC0,0x08,0x40,0x0F,0x40,
0x08,0x40,0x0F,0x40,0x08,0x40,0x0F,0x40,
0x08,0x40,0x0F,0xFC,0x70,0x00,0x00,0x00,
};
charxian16K[]={
/*以下是'线'的16点阵楷体字模,32byte*/
0x00,0x80,0x00,0x90,0x08,0x88,0x10,0x80,
0x24,0xF0,0x45,0x80,0x78,0xB0,0x11,0xC0,
0x2C,0x88,0x70,0x50,0x04,0x60,0x18,0xA4,
0x63,0x14,0x00,0x0C,0x00,0x04,0x00,0x00,
};
charju16K[]={
/*以下是'矩'的16点阵楷体字模,32byte*/
0x00,0x00,0x08,0x00,0x08,0x78,0x10,0x80,
0x1E,0x80,0x28,0xF8,0x48,0x88,0x0E,0x88,
0xF8,0xF0,0x08,0x80,0x14,0x80,0x12,0x9E,
0x20,0xE0,0x40,0x00,0x00,0x00,0x00,0x00,
};
charxing16K[]={
/*以下是'形'的16点阵楷体字模,32byte*/
0x00,0x00,0x07,0x88,0x3A,0x08,0x12,0x10,
0x12,0x20,0x17,0x48,0xFA,0x10,0x12,0x20,
0x12,0xC8,0x12,0x08,0x22,0x10,0x42,0x20,
0x00,0x40,0x00,0x80,0x03,0x00,0x00,0x00,
};
charyuan16K[]={
/*以下是'圆'的16点阵楷体字模,32byte*/
0x00,0xF8,0x3F,0x08,0x23,0x88,0x24,0x88,
0x27,0x08,0x21,0xC8,0x2E,0x48,0x29,0x48,
0x29,0x48,0x22,0x88,0x24,0x48,0x28,0x08,
0x3F,0xE8,0x00,0x10,0x00,0x00,0x00,0x00,
};
charqing16K[]={
/*以下是'清'的16点阵楷体字模,32byte*/
0x00,0x80,0x00,0xE0,0x33,0x80,0x10,0xE0,
0x03,0x80,0x40,0xFC,0x2F,0x00,0x01,0xE0,
0x12,0x20,0x13,0xA0,0x22,0x20,0x63,0xA0,
0x42,0x20,0x02,0x60,0x00,0x20,0x00,0x00,
};
charping16K[]={
/*以下是'屏'的16点阵楷体字模,32byte*/
0x00,0xF0,0x0F,0x30,0x08,0x60,0x0F,0x80,
0x0A,0x20,0x09,0x40,0x08,0xF8,0x17,0x20,
0x11,0x3E,0x2F,0xE0,0x21,0x20,0x42,0x20,
0x82,0x20,0x04,0x20,0x08,0x20,0x00,0x00,
};
charbao16K[]={
/*以下是'保'的16点阵楷体字模,32byte*/
0x00,0x00,0x09,0xF0,0x0A,0x10,0x12,0x10,
0x13,0xE0,0x30,0x80,0x50,0xFC,0x9F,0x80,
0x11,0xC0,0x12,0xA0,0x14,0x98,0x18,0x8E,
0x10,0x80,0x10,0x80,0x00,0x00,0x00,0x00,
};
charcun16K[]={
/*以下是'存'的16点阵楷体字模,32byte*/
0x01,0x00,0x01,0x00,0x01,0xF0,0x1E,0x00,
0x02,0x70,0x05,0x90,0x08,0x20,0x08,0x40,
0x18,0x7E,0x2B,0xA0,0xC8,0x20,0x08,0x20,
0x08,0x20,0x08,0xA0,0x00,0x40,0x00,0x00,
};
charjia16K[]={
/*以下是'加'的16点阵楷体字模,32byte*/
0x00,0x00,0x08,0x00,0x08,0x00,0x08,0x00,
0x0F,0x00,0x79,0x3C,0x09,0x44,0x11,0x44,
0x11,0x44,0x22,0x44,0x22,0x78,0x4A,0x00,
0x84,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
};
charzai16K[]={
/*以下是'载'的16点阵楷体字模,32byte*/
0x00,0x80,0x08,0xA0,0x08,0x90,0x0E,0x80,
0x38,0xF0,0x0F,0x80,0x78,0x50,0x0E,0x50,
0x34,0x20,0x1E,0x20,0x34,0x50,0x0E,0x92,
0x75,0x0A,0x04,0x06,0x04,0x02,0x00,0x00,
};
charbang16K[]={
/*以下是'帮'的16点阵楷体字模,32byte*/
0x04,0x00,0x07,0x38,0x1C,0x48,0x06,0x50,
0x1C,0x50,0x07,0x48,0x78,0x58,0x11,0x40,
0x21,0xF0,0x4F,0x10,0x09,0x10,0x09,0x50,
0x09,0x20,0x01,0x00,0x01,0x00,0x00,0x00,
};
charzhu16K[]={
/*以下是'助'的16点阵楷体字模,32byte*/
0x00,0x00,0x00,0x20,0x0C,0x20,0x34,0x20,
0x24,0x20,0x34,0x38,0x25,0xC8,0x34,0x48,
0x24,0x48,0x26,0x88,0x38,0x88,0xE1,0x28,
0x02,0x10,0x04,0x00,0x00,0x00,0x00,0x00,
};
/*自定义函数*/
voidoutChinese(char*mat,intmatsize,intx,inty,intcolor);
voidfill(intstartx,intstarty,intendx,intendy,intcolor);
voidshowHelp();
voidsave();
voidload();
intmouseStatus(int*x,int*y);
intsetMousePos(intx,inty);
voidDrawMouse(floatx,floaty);
voidDrawLine();
voidDrawRectangle();
voidLineToCircle(intx0,inty0,intr);
voidDrawCircle();
longfactorial(intn);
floatberFunction(inti,intn,doublet);
voidDrawBezier();
/*根据点阵信息显示中文函数*/
voidoutChinese(char*mat,intmatsize,intx,inty,intcolor)
/*依次:
字模指针、点阵大小、起始坐标(x,y)、颜色*/
{
inti,j,k,n;
n=(matsize-1)/8+1;
for(j=0;jfor(i=0;ifor(k=0;k<8;k++)
if(mat[j*n+i]&(0x80>>k))
/*测试为1的位则显示*/
putpixel(x+i*8+k,y+j,color);
}
/*填充函数*/
voidfill(intstartx,intstarty,intendx,intendy,intcolor)
{
inti,j;
for(i=startx;i<=endx;i++)
for(j=starty;j<=endy;j++)
/*在指定位置以指定颜色画一像素*/
putpixel(i,j,color);
}
/*显示用户帮助函数*/
voidshowHelp()
{
setcolor(14);
outtextxy(45,50,"Line:
");
setcolor(WHITE);
outtextxy(45,50,"1Pressleftbuttontostartuntiltolineend.");
outtextxy(45,65,"2UseUP,DOWN,LEFT,RIGHTkeystomoveit.");
outtextxy(45,80,"3UsePAGEUPkeytoenlargeit,andPAGEDOWNkeytoshrinkit.");
outtextxy(45,95,"4UseSPACEkeytorotateit.");
setcolor(14);
outtextxy(45,120,"Rectangle:
");
setcolor(WHITE);
outtextxy(45,120,"1Pressleftbuttontostartuntiltorightcorner.");
outtextxy(45,135,"2UseUP,DOWN,LEFT,RIGHTkeystomoveit.");
outtextxy(45,150,"3UsePAGEUPkeytoenlargeit,andPAGEDOWNkeytoshrinkit.");
setcolor(14);
outtextxy(45,170,"Circle:
");
setcolor(WHITE);
outtextxy(45,170,"1Pressleftbuttontostartuntiltoend.");
outtextxy(45,185,"2UsePAGEUPkeytoenlargeit,andPAGEDOWNkeytoshrinkit.");
setcolor(14);
outtextxy(45,205,"Bezier:
");
setcolor(WHITE);
outtextxy(45,205,"Pressleftbuttontostart,andrightbuttontoend.");
outtextxy(45,230,"PressESCkeytostoptheoperationfunction.");
outtextxy(45,245,"Pressrightbuttontoendthedrawingworks.");
outtextxy(45,260,"Pressanykeytocontinue......");
getch();
fill(40,40,625,270,0);
}
/*保存函数*/
voidsave()
{
inti,j;
FILE*fp;
charfileName[20];
fill(0,447,630,477,2);
gotoxy(1,25);
printf("\n\n\n\nInputthefilename[.dat]:
");
scanf("%s",fileName);
fill(0,447,630,477,2);
/*以读写的方式打开文件*/
if((fp=fopen(fileName,"w+"))==NULL)
{
outtextxy(260,455,"Failedtoopenfile!
");
exit(0);
}
outtextxy(280,455,"saving...");
/*保存像素到文件*/
for(i=5;i<630;i++)
for(j=30;j<=445;j++)
fputc(getpixel(i,j),fp);
fclose(fp);
fill(0,447,630,477,2);
outtextxy(260,455,"saveover!
");
}
/*打开函数*/
voidload()
{
inti,j;
charfileName[20];
FILE*fp;
fill(0,447,630,477,2);
gotoxy(1,25);
printf("\n\n\n\nInputthefilename[.dat]:
");
scanf("%s",fileName);
/*打开指定的文件*/
if((fp=fopen(fileName,"r+"))!
=NULL)
{
fill(0,447,630,477,2);
outtextxy(280,455,"loading...");
/*从文件中读出像素*/
for(i=5;i<630;i++)
for(j=30;j<=445;j++)
putpixel(i,j,fgetc(fp));
fill(0,447,630,477,2);
outtextxy(280,455,"loadingover!
");
}
/*打开失败*/
else
{
fill(0,447,630,477,2);
outtextxy(260,455,"Failedtoopenfile!
");
}
fclose(fp);
}
/*获取鼠标状态函数*/
intmouseStatus(int*x,int*y)
{
/*定义两个寄存器变量,分别存储入口参数和出口参数*/
unionREGSinregs,outregs;
intstatus;
status=NO_PRESSED;
/*入口参数AH=3,读取鼠标位置及其按钮状态*/
inregs.x.ax=3;
int86(0x33,&inregs,&outregs);
/*CX表示水平位置,DX表示垂直位置*/
*x=outregs.x.cx;
*y=outregs.x.dx;
/*BX表示按键状态*/
if(outregs.x.bx&1)
status=LEFT_PRESSED;
elseif(outregs.x.bx&2)
status=RIGHT_PRESSED;
return(status);
}
/*设置鼠标指针位置函数*/
intsetMousePos(intx,inty)
{
unionREGSinregs,outregs;
/*入口参数AH=4,设置鼠标指针位置*/
inregs.x.ax=4;
inregs.x.cx=x;
inregs.x.dx=y;
int86(0x33,&inregs,&outregs);
}
/*绘制鼠标函数*/
voidDrawMouse(floatx,floaty)
{
line(x,y,x+5,y+15);
line(x,y,x+15,y+5);
line(x+5,y+15,x+15,y+5);
line(x+11,y+9,x+21,y+19);
line(x+9,y+11,x+19,y+21);
line(x+22,y+19,x+20,y+21);
}
/*绘制直线函数*/
voidDrawLine()
{
intx0,y0,x1,y1;
intlast_x=0,last_y=0;
intendFlag=0;
intkey;
inttemStartx,temStarty,temEndx,temEndy;
intincrement_x,increment_y,angle;
DrawMouse(last_x,last_y);
while
(1)
{
/*右键结束画直线*/
while((mouseStatus(&x1,&y1)==RIGHT_PRESSED))
endFlag=1;
if(endFlag==1)
break;
/*鼠标移动,没有单击,仅仅画移动的鼠标*/
while(mouseStatus(&x1,&y1)==NO_PRESSED)
{
if(last_x!
=x1||last_y!
=y1)
{
DrawMouse(last_x,last_y);
DrawMouse(x1,y1);
last_x=x1;
last_y=y1;
}
}
/*单击左键后,开始画直线*/
if(mouseStatus(&x0,&y0)==LEFT_PRESSED)
{
DrawMouse(last_x,last_y);
line(x0,y0,x1,y1);
last_x=x1;
last_y=y1;
/*拉动过程中,画直线和鼠标*/
while(mouseStatus(&x1,&y1)==LEFT_PRESSED)
{
if(last_x!
=x1||last_y!
=y1)
{
line(x0,y0,last_x,last_y);
line(x0,y0,x1,y1);
last_x=x1;
last_y=y1;
}
}
/*松开左键后,画直线完成,记录直线的起始位置*/
lineStartx=x0;
lineStarty=y0;
lineEndx=x1;
lineEndy=y1;
while
(1)
{
/*从键盘获取键值,开始操作(移动、放大、缩小、旋转)直线*/
key=bioskey(0);
/*ESC键,退出操作*/
if(key==ESC)
break;
/*旋转*/
if(key==SPACE)
{
/*计算旋转中心*/
/*如果直线示倾斜的*/
if((lineStarty!
=lineEndy)&&(lineStartx!
=lineEndx))
{
Centx=(lineEndx-lineStartx)/2+lineStartx;
Centy=(lineEndy-lineStarty)/2+lineStarty;
}
/*如果直线是竖直的*/
if(lineStarty==lineEndy)
{
Centx=(lineEndx-lineStartx)/2+lineStartx;
Centy=lineStarty;
}
/*如果直线是水平的*/
if(lineStartx==lineEndx)
{
Centx=lineStartx;
Centy=(lineEndy-lineStarty)/2+lineStarty;
}
temStartx=lineStartx;
temStarty=lineStarty;
temEndx=lineEndx;
temEndy=lineEndy;
/*旋转不能超过边界*/
if(lineStartx>=10&&lineStarty>=40&&lineEndx<=620&&lineEndy<=445)
{
/*清除原有的直线*/
setwritemode(XOR_PUT);
line(lineStartx,lineStarty,lineEndx,lineEndy);
/*计算旋转30度后的起点坐标*/
lineStartx=(temStartx-Centx)*cos(pi/6)-(temStarty-Centy)*sin(pi/6)+Centx;
lineEndx=(temEndx-Centx)*cos(pi/6)-(temEndy-Centy)*sin(pi/6)+Centx;
/*计算旋转30度后的终点坐标*/
lineStarty=(temStartx-Centx)*sin(pi/6)+(temStarty-Centy)*cos(pi/6)+Centy;
lineEndy=(temEndx-Centx)*sin(pi/6)+(temEndy-Centy)*cos(pi/6)+Centy;
temStartx=lineSt