帮忙呀,距阵问题
有一个3*3的距阵,其中X代表未知数,从2,4,6,8中选一个数代替X,使其行,列,对角线三数之和都为15 。想了好久都搞不清思路,谁来帮我一下,谢谢
x 1 x
3 5 7
x 9 x
#include <math.h> #include <string.h> typedef struct { float x, y, z, w; }vec4; typedef float mat16[16]; void MatrixClear(mat16 matrix); void MatrixCopy(mat16 dstmat, mat16 srcmat); vec4 VectorMatrixMultiply(vec4 v, mat16 matrix); void MatrixRotate(double angle, float x, float y, float z, mat16 matrix); void MatrixProduct(mat16 dstmat, mat16 srcmat, mat16 m); void MatrixTranslate(mat16 matrix, float x, float y, float z); void MatrixZero(mat16 matrix); vec4 VectorProduct(vec4 v1, vec4 v2); void MatrixClear(mat16 matrix) { matrix[0] = 1.0f; matrix[1] = 0.0f; matrix[2] = 0.0f; matrix[3] = 0.0f; matrix[4] = 0.0f; matrix[5] = 1.0f; matrix[6] = 0.0f; matrix[7] = 0.0f; matrix[8] = 0.0f; matrix[9] = 0.0f; matrix[10] = 1.0f; matrix[11] = 0.0f; matrix[12] = 0.0f; matrix[13] = 0.0f; matrix[14] = 0.0f; matrix[15] = 1.0f; } void MatrixCopy(mat16 dstmat, mat16 srcmat) { dstmat[0] = srcmat[0]; dstmat[1] = srcmat[1]; dstmat[2] = srcmat[2]; dstmat[3] = srcmat[3]; dstmat[4] = srcmat[4]; dstmat[5] = srcmat[5]; dstmat[6] = srcmat[6]; dstmat[7] = srcmat[7]; dstmat[8] = srcmat[8]; dstmat[9] = srcmat[9]; dstmat[10] = srcmat[10]; dstmat[11] = srcmat[11]; dstmat[12] = srcmat[12]; dstmat[13] = srcmat[13]; dstmat[14] = srcmat[14]; dstmat[15] = srcmat[15]; } vec4 VectorMatrixMultiply(vec4 v, mat16 matrix) { vec4 out; out.x = (v.x * matrix[0]) + (v.y * matrix[4]) + (v.z * matrix[8]) + matrix[12]; out.y = (v.x * matrix[1]) + (v.y * matrix[5]) + (v.z * matrix[9]) + matrix[13]; out.z = (v.x * matrix[2]) + (v.y * matrix[6]) + (v.z * matrix[10]) + matrix[14]; return out; } void MatrixRotate(double angle, float x, float y, float z, mat16 matrix) { float sine = (float)sin(angle); float cosine = (float)cos(angle); float sinAngle = (float)sin(3.14 * angle / 180); float cosAngle = (float)cos(3.14 * angle / 180); float oneSubCos = 1.0f - cosAngle; matrix[0] = (x * x) * oneSubCos + cosAngle; matrix[4] = (x * y) * oneSubCos - (z * sinAngle); matrix[8] = (x * z) * oneSubCos + (y * sinAngle); matrix[1] = (y * x) * oneSubCos + (sinAngle * z); matrix[5] = (y * y) * oneSubCos + cosAngle; matrix[9] = (y * z) * oneSubCos - (x * sinAngle); matrix[2] = (z * x) * oneSubCos - (y * sinAngle); matrix[6] = (z * y) * oneSubCos + (x * sinAngle); matrix[10] = (z * z) * oneSubCos + cosAngle; } void MatrixProduct(mat16 dstmat, mat16 srcmat, mat16 m) { // Return the value of this vector * m. mat16 newMatrix; const float *m1 = srcmat, *m2 = m; newMatrix[0] = m1[0] * m2[0] + m1[4] * m2[1] + m1[8] * m2[2]; newMatrix[1] = m1[1] * m2[0] + m1[5] * m2[1] + m1[9] * m2[2]; newMatrix[2] = m1[2] * m2[0] + m1[6] * m2[1] + m1[10] * m2[2]; newMatrix[3] = 0; newMatrix[4] = m1[0] * m2[4] + m1[4] * m2[5] + m1[8] * m2[6]; newMatrix[5] = m1[1] * m2[4] + m1[5] * m2[5] + m1[9] * m2[6]; newMatrix[6] = m1[2] * m2[4] + m1[6] * m2[5] + m1[10] * m2[6]; newMatrix[7] = 0; newMatrix[8] = m1[0] * m2[8] + m1[4] * m2[9] + m1[8] * m2[10]; newMatrix[9] = m1[1] * m2[8] + m1[5] * m2[9] + m1[9] * m2[10]; newMatrix[10] = m1[2] * m2[8] + m1[6] * m2[9] + m1[10] * m2[10]; newMatrix[11] = 0; newMatrix[12] = m1[0] * m2[12] + m1[4] * m2[13] + m1[8] * m2[14] + m1[12]; newMatrix[13] = m1[1] * m2[12] + m1[5] * m2[13] + m1[9] * m2[14] + m1[13]; newMatrix[14] = m1[2] * m2[12] + m1[6] * m2[13] + m1[10] * m2[14] + m1[14]; newMatrix[15] = 1; memcpy(dstmat, newMatrix, sizeof(mat16)); } void MatrixTranslate(mat16 matrix, float x, float y, float z) { // To translate a 4x4 matrix you must replace the bottom row values. The first // which is matrix[12] is for x, [13] is the y, and so on. The last one is set to 1.0. matrix[12] = x; matrix[13] = y; matrix[14] = z; matrix[15] = 1.0f; } void MatrixZero(mat16 matrix) { // To set the matrix to zero you set all the values in the matrix like so... matrix[0] = 0.0f; matrix[1] = 0.0f; matrix[2] = 0.0f; matrix[3] = 0.0f; matrix[4] = 0.0f; matrix[5] = 0.0f; matrix[6] = 0.0f; matrix[7] = 0.0f; matrix[8] = 0.0f; matrix[9] = 0.0f; matrix[10] = 0.0f; matrix[11] = 0.0f; matrix[12] = 0.0f; matrix[13] = 0.0f; matrix[14] = 0.0f; matrix[15] = 0.0f; } vec4 VectorProduct(vec4 v1, vec4 v2) { vec4 ret; ret.x = v1.x * v2.x; ret.y = v1.y * v2.y; ret.z = v1.z * v2.z; return ret; }