📄 veclib4d.c
字号:
* ie. Identity, Rotation (6 kinds), Translation and Scaling. *//* V4MatIdentity() * * Initialises to identity matrix. */Matrix5 *V4MatIdentity(mat)Matrix5 *mat;{ int i, j; for (i = 0; i < 5; i += 1) for (j = 0; j < 5; j += 1) mat->element[i][j] = i == j ? 1.0 : 0.0;}/* V4MatRotationXY() etc. * * Initialises a matrix to a rotation by theta about the indicated hyperplane. */Matrix5 *V4MatRotationXY(mat, theta)Matrix5 *mat;double theta;{ double s, c; s = sin(theta); c = cos(theta); V4MatIdentity(mat); mat->element[0][0] = c; mat->element[0][1] = s; mat->element[1][0] = -s; mat->element[1][1] = c; return mat;}Matrix5 *V4MatRotationXW(mat, theta)Matrix5 *mat;double theta;{ double s, c; s = sin(theta); c = cos(theta); V4MatIdentity(mat); mat->element[0][0] = c; mat->element[0][3] = s; mat->element[3][0] = -s; mat->element[3][3] = c; return mat;}Matrix5 *V4MatRotationYZ(mat, theta)Matrix5 *mat;double theta;{ double s, c; s = sin(theta); c = cos(theta); V4MatIdentity(mat); mat->element[1][1] = c; mat->element[1][2] = s; mat->element[2][1] = -s; mat->element[2][2] = c; return mat;}Matrix5 *V4MatRotationYW(mat, theta)Matrix5 *mat;double theta;{ double s, c; s = sin(theta); c = cos(theta); V4MatIdentity(mat); mat->element[1][1] = c; mat->element[1][3] = -s; mat->element[3][1] = s; mat->element[3][3] = c; return mat;}Matrix5 *V4MatRotationZX(mat, theta)Matrix5 *mat;double theta;{ double s, c; s = sin(theta); c = cos(theta); V4MatIdentity(mat); mat->element[0][0] = c; mat->element[0][2] = -s; mat->element[2][0] = s; mat->element[2][2] = c; return mat;}Matrix5 *V4MatRotationZW(mat, theta)Matrix5 *mat;double theta;{ double s, c; s = sin(theta); c = cos(theta); V4MatIdentity(mat); mat->element[2][2] = c; mat->element[2][3] = -s; mat->element[3][2] = s; mat->element[3][3] = c; return mat;}/* V4MatTranslation() * * Initialise a matrix to the translation v */Matrix5 *V4MatTranslation(mat, v)Matrix5 *mat;Vector4 *v;{ V4MatIdentity(mat); mat->element[4][0] = v->x; mat->element[4][1] = v->y; mat->element[4][2] = v->z; mat->element[4][3] = v->w; return mat;}/* V4MatScaling() * * Initialise a matrix to the scaling v */Matrix5 *V4MatScaling(mat, v)Matrix5 *mat;Vector4 *v;{ V4MatIdentity(mat); mat->element[0][0] = v->x; mat->element[1][1] = v->y; mat->element[2][2] = v->z; mat->element[3][3] = v->w; return mat;}/* MATRIX OPERATIONS * * The following functions are provided for a fast compact method * to create compound transformations. Matrix multiplication has * been folded into the functions thus eliminating the need for * many redundant calculations. For sparse matrices containing * mainly just ones and zeros this represents a considerable saving. * * Each function takes the form F(M, args) and has the effect * M := M o F1(args) where F1 is the transformation corresponding * to F. */Matrix5 *V4MatRotateXY(mat, theta)Matrix5 *mat;double theta;{ double a, b, f, g, k, l, p, q, u, v; double st, ct; st = sin(theta); ct = cos(theta); a = mat->element[0][0]; b = mat->element[0][1]; f = mat->element[1][0]; g = mat->element[1][1]; k = mat->element[2][0]; l = mat->element[2][1]; p = mat->element[3][0]; q = mat->element[3][1]; u = mat->element[4][0]; v = mat->element[4][1]; mat->element[0][0] = a * ct - b * st; mat->element[0][1] = a * st + b * ct; mat->element[1][0] = f * ct - g * st; mat->element[1][1] = f * st + g * ct; mat->element[2][0] = k * ct - l * st; mat->element[2][1] = k * st + l * ct; mat->element[3][0] = p * ct - q * st; mat->element[3][1] = p * st + q * ct; mat->element[4][0] = u * ct - v * st; mat->element[4][1] = u * st + v * ct; return mat;}Matrix5 *V4MatRotateXW(mat, theta)Matrix5 *mat;double theta;{ double a, d, f, i, k, n, p, s, u, x; double st, ct; st = sin(theta); ct = cos(theta); a = mat->element[0][0]; d = mat->element[0][3]; f = mat->element[1][0]; i = mat->element[1][3]; k = mat->element[2][0]; n = mat->element[2][3]; p = mat->element[3][0]; s = mat->element[3][3]; u = mat->element[4][0]; x = mat->element[4][3]; mat->element[0][0] = a * ct - d * st; mat->element[0][3] = a * st + d * ct; mat->element[1][0] = f * ct - i * st; mat->element[1][3] = f * st + i * ct; mat->element[2][0] = k * ct - n * st; mat->element[2][3] = k * st + n * ct; mat->element[3][0] = p * ct - s * st; mat->element[3][3] = p * st + s * ct; mat->element[4][0] = u * ct - x * st; mat->element[4][3] = u * st + x * ct; return mat;}Matrix5 *V4MatRotateYZ(mat, theta)Matrix5 *mat;double theta;{ double b, c, g, h, l, m, q, r, v, w; double st, ct; st = sin(theta); ct = cos(theta); b = mat->element[0][1]; c = mat->element[0][2]; g = mat->element[1][1]; h = mat->element[1][2]; l = mat->element[2][1]; m = mat->element[2][2]; q = mat->element[3][1]; r = mat->element[3][2]; v = mat->element[4][1]; w = mat->element[4][2]; mat->element[0][1] = b * ct - c * st; mat->element[0][2] = b * st + c * ct; mat->element[1][1] = g * ct - h * st; mat->element[1][2] = g * st + h * ct; mat->element[2][1] = l * ct - m * st; mat->element[2][2] = l * st + m * ct; mat->element[3][1] = q * ct - r * st; mat->element[3][2] = q * st + r * ct; mat->element[4][1] = v * ct - w * st; mat->element[4][2] = v * st + w * ct; return mat;}Matrix5 *V4MatRotateYW(mat, theta)Matrix5 *mat;double theta;{ double b, d, g, i, l, n, q, s, v, x; double st, ct; st = sin(theta); ct = cos(theta); b = mat->element[0][1]; d = mat->element[0][3]; g = mat->element[1][1]; i = mat->element[1][3]; l = mat->element[2][1]; n = mat->element[2][3]; q = mat->element[3][1]; s = mat->element[3][3]; v = mat->element[4][1]; x = mat->element[4][3]; mat->element[0][1] = b * ct + d * st; mat->element[0][3] = - b * st + d * ct; mat->element[1][1] = g * ct + i * st; mat->element[1][3] = - g * st + i * ct; mat->element[2][1] = l * ct + n * st; mat->element[2][3] = - l * st + n * ct; mat->element[3][1] = q * ct + s * st; mat->element[3][3] = - q * st + s * ct; mat->element[4][1] = v * ct + x * st; mat->element[4][3] = - v * st + x * ct; return mat;}Matrix5 *V4MatRotateZX(mat, theta)Matrix5 *mat;double theta;{ double a, c, f, h, k, m, p, r, u, w; double st, ct; st = sin(theta); ct = cos(theta); a = mat->element[0][0]; c = mat->element[0][2]; f = mat->element[1][0]; h = mat->element[1][2]; k = mat->element[2][0]; m = mat->element[2][2]; p = mat->element[3][0]; r = mat->element[3][2]; u = mat->element[4][0]; w = mat->element[4][2]; mat->element[0][0] = a * ct + c * st; mat->element[0][2] = - a * st + c * ct; mat->element[1][0] = f * ct + h * st; mat->element[1][2] = - f * st + h * ct; mat->element[2][0] = k * ct + m * st; mat->element[2][2] = - k * st + m * ct; mat->element[3][0] = p * ct + r * st; mat->element[3][2] = - p * st + r * ct; mat->element[4][0] = u * ct + w * st; mat->element[4][2] = - u * st + w * ct; return mat;}Matrix5 *V4MatRotateZW(mat, theta)Matrix5 *mat;double theta;{ double c, d, h, i, m, n, r, s, w, x; double st, ct; st = sin(theta); ct = cos(theta); c = mat->element[0][2]; d = mat->element[0][3]; h = mat->element[1][2]; i = mat->element[1][3]; m = mat->element[2][2]; n = mat->element[2][3]; r = mat->element[3][2]; s = mat->element[3][3]; w = mat->element[4][2]; x = mat->element[4][3]; mat->element[0][2] = c * ct + d * st; mat->element[0][3] = - c * st + d * ct; mat->element[1][2] = h * ct + i * st; mat->element[1][3] = - h * st + i * ct; mat->element[2][2] = m * ct + n * st; mat->element[2][3] = - m * st + n * ct; mat->element[3][2] = r * ct + s * st; mat->element[3][3] = - r * st + s * ct; mat->element[4][2] = w * ct + x * st; mat->element[4][3] = - w * st + x * ct; return mat;}Matrix5 *V4MatTranslate(mat, v)Matrix5 *mat;Vector4 *v;{ double e, j, o, t, y; e = mat->element[0][4]; j = mat->element[1][4]; o = mat->element[2][4]; t = mat->element[3][4]; y = mat->element[4][4]; if (e != 0.0) { mat->element[0][0] += e * v->x; mat->element[0][1] += e * v->y; mat->element[0][2] += e * v->z; mat->element[0][3] += e * v->w; } if (j != 0.0) { mat->element[1][0] += j * v->x; mat->element[1][1] += j * v->y; mat->element[1][2] += j * v->z; mat->element[1][3] += j * v->w; } if (o != 0.0) { mat->element[2][0] += o * v->x; mat->element[2][1] += o * v->y; mat->element[2][2] += o * v->z; mat->element[2][3] += o * v->w; } if (t != 0.0) { mat->element[3][0] += t * v->x; mat->element[3][1] += t * v->y; mat->element[3][2] += t * v->z; mat->element[3][3] += t * v->w; } if (y != 0.0) { mat->element[4][0] += y * v->x; mat->element[4][1] += y * v->y; mat->element[4][2] += y * v->z; mat->element[4][3] += y * v->w; } return mat;}Matrix5 *V4MatScale(mat, v)Matrix5 *mat;Vector4 *v;{ if (v->x != 1.0) { mat->element[0][0] *= v->x; mat->element[1][0] *= v->x; mat->element[2][0] *= v->x; mat->element[3][0] *= v->x; mat->element[4][0] *= v->x; } if (v->y != 1.0) { mat->element[0][1] *= v->y; mat->element[1][1] *= v->y; mat->element[2][1] *= v->y; mat->element[3][1] *= v->y; mat->element[4][1] *= v->y; } if (v->z != 1.0) { mat->element[0][2] *= v->z; mat->element[1][2] *= v->z; mat->element[2][2] *= v->z; mat->element[3][2] *= v->z; mat->element[4][2] *= v->z; } if (v->w != 1.0) { mat->element[0][3] *= v->w; mat->element[1][3] *= v->w; mat->element[2][3] *= v->w; mat->element[3][3] *= v->w; mat->element[4][3] *= v->w; } return mat;}/* Postscript * * The elements of the 5x5 matrix are labelled in the above functions * as follows: * * a = mat->element[0][0]; * b = mat->element[0][1]; * c = mat->element[0][2]; * d = mat->element[0][3]; * e = mat->element[0][4]; * f = mat->element[1][0]; * g = mat->element[1][1]; * h = mat->element[1][2]; * i = mat->element[1][3]; * j = mat->element[1][4]; * k = mat->element[2][0]; * l = mat->element[2][1]; * m = mat->element[2][2]; * n = mat->element[2][3]; * o = mat->element[2][4]; * p = mat->element[3][0]; * q = mat->element[3][1]; * r = mat->element[3][2]; * s = mat->element[3][3]; * t = mat->element[3][4]; * u = mat->element[4][0]; * v = mat->element[4][1]; * w = mat->element[4][2]; * x = mat->element[4][3]; * y = mat->element[4][4]; * * ie. * [ a b c d e ] * [ f g h i j ] * [ k l m n o ] * [ p q r s t ] * [ u v w x y ] * */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -