您的位置:

c语言透视投影,投影与透视

本文目录一览:

求C语言编的程序 急用

/*显示一个立方体*/

#include dos.h

#include math.h

#include conio.h

#include stdio.h

#include stdlib.h

#include graphics.h

#define PI 3.1415926

/*定义按键*/

#define ESC 0x11b

/*以下4个键,依次是上 下 左 右*/

#define X_axis_clkwise 0x4800

#define X_axis_Cntclkwise 0x5000

#define Y_axis_clkwise 0x4b00

#define Y_axis_Cntclkwise 0x4d00

/*以下2个键,依次是A, D*/

#define Z_axis_clkwise 0x1e61

#define Z_axis_Cntclkwise 0x2064

#define Distance_forward 0x1177

#define Distance_Backward 0x1f73

/*以下6个键,依次是U, J, I, K, O, L*/

#define X_Delta_Plus 0x1675

#define X_Delta_Minus 0x246a

#define Y_Delta_Plus 0x1769

#define Y_Delta_Minus 0x256b

#define Z_Delta_Plus 0x186f

#define Z_Delta_Minus 0x266c

/*绕X轴旋转矩阵*/

float X_Rotate_Matrix[4][4] = { 1, 0, 0, 0,

0, 1, 0, 0,

0, 0, 1, 0,

0, 0, 0, 1 };

/*绕Y轴旋转矩阵*/

float Y_Rotate_Matrix[4][4] = { 1, 0, 0, 0,

0, 1, 0, 0,

0, 0, 1, 0,

0, 0, 0, 1 };

/*绕Z轴旋转矩阵*/

float Z_Rotate_Matrix[4][4] = { 1, 0, 0, 0,

0, 1, 0, 0,

0, 0, 1, 0,

0, 0, 0, 1 };

/*平移矩阵*/

float Transist_Matrix[4][4] = { 1, 0, 0, 0,

0, 1, 0, 0,

0, 0, 1, 0,

0, 0, 0, 1 };

/*透视投影变换矩阵*/

float Perspective_Projection[4][4] = { 1, 0, 0, 0,

0, 1, 0, 0,

0, 0, 0, 0,

0, 0, 0, 1 };

int num;

float *Matrix_Mul(float *pMatrix1, int Num_Row_Matrix1, int Num_Column_Matrix1,

float *pMatrix2, int Num_Row_Matrix2, int Num_Column_Matrix2 ) {

/*实现两个矩阵:

输入参数: *pMatrix1: 指向第一个矩阵

Num_Row_Matrix1: 第一个矩阵的行数

Num_Column_Matrix1: 第一个矩阵的列数

余下三个参数类推;

return 指向运算结果的float类型指针.*/

int i, j, m, n;

float *pNewMatrix1, *pNewMatrix2, Sum;

if( Num_Column_Matrix1 != Num_Row_Matrix2) {

printf("Invalid Matrixs!\n");

return 0;

}

pNewMatrix1 = malloc(Num_Row_Matrix1 * Num_Column_Matrix2 * 4);

/*申请内存空间, Size(/bytes) = 第一个矩阵的行数 * 第二个矩阵的列数 * 4(= sizeof(float))*/

pNewMatrix2 = pNewMatrix1;

/*具体算法详见如下代码*/

for( i = 0; i Num_Row_Matrix1; i++) {

for( n = 0; n Num_Column_Matrix2; n++) {

Sum = 0;

for( j = 0; j Num_Column_Matrix1; j++)

Sum += (*(pMatrix1+i*Num_Column_Matrix1+j)) * (*(pMatrix2+j*Num_Column_Matrix2+n));

*(pNewMatrix1++) = Sum;

}

}

return pNewMatrix2;

}

/*转换成齐次坐标矩阵*/

void Matrix_Convertion(float *pMatrix, int Num_Row) {

int i, j;

for(i = 0; i Num_Row; i++) {

if((*(pMatrix+i*4+3)) != 0) {

*(pMatrix+i*4) = (*(pMatrix+i*4)) / (*(pMatrix+i*4+3));

*(pMatrix+i*4+1) = (*(pMatrix+i*4+1)) / (*(pMatrix+i*4+3));

*(pMatrix+i*4+2) = (*(pMatrix+i*4+2)) / (*(pMatrix+i*4+3));

}

}

}

/*取得投影坐标*/

