请大家看看我遍的一个显示3视图的程序为什么不能显示试图呢??另外每次编译后,以后运行时常常会出现俯视图(就是"the top view "下面一直到“the profile view:"这部分)输出结果为(6,9,5,1,2,6)这样的错误结果而不是正确结果:(5,9,10,8,5)大家帮帮忙啊,谢谢
有图
/*该程序中的NZ方向与初始化的数据集中的面表有关系,也既与面中的点的排列顺序有关系*/
#include <stdio.h>
#include <alloc.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <graphics.h>
static float data[]={0,0,0, 0,0,20, 40,0,20, 40,0,0,
0,30,0, 0,30,20, 40,30,20, 40,30,0,
0,40,10, 40,40,10,
4,3,2,1,4, 6,9,5,1,2,6, 2,3,7,6,2,
3,4,8,10,7,3, 4,1,5,8,4, 5,9,10,8,5,
6,7,10,9,6};
static int data_idx=0;
void ClearMatrix();
void MatrixMultiply();
void DetermineVisibility();
void DrawOneSurface();
void MatrixROTATE();
void ROTATEMATRIX();
void REQUIRE();
int NP,NF,FP;
float **P,**PT,**F;
float T[3][3]={{0,0,0},{0,0,0},{0,0,0}};
float D;
int i,j,k;
float JJ,KK,HI,R;
char str[10];
float X0,Y0;
float M,L;
float L1,L2,L3;
float MAX,MUN;
float P1,P2,P3;
void main()
{
int driver,mode;
driver=DETECT;
registerbgidriver(EGAVGA_driver);
initgraph(&driver,&mode,"c:\\tc");
cleardevice();
printf("NP,NF,FP=?");
scanf("%d,%d,%d",&NP,&NF,&FP);
P =malloc(NP*sizeof(float *));
for(i=0;i<NP;i++)
*(P+i)=malloc(3*sizeof(float));
PT=malloc(NF*sizeof(float *));
for(i=0;i<NF;i++)
*(PT+i)=malloc(3*sizeof(float));
F =malloc(NF*sizeof(int *));
for(i=0;i<NF;i++)
*(F+i)=malloc((FP+1)*sizeof(float));
printf("build list of point\n");
for(i=0;i<NP;i++)
{
for(j=0;j<3;j++,data_idx++)
{
P[i][j] =data[data_idx];
printf("%f\t",P[i][j]);
}
printf("\n");
}
printf("\n");
printf("build list of surface\n");
for(i=0;i<NF;i++)
{
for(j=0;j<FP+1;j++,data_idx++)
{
F[i][j]=data[data_idx];
printf("%f\t",F[i][j]);
if(j==0)
D=F[i][j];
if(j>0 && F[i][j] == D)
{data_idx++;
printf("\n");
break;
}
}
printf("\n");
}
printf("\n");
printf("Do You Want Three-Views?");
scanf("%s",str); /*needn't the &*/
if(strcmp(str,"NO") == 0)
goto AXONOMART;
printf("X0,Y0=?");
scanf("%f,%f",&X0,&Y0);
printf("the front view:\n");
T[0][0]=1;T[1][1]=1;
printf("L=?");scanf("%f",&L);
printf("M=?");scanf("%f",&M);
/*MATRIX MULTIPLIY*/
MatrixMultiply();
/*DETERMINE VISIBILITY;*/
DetermineVisibility();
ClearMatrix();
printf("\n");
printf("the top view:\n");
T[0][0]=1;T[2][1]=-1;
printf("M=?");scanf("%f",&M);
/*MATRIX MULTIPLIY*/
MatrixMultiply();
/*DETERMINE VISIBILITY;*/
DetermineVisibility();
ClearMatrix();
printf("\n");
printf("the profile view:\n");
T[1][1]=1;T[2][0]=1;
printf("L=?");
scanf("%f",&L);
MatrixMultiply();
DetermineVisibility();
ClearMatrix();
printf("\n");
AXONOMART:
printf("Do You Want AxNoMatr View?");
scanf("%s",str);
if(strcmp(str,"NO")==0)
goto OBLIQUE;
printf("X0,Y0=?");
scanf("%f,%f",&X0,&Y0);
/*MATRIX MULTIPLIY*/
MatrixROTATE();
/*DETERMINE VISIBILITY;*/
DetermineVisibility();
ClearMatrix();
printf("\n");
OBLIQUE:
printf("Do You Want OBLIQUE View?");
scanf("%s",str);
if(strcmp(str,"NO")==0)
goto PERSPECTIVE;
printf("X0,Y0=?");
scanf("%f,%f",&X0,&Y0);
printf("L1,L2,L3=");
scanf("%f,%f,%f",&L1,&L2,&L3);
/*MATRIX MULTIPLIY*/
T[0][0]=1;T[1][1]=1;T[2][0]=-L1/L3;T[2][1]=-L2/L3;
MatrixROTATE();
/*DETERMINE VISIBILITY;*/
DetermineVisibility();
ClearMatrix();
PERSPECTIVE:
printf("\n");
printf("DO YOU WANT PERSPECTIVE VIEW?");
scanf("%s",str);
if(strcmp(str,"NO")==0)
exit(0);
printf("X0,Y0=?");
scanf("%f,%f",&X0,&Y0);
printf("JJ=3,HI=1700,R=1/K\n");
printf("JJ,HI,R=?");
scanf("%f,%f,%f",&JJ,&HI,&R);
ROTATEMATRIX();
/*MATRIX MULTIPLIY*/
MatrixMultiply();
/*DETERMINE VISIBILITY;*/
REQUIRE();
for(i=0;i<NP;i++)
{
PT[i][2]=PT[i][2]-MAX;
PT[i][1]=PT[i][1]-HI;
for(j=0;j<3;j++)
PT[i][j]=PT[i][j]/(1+R*PT[i][2]);
}
DetermineVisibility();
/*DRAWING ONE SURFACE*/
getch();
closegraph();
for(i=0;i<NP;i++)
{free(P+i);
free(PT+i);
}
free(P);
free(PT);
for(i=0;i<NF;i++)
free(F+i);
free(F);
}
void MatrixMultiply()
{
for(i=0;i<NP;i++)
{
for(j=0;j<3;j++)
{
PT[i][j]=0;
for(k=0;k<3;k++)
PT[i][j]=PT[i][j]+P[i][k]*T[k][j];
}
PT[i][0]=PT[i][0]+L;
PT[i][1]=PT[i][1]+M;
}
}
void DetermineVisibility()
{
float XA,XB,YA,YB,NZ;
for(KK=0;KK<NF;KK++)
{
P1=F[KK][0];
P2=F[KK][1];
P3=F[KK][2];
XA=PT[P2-1][0]-PT[P1-1][0];
YA=PT[P2-1][1]-PT[P1-1][1];
XB=PT[P3-1][0]-PT[P1-1][0];
YB=PT[P3-1][1]-PT[P1-1][1];
NZ=XA*YB-XB*YA;
printf("NZ=%f\n",NZ);
if(NZ>0.001) DrawOneSurface();
}
}
void DrawOneSurface()
{
float x,x1,y,y1;
x1=PT[P1-1][0];y1=PT[P1-1][1];
for(JJ=0;JJ<FP+1;JJ++)
{
D=F[KK][JJ];
printf("D=%f\n",D);
x=PT[D-1][0]+X0;
printf("x=%f\t",x);
y=-PT[D-1][1]+Y0;
printf("y=%f\t",y);
if(JJ==0) moveto(x1,y1);
else line(x1,y1,x,y);
x1=x; y1=y;
if(JJ>0 && D==P1) break;
}
}
void ClearMatrix()
{
for(JJ=0;JJ<3;JJ++)
for(KK=0;KK<3;KK++)
T[JJ][KK]=0;
}
void MatrixROTATE()
{
float FE,CE,FT,CT;
printf("FE,CE=");
scanf("%f,%f",&FE,&CE);
FT=FE*3.14159/180;
CT=CE*3.14159/180;
T[0][0]=cos(FT);T[1][1]=cos(CT);
T[2][0]=sin(FT);T[0][1]=sin(CT);
T[2][1]=-T[0][0]*T[0][1];
T[0][1]= T[2][0]*T[0][1];
}
void ROTATEMATRIX()
{
float FT,FE;
printf("FE=");
scanf("%f",&FE);
FT=(FE*3.14159)/180;
T[0][0]=cos(FT);T[2][2]=T[0][0];
T[2][0]=sin(FT);T[0][2]=-T[2][0];
T[1][1]=1;
}
void REQUIRE()
{
MAX=PT[0][JJ-1];
MUN=MAX;
for(i=0;i<NP;i++)
{
if(PT[i][JJ-1]>MAX) MAX=PT[i][JJ-1];
if(PT[i][JJ-1]<MUN) MUN=PT[i][JJ-1];
}
}