中点画直线算法 (4)
/* 三视图, 正轴测图, 三维错切, 斜轴测图(斜二测) */
#include "math.h"
#include "graphics.h"
#define PI 3.141592654
#define RAD PI/180
#define N 15 /* 立体的顶点数 */
void draw(float q[][4]);
void PxT(float t[][4]);
void TxT(float t1[][4], float t2[][4]);
void PxT_axis(float t[][4]); /* 坐标轴端点的变换 */
void draw_axis(float q[][4]); /* 画坐标轴 */
float p[N][4]={{0, 0, 80, 1}, {0, 40, 80, 1}, {50, 100, 80, 1}, {120, 100, 80, 1},
{120, 0, 80, 1},
{0, 0, 80, 1}, {0, 0, 0, 1}, {0, 40, 0, 1}, {50, 100, 0, 1}, {120, 100, 0, 1},
{120, 100, 80, 1}, {50, 100, 80, 1}, {50, 100, 0, 1}, {0, 40, 0, 1},
{0, 40, 80, 1}}; /* 立体的顶点坐标 */
int tp[]={0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1};
/* 立体抬落笔码 */
float axis_p[6][4]={{0, 0, 0, 1}, {250, 0, 0, 1}, {0, 0, 0, 1}, {0, 150, 0, 1}, {0,
0, 0, 1}, {0, 0, 120, 1}};/* 坐标轴端点 */
float axis_tp[6]={0, 1, 0, 1, 0, 1};
/* 坐标轴抬落笔码 */
float T[4][4]; /* 存放两个变换阵的积,见TxT子程 */
float vx=0, vy=200, hy=170, wx=160; /* 三视图位置 */
float ax=140, ay=20, af=35.267, bt=45; /* 正等测图位置及转角 */
float /* x轴向错切 */ d=0, g=0.2, /* y轴向错切 */ b=0, h=0, /* z轴向错切 */ c=0,
f=0, /* 平移 */ bx=0, by=0; /* 三维错切 */
float cx=300, cy=20, g0=0.364, h0=-0.364; /* 斜二测图位置及转角 */
void main()
{
float tv[4][4], th[4][4], tw[4][4], to[4][4], ts[4][4], tx[4][4];
int i, j, driver, mode;
float CB, SB, CA, SA;
driver=DETECT;
initgraph(&driver, &mode," ");
CB=cos(bt*RAD); SB=sin(bt*RAD); CA=cos(af*RAD); SA=sin(af*RAD);
for(i=0; i<4; i++)
for(j=0; j<4; j++)
tv[i][j]=th[i][j]=tw[i][j]=to[i][j]=ts[i][j]=tx[i][j]=0;
/* 主视变换阵 */
tv[0][0]=1; tv[1][1]=1; tv[3][3]=1; tv[3][0]=vx; tv[3][1]=vy;
/* 俯视变换阵 */
th[0][0]=1; th[2][1]=-1; th[3][3]=1; th[3][0]=vx; th[3][1]=hy;
/* 左视变换阵 */
tw[1][1]=1; tw[2][0]=1; tw[3][3]=1; tw[3][0]=wx; tw[3][1]=vy;
/* 正轴测变换阵 */
to[0][0]=CB; to[0][1]=SB*SA; to[1][1]=CA; to[2][0]=SB; to[2][1]=-CB*SA; to[3][0]=ax; to[3][1]=ay;
/* 错切变换阵 */
ts[0][0]=1; ts[1][1]=1; ts[2][2]=1; ts[3][3]=1; ts[0][1]=b; ts[0][2]=c; ts[1][0]=d; ts[1][2]=f;
ts[2][0]=g; ts[2][1]=h; ts[3][0]=bx; ts[3][1]=by;
/* 斜轴测变换阵 */
tx[0][0]=1; tx[1][1]=1; tx[3][3]=1; tx[2][0]=g0; tx[2][1]=h0; tx[3][0]=cx; tx[3][1]=cy;
setcolor(WHITE);
PxT(tv); /* 主视图 */
PxT(th); /* 俯视图 */
PxT(tw); /* 左视图 */
PxT(to); /* 正轴测 */
setcolor(RED);
TxT(ts, to);/* 错切*轴测 */ PxT(T); /* 错切立体图 */
setcolor(CYAN);
PxT(tx); /* 斜轴测 */
/* 以下画坐标轴 */
setcolor(YELLOW);
PxT_axis(to);
getch();
closegraph();
}
void PxT(float t[][4]) /* 立体顶点的变换 */ {
float q[N][4];
int i, j, k;
for(i=0; i