float *Get_X_Y(float *pMatrix, int Num_Row) {

int i, j, Num;

float *pNewMatrix;

Num = 0;

for(i = 0; i Num_Row; i++) {

if((*(pMatrix+i*4+3)) != 0)

Num++;

}

pNewMatrix = malloc(Num * 2 * 4);

/*存放格式,{(x1, y1),(x2, y2), ... ,(xn, yn)}*/

for(i = 0; i Num; i++) {

if((*(pMatrix+i*4+3)) != 0) {

*(pNewMatrix+i*2) = (*(pMatrix+i*4))+300; /*显示在屏幕中心, x = 300*/

*(pNewMatrix+i*2+1) = (*(pMatrix+i*4+1))+200; /*显示在屏幕中心, y = 200*/

}

}

return pNewMatrix;

}

/*设置旋转矩阵, Rotate around aixs labled with X or Y or Z*/

void SetMatrix_X(float X_Angle) {

float CosX, SinX;

SinX = sin(X_Angle * PI /128);

CosX = cos(X_Angle * PI /128);

X_Rotate_Matrix[1][1] = CosX;

X_Rotate_Matrix[1][2] = SinX;

X_Rotate_Matrix[2][1] = -1 * SinX;

X_Rotate_Matrix[2][2] = CosX;

}

void SetMatrix_Y(float Y_Angle) {

float CosY, SinY;

SinY = sin(Y_Angle * PI /128);

CosY = cos(Y_Angle * PI /128);

Y_Rotate_Matrix[0][0] = CosY;

Y_Rotate_Matrix[0][2] = -1 * SinY;

Y_Rotate_Matrix[2][0] = SinY;

Y_Rotate_Matrix[2][2] = CosY;

}

void SetMatrix_Z(float Z_Angle) {

float CosZ, SinZ;

SinZ = sin(Z_Angle * PI /128);

CosZ = cos(Z_Angle * PI /128);

Z_Rotate_Matrix[0][0] = CosZ;

Z_Rotate_Matrix[0][1] = SinZ;

Z_Rotate_Matrix[1][0] = -1 * SinZ;

Z_Rotate_Matrix[1][1] = CosZ;

}

/*类同*/

void Set_Transist_Matrix(float X, float Y,float Z) {

Transist_Matrix[3][0] = X;

Transist_Matrix[3][1] = Y;

Transist_Matrix[3][2] = Z;

}

/*类同*/

void Set_Perspective_Projection(float k) {

Perspective_Projection[2][3] = -1/k;

}

/*初始化图形驱动*/

void InitGraph(void) {

int gd=DETECT,gm;

initgraph(gd,gm,"E:\\TC");

}

/*生成立方体*/

float *Cube(void) {

int i, j, k;

float *pPoints1, *pPoints2;

num = 0;

for( i = -50; i = 50; i += 20)

for( j = -50; j = 50; j += 20)

for( k = -50; k = 50; k += 20)

num++;

pPoints1 = malloc( num * 4 * 4 );

pPoints2 = pPoints1;

for( i = -50; i = 50; i += 20)

for( j = -50; j = 50; j += 20)

for( k = -50; k = 50; k += 20) {

*(pPoints1++) = i;

*(pPoints1++) = j;

*(pPoints1++) = k;

*(pPoints1++) = 1;

}

return pPoints2;

}

/*Functions used for drawing Clearing*/

void Plot_NewPoints(float *pPoints) {

int i;

for(i=0;inum;i++)

putpixel( (int) (*(pPoints+i*2)), (int) (*(pPoints+i*2+1)), 7);

}

void Clear_OldPoints(float *pPoints) {

int i;

for(i=0;inum;i++)

putpixel( (int) (*(pPoints+i*2)), (int) (*(pPoints+i*2+1)), 0);

}

/*Function used for controlling*/

void Operate(int Switch, float *Ang_Rot_X, float *Ang_Rot_Y, float *Ang_Rot_Z,

float *X_Delta, float *Y_Delta, float *Z_Delta,float *Distance) {

switch(Switch) {

case X_axis_clkwise: (*Ang_Rot_X)--; break;

case X_axis_Cntclkwise: (*Ang_Rot_X)++; break;

case Y_axis_clkwise: (*Ang_Rot_Y)--; break;

case Y_axis_Cntclkwise: (*Ang_Rot_Y)++; break;

case Z_axis_clkwise: (*Ang_Rot_Z)--; break;

case Z_axis_Cntclkwise: (*Ang_Rot_Z)++; break;

case X_Delta_Plus: (*X_Delta)--; break;

case X_Delta_Minus: (*X_Delta)++; break;

case Y_Delta_Plus: (*Y_Delta)--; break;

case Y_Delta_Minus: (*Y_Delta)++; break;

case Z_Delta_Plus: (*Z_Delta)++; break;

case Z_Delta_Minus: (*Z_Delta)--; break;

case Distance_forward: (*Distance)++; break;

case Distance_Backward: (*Distance)--; break;

default: (*Ang_Rot_Y)++; break;

}

}

