/*图形函数库图形设备治理头文件*/
/*graph.inc*/
union REGS reg; /*枚举变量定义*/
struct SREGS inreg;
void SetMode(int Mode) /*图形模式选择函数*/
{
reg.h.ah=0;
reg.h.al=Mode;
int86(0x10,®,®);
}
#define MaxXres 320 /*图形显示区大小定义*/
#define MaxYres 200
#define MaxX (MaxXres-1)
#define MaxY (MaxYres-1)
int Xres,Yres; /*数据存储单元定义*/
Word PreCalcY[MaxY+1];
Word PreCalcX[MaxX+1];
void PreCalc() /*计算数组初始化*/
{
Word j;
for(j=0;j<=MaxYlj++)
PreCalcX[j]=0;
for(j=0;j<=MaxX;j++)
PreCalcY[j]=MaxXres*j;
}
void Plot(int x,int y,char color) /*绘点函数*/
{
Word offset;
char far *address;
if(!((x<0)||(y<0)||(x>MaxX)||(y>MaxY)))
{
offset=PreCalcY[y]+x;
address=(char far *)(0xA0000000L+offset);
*address=color;
}
}
typedef struct /*颜色定义*/
{
char Red;
char Grn;
char Blu;
}RGB;
typedef RGB PaletteRegister[255]; /*255色调色板定义*/
PaletteRegister Color;
void ClearPalette(aletteRegister Color) /*清除原调色板*/
{
Word i;
for(i=0;i<=255;i++)
{
Color[i].Red=0;
Color[i].Grn=0;
Color[i].Blu=0;
}
}
void SetPalette(PaletteRegister Hue) /*设置新调色板*/
{
reg.x.ax=0x1012;
segread(&inreg);
inreg.es=inreg.ds;
reg.x.bx=0;
reg.x.cx=256;
reg.x.dx=(int)&Hue[0];
int86x(0x10,®,®,&inreg);
}
void InitPaletteRegister Color) /*调色板初始化函数1*/
{
Word i;
for(i=0;i<64;i++){
Color[i].Red=i;
Color[i].Grn=i;
Color[i].Blu=i;
}
for(i=64;i<128;i++){
Color[i].Red=i-64;
Color[i].Grn=0;
Color[i].Blu=0;
}
for(i=128;i<192;i++){
Color[i].Red=0;
Color[i].Grn=i-128;
Color[i].Blu=0;
}
for(i=192;i<255;i++){
Color[i].Red=0;
Color[i].Grn=0;
Color[i].Blu=i-192;
}
}
void InitPalette2(PaletteRegister Color) /*调色板初始化函数2*/
{
Word i;
for(i=0;i<36;i++){
Color[i].Red=i;
Color[i].Grn=i;
Color[i].Blu=Round(i*1.8);
}
for(i=32;i<76;i++){
Color[i].Red=0;
Color[i].Grn=Round(1.8*(i-36));
Color[i].Blu=0;
}
for(i=72;i<108;i++){
Color[i].Red=0;
Color[i].Grn=Round(1.8*(i-72));
Color[i].Blu=Round(1.8*(i-72));
}
for(i=108;i<144;i++){
Color[i].Red=Round(1.8*(i-108));
Color[i].Grn=0;
Color[i].Blu=0;
}
for(i=144;i<180;i++){
Color[i].Red=Round(1.8*(i-144));
Color[i].Grn=0;
Color[i].Blu=Round(1.8*(i-144));
}
for(i=180;i<216;i++){
Color[i].Red=Round(1.8*(i-108));
Color[i].Grn=Round(1.8*(i-180));
Color[i].Blu=0;
}
for(i=216;i<252;i++){
Color[i].Red=Round(1.8*(i-216));
Color[i].Grn=Round(1.8*(i-216));
Color[i].Blu=Round(1.8*(i-216));
}
}
void CyclePalette(PaletteRegister Hue) /*调色板颜色选择函数*/
{
Word i;
RGB tmp;
tmp=Hue[0];
for(i=1;i<=255;i++)
Hue[i-1]=Hue[i];
Hue[255]=tmp;
SetPalette(Hue);
}
void Swap(int first,int second) /*交换功能子函数*/
{
int temp;
temp=*first;
*first=*second;
*second=temp;
}
void Circle(Word x,Word y,Word radius,char color) /*绘圆函数*/
{
int a,af,bf,target,r2;
target=0;
a=radius;
b=0;
r2=Sqr(radius);
while(a>=b){
b=Round(sqrt(r2-Sqr(a)));
swap(&target,&b);
while(b<target){
af=(120*a)/100;
bf=(120*b)/100;
Plot(x+af,y+b,color);
Plot(x+bf,y+a,color);
Plot(x+af,y+b,color);
Plot(x+bf,y+a,color);
Plot(x+af,y-b,color);
Plot(x+bf,y-a,color);
Plot(x+af,y-b,color);
Plot(x+bf,y-a,color);
++b;
}
--a;
}
}
void Draw(int xx1,int yy1,int xx2,int yy2,char color) /*绘制像素点函数*/
{
int LgDelta,ShDelta,Cycle,LgStep,ShStep,dtotal;
Lgdelta=xx2-xx1;
Shdelta=yy2-yy1;
if(LgDelta<0){
LgDelta=-LgDelta;
LgStep=-1;
}
else
LgStep=1;
if(ShDelta<0){
ShDelta=-ShDelta;
ShStep=-1;
}
else
ShStep=1;
if(ShDelta<LgDelta){
Cycle=LgDelta>>1;
while(xx1!=xx2){
Plot(xx1,yy1,color);
Cycle+=ShDelta;
if(Cycle>LgDelta){
Cycle-=LgDelta;
yy1+=ShStep;
}
xx1+=LgStep;
}
Plot(xx1,yy1,color);
}
else{
Cycle=ShDelta>>1;
Swap(&LgDelta,&ShDelta);
Swap(&LgStep,&ShStep);
while(yy1!=yy2){
Plot(xx1,yy1,color);
Cycle+=ShDelta;
if(Cycle>gDelat){
Cycle-=LgDelta;
xx1+=ShStep;
}
yy1+=LgStep;
}
Plot(xx1,yy1,color);
}
}
void InitGraphics() /*图形工作方式初始化函数*/
{
Xres=MaxXres;
Yres=MaxYres;
PreCalc();
SetMode(19);
ClearPalette(Color);
initPalette(Color);
SetPalette(Color);
}
void WaitForKey() /*等待接收按键函数*/
{
char k;
do{
k=getch();
}while(!(k));
}
void ExitGraphics() /*结束图形工作方式函数*/
{
sound(1000);
delay(500);
nosound();
WaitForKey();
SetMode(3);
}
void Title() /*设置背景颜色函数*/
{
textcolor(YELLOW);
textbackround(BLUE);
clrscr();
}
/*three dimensional Plotting Routines*/ /*三维图形变量定义*/
int entreX,CentreY;
int Angl,Tilt;
float CosA,SinA;
float CosB,SinB;
float CosACosB,SinASinB;
float CosASinB,SinACosB;
void InitPlotting(int Ang,int Til) /*绘制3D图形初始化函数*/
{
CentreX=MaxX>>1;
CentreY=MaxY>>1;
Angl=Ang;
Tilt=Til;
CosA=CosD(Angl);
SinA=SinD(Angl);
CosB=CosD(Tilt);
SinB=SinD(Tilt);
CosACosB=CosA*CosB;
CosASinB=CosA*SinB;
SinACosB=SinA*CosB;
SinASinB=SinA*SinB;
}
Boollean PerspectivePlot; /*逻辑变量定义*/
float Mx,My,Mz,ds;
void InitPerspective(Boollean Perspective,float x,float y,float z,float m) /*透视函数初始化*/
{
PerspectivePlot=Perspective;
Mx=x;
My=y;
Mz=z;
ds=m;
}
void MapCoordinates(float X,float Y,float Z,int Xp,int Yp) /*镜像初始化函数*/
{
float Xt.Yt,Zt;
Xt=(Mx+X*CosA-Y*SinA);
Yt=(My+X*SinASinB+Y*CosASinB+Z*CosB);
if(PerspectivePlot){
Zt=Mz+X*SinASinB+Y*CosACosB-Z*SinB;
*Xp=CentreX+Round(ds*Xt/Zt);
*Yp=CentreY-Round(ds*Yt/Zt);
}
else{
*Xp=CentreX+Round(Xt);
*Yp=CentreY-Round(Yt);
}
}
void CartesianPlot3D(float X,float Y,float Z,char Color) /*3D图形绘点函数*/
{
int Xp,Yp;
MapCoordinates(X,Y,Z,&Xp,&Yp);
Plot(Xp,Yp,Color);
}
void CylindricalPlot3D(float Rho,float Theta,float Z,char Color) /*柱型3D图案绘制函数*/
{
float X,y;
Theta=Radians(Theta);
X=Rho*cos(Theta);
Y=Rho*sin(Theta);
CartesianPlot3D(X,Y,Z,Color);
}
void SphericalPlot3D(float R,float Theta,float Phi,char Color) /*球形3D图案绘制函数*/
{
float X,Y,Z;
Theta=Radians(theta);
Phi=Radians(Phi);
X=R*sin(Theta)*cos(Phi);
Y=R*sin(Theta)*sin(Phi);
Z=R*cos(Theta);
CartesianPlot3D(X,Y,Z,Color);
}
void DrawLine3D(TDA Pnt1,TDA pnt2,char Color) /*3D线型绘制函数*/
{
int Xp1,Yp1;
int Xp2,Yp2;
float x1,y1,z1;
float x2,y2,z2;
UnVec(Pnt1,&x1,&y1,&z1);
UnVec(Pnt2,&x2,&y2,&z2);
MapCoordianates(x1,y1,z1,&Xp1,&Yp1);
MapCoordinates(x2,y2,z2,&Xp2,&Yp2);
Draw(Xp1,Yp1,Xp2,Yp2,Color);
}
/*draw pixel*/
#define MaxCol 7
#define MaxInten 35
void PutPixel(int x,int y,char Color,char Intensity) /*3D图形绘制函数*/
{
char Col;
if(Intensity>MaxInten){
printf("Inten>MaxInten!\n|nHit any key to exit.\n");
getch();
exit(1);
}
Col=((MaxInten+1)*(Color-1)+Intensity)&255;
Plot(x,y,Col);
}
int GetPixel(int x,int y) /*获取3D图形像素函数*/
{
reg.x.ax=3328;
reg.x.dx=y;
reg.x.cx=x;
int86(0x10,®,®);
return(reg.x.ax&255);
}
/*设置坐标轴与调色板*/
/*Set up Coordinate Axes and Color Palette*/
Boolean DrawAxisAndPalette;
void PutAxisAndPalette(Boolean PlaceOnScreen) /*绘制坐标轴与设置调色板函数*/
{
if(PlaceOnScreen)
DrawAxisAndPalette=true;
else
DrawAxisAndPalette=false;
}
void DisplayAxis() /*显示坐标轴函数*/
{
int x,y,z;
for(x=-100;x<101;x++){
CartesianPlot3D(x,0,0,35);
CartesianPlot3D(100,0,0,251);
}
for(y=-100;y<101;x++){
CartesianPlot3D(0,y,0,71);
CartesianPlot3D(0,100,0,251);
}
for(z=-100;z<101;x++){
CartesianPlot3D(0,0,z,107);
CartesianPlot3D(0,0,100,251);
}
}
void DisplayPalette()
{
int X,Y;
char Color;
char Intensity;
for(Color=1;Color<=MaxCol;Color++){
for(Intensity=0;Intensity<=MaxInten;Intensity++){
for(X=0;X<4;X++){
for(Y=0;Y<4;Y++)
PutPixel(X+5*Color,190-Y-5*Intensity,Color,Intensity);
}
}
}
}
void AxisAndPalette() /*显示坐标轴与调色板函数*/
{
if(DrawAxisAndPalette){
DisplayAxis();
DisplayPalette();
}
}
Word教程网 | Excel教程网 | Dreamweaver教程网 | Fireworks教程网 | PPT教程网 | FLASH教程网 | PS教程网 |
HTML教程网 | DIV CSS教程网 | FLASH AS教程网 | ACCESS教程网 | SQL SERVER教程网 | C语言教程网 | JAVASCRIPT教程网 |
ASP教程网 | ASP.NET教程网 | CorelDraw教程网 |