int main() {

int i, j, Key;

float *pMatrix1, *pMatrix2;

float *pBasePoints;

float *pPerspectivePoints;

float Ang_Rot_Xaxis, Ang_Rot_Yaxis, Ang_Rot_Zaxis;

float X_Delta, Y_Delta, Z_Delta;

float Distance;

clrscr();

InitGraph();

/*Varieties initialized*/

pBasePoints = Cube();

Ang_Rot_Xaxis = 0;

Ang_Rot_Yaxis = 0;

Ang_Rot_Zaxis = 0;

X_Delta = 0;

Y_Delta = 0;

Z_Delta = 0;

Distance = 200;

Key = 0;

while(Key != ESC) {

if( bioskey(1) )

Key = bioskey(0);

Operate(Key, Ang_Rot_Xaxis, Ang_Rot_Yaxis, Ang_Rot_Zaxis,

X_Delta, Y_Delta, Z_Delta, Distance);

SetMatrix_X(Ang_Rot_Xaxis);

SetMatrix_Y(Ang_Rot_Yaxis);

SetMatrix_Z(Ang_Rot_Zaxis);

Set_Transist_Matrix(X_Delta, Y_Delta, Z_Delta);

Set_Perspective_Projection(Distance);

/*The following may be known by you

pay your attention specially to the pair of malloc free */

pMatrix1 = Matrix_Mul( (float*)X_Rotate_Matrix, 4, 4, (float*)Y_Rotate_Matrix, 4, 4);

pMatrix2 = Matrix_Mul( pMatrix1, 4, 4, (float*)Z_Rotate_Matrix, 4, 4);

free(pMatrix1);

pMatrix1 = Matrix_Mul( pMatrix2, 4, 4, (float*)Transist_Matrix, 4, 4);

free(pMatrix2);

pMatrix2 = Matrix_Mul( pMatrix1, 4, 4, (float*)Perspective_Projection, 4, 4);

free(pMatrix1);

pMatrix1 = Matrix_Mul( pBasePoints, num, 4, pMatrix2, 4, 4);

free(pMatrix2);

Matrix_Convertion( pMatrix1, num);

pPerspectivePoints = Get_X_Y(pMatrix1, num);

Plot_NewPoints(pPerspectivePoints);

delay(5000);

Clear_OldPoints(pPerspectivePoints);

free(pPerspectivePoints);

free(pMatrix1);

}

free(pBasePoints);

closegraph();

return 0;

}

c语言编的两点透视投影:我觉得函数调用部分,数组名与指针之间传递有问题,很急的。。。

修改一:

修改一下zhuanhuan函数,如下

void zhuanhuan(float (*a)[9], float bian[4][4],float Matrix[4][9]){ //改成3个参数不需要反回值,顺便给形参加上反回类型吧。

//float matri[4][9]; //这一行可以不要了

for(int i=0;i4;i++){ //xinjuzhenhangshu

for(int j=0;j9;j++){ //xinjuzhenlieshu

for(int k;k4;k++){

a[i][j]=bian[i][k]*Matrix[k][j]; //把matri改为形参a就可以了

} //jisuanzhongxiaoshidehanglieshu4

}

}

}

修改二:

main函数里面以下3句

====》matri=zhuanhuan(bian,Matrix);//xuanzhuan45du

====》matri2=zhuanhuan(pingyi,matri);//pingyichengbiaozhuntouyinggeshi

--==》matri3=zhuanhuan(M,matri2);//touying

分别修改为

zhuanhuan(matri,bian,Matrix);

matri2=zhuanhuan(matri2,pingyi,matri);

matri3=zhuanhuan(matri3, M,matri2);

原理:

函数的形参若是指针,则改变指针所指向地址的值就会改变实参的值,因此在调用matri3=zhuanhuan(matri3,M,matri2);时实参的地址matri3传递到形参int (*a)[9]时,形参a指向的地址的值就是实参matri3的值,因此改变形参的值就会改变实参matri3的值,因此只需直接调用函数即可,不须反回值。

注:你的程序究竟是干什么的还不知道,因为这还不是完整程序,程序中的line函数不知道是怎样的,还有什么VGA,VGAHI是什么也不知道,因此,只能把程序修改成这样,至于能不能达到要求那就不清楚了。 程序中的其他错误没法验证,但基本思想是这样的。

C语言做投影问题

简单的说,就是这样的,比如一个点的坐标是(x,y,z),变成(x,y)就是一种投影。你把z值去掉后算出面积,就是投影面积。然后算出原来多边形平面和xoy平面的夹角theta,投影面积除以cos(theta)就是结果。