⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mtxlib.cpp

📁 游戏编程精髓数学部分代码和原程序不错的!不错!
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/* Copyright (C) Dante Treglia II and Mark A. DeLoura, 2000.  * All rights reserved worldwide. * * This software is provided "as is" without express or implied * warranties. You may freely copy and compile this source into * applications you distribute provided that the copyright text * below is included in the resulting source code, for example: * "Portions Copyright (C) Dante Treglia II and Mark A. DeLoura, 2000" *///==========================================================// C++ Matrix Library// Version: 2.3 <FINAL>// Date: March 19, 2000// Authors: Dante Treglia II and Mark A. DeLoura// Thanks to: Miguel Gomez, Stan Melax, Pete Isensee, //             Gabor Nagy, Scott Bilas, James Boer//==========================================================#include <cmath>#include <cstdio>#include <cassert>#include "mtxlib.h"////////////////////////////////////////////////////////////// vector2 class//// Constructor with initializing float valuesvector2::vector2(float inX, float inY) {  x = inX;  y = inY;}// Constructor with initializing vector2vector2::vector2(const vector2 &v) {  x = v.x;  y = v.y;}// Array indexingfloat &vector2::operator [] (unsigned int i) {  assert (i<2);    return *(&x+i);}// Array indexingconst float &vector2::operator [] (unsigned int i) const {  assert (i<2);  return *(&x+i);}// Assign vector2 &vector2::operator = (const vector2 &v) {  x = v.x;  y = v.y;  return *this;}// Add a vector2 to this onevector2 &vector2::operator += (const vector2 &v) {  x += v.x;  y += v.y;  return *this;}// Subtract a vector2 from this onevector2 &vector2::operator -= (const vector2 &v) {  x -= v.x;  y -= v.y;  return *this;}// Multiply the vector2 by a floatvector2 &vector2::operator *= (float f) {  x *= f;  y *= f;  return *this;}// Divide the vector2 by a floatvector2 &vector2::operator /= (float f) {  x /= f;  y /= f;  return *this;}// Are these two vector2's equal?bool operator == (const vector2 &a, const vector2 &b) {  return ((a.x == b.x) && (a.y == b.y));}// Are these two vector2's not equal?bool operator != (const vector2 &a, const vector2 &b) {  return ((a.x != b.x) || (a.y != b.y));}// Add two vector2'svector2 operator + (const vector2 &a, const vector2 &b) {  vector2 ret(a);  ret += b;  return ret;}// Subtract one vector2 from anothervector2 operator - (const vector2 &a, const vector2 &b) {  vector2 ret(a);  ret -= b;  return ret;}// Multiply vector2 by a floatvector2 operator * (const vector2 &v, float f) {  return vector2(f * v.x, f * v.y);}// Multiply vector2 by a floatvector2 operator * (float f, const vector2 &v) {  return vector2(f * v.x, f * v.y);}// Divide vector2 by a floatvector2 operator / (const vector2 &v, float f) {  return vector2(v.x / f, v.y / f);}// Negate a vector2vector2 operator - (const vector2 &a) {  return vector2(-a.x, -a.y);}// Get length of a vector2float vector2::length() const {  return (float) sqrt(x*x + y*y);}// Get squared length of a vector2float vector2::lengthSqr() const {  return (x*x + y*y);}// Does vector2 equal (0, 0)?bool vector2::isZero() const {  return ((x == 0.0F) && (y == 0.0F));}// Normalize a vector2vector2 &vector2::normalize() {  float m = length();  if (m > 0.0F)     m = 1.0F / m;  else     m = 0.0F;  x *= m;  y *= m;  return *this;}////////////////////////////////////////////////////////////// vector3 class//// Constructor with initializing float valuesvector3::vector3(float inX, float inY, float inZ) {  x = inX;  y = inY;  z = inZ;}// Constructor with initializing vector3vector3::vector3(const vector3 &v) {  x = v.x;  y = v.y;  z = v.z;}// Constructor with initializing vector2vector3::vector3(const vector2 &v) {  x = v.x;  y = v.y;  z = 0.0F;}// Array indexingfloat &vector3::operator [] (unsigned int i) {  assert (i<3);          return *(&x+i);}// Array indexingconst float &vector3::operator [] (unsigned int i) const {  assert (i<3);  return *(&x+i);}// Assignvector3 &vector3::operator = (const vector3 &v) {  x = v.x;  y = v.y;  z = v.z;  return *this;}// Assign from a vector2vector3 &vector3::operator = (const vector2 &v) {  x = v.x;  y = v.y;  z = 0.0F;  return *this;}// Add a vector3 to this onevector3 &vector3::operator += (const vector3 &v) {  x += v.x;  y += v.y;  z += v.z;  return *this;}// Subtract a vector3 from this onevector3 &vector3::operator -= (const vector3 &v) {  x -= v.x;  y -= v.y;  z -= v.z;  return *this;}// Multiply the vector3 by a floatvector3 &vector3::operator *= (float f) {  x *= f;  y *= f;  z *= f;  return *this;}// Divide the vector3 by a floatvector3 &vector3::operator /= (float f) {  x /= f;  y /= f;  z /= f;  return *this;}// Are these two vector3's equal?bool operator == (const vector3 &a, const vector3 &b) {  return ((a.x == b.x) && (a.y == b.y) && (a.z == b.z));}// Are these two vector3's not equal?bool operator != (const vector3 &a, const vector3 &b) {  return ((a.x != b.x) || (a.y != b.y) || (a.z != b.z));}// Add two vector3'svector3 operator + (const vector3 &a, const vector3 &b) {  vector3 ret(a);  ret += b;  return ret;}// Subtract one vector3 from anothervector3 operator - (const vector3 &a, const vector3 &b) {  vector3 ret(a);  ret -= b;  return ret;}// Multiply vector3 by a floatvector3 operator * (const vector3 &v, float f) {  return vector3(f * v.x, f * v.y, f * v.z);}// Multiply vector3 by a floatvector3 operator * (float f, const vector3 &v) {  return vector3(f * v.x, f * v.y, f * v.z);}// Divide vector3 by a floatvector3 operator / (const vector3 &v, float f) {  return vector3(v.x / f, v.y / f, v.z / f);}// Negate a vector3vector3 operator - (const vector3 &a) {  return vector3(-a.x, -a.y, -a.z);}// Get length of a vector3float vector3::length() const {  return (float) sqrt(x*x + y*y + z*z);}// Get squared length of a vector3float vector3::lengthSqr() const {  return (x*x + y*y + z*z);}// Does vector3 equal (0, 0, 0)?bool vector3::isZero() const {  return ((x == 0.0F) && (y == 0.0F) && (z == 0.0F));}// Normalize a vector3vector3 &vector3::normalize() {  float m = length();  if (m > 0.0F)     m = 1.0F / m;  else     m = 0.0F;  x *= m;  y *= m;  z *= m;  return *this;}////////////////////////////////////////////////////////////// vector4 class//// Constructor with initializing float valuesvector4::vector4(float inX, float inY, float inZ, float inW) {  x = inX;  y = inY;  z = inZ;  w = inW;}// Constructor with initializing vector4vector4::vector4(const vector4 &v) {  x = v.x;  y = v.y;  z = v.z;  w = v.w;}// Constructor with initializing vector3vector4::vector4(const vector3 &v) {  x = v.x;  y = v.y;  z = v.z;  w = 0.0F;}// Constructor with initializing vector2vector4::vector4(const vector2 &v) {  x = v.x;  y = v.y;  z = 0.0F;  w = 0.0F;}// Array indexingfloat &vector4::operator [] (unsigned int i) {  assert (i<4);    return *(&x+i);}// Array indexingconst float &vector4::operator [] (unsigned int i) const {  assert (i<4);    return *(&x+i);}// Assignvector4 &vector4::operator = (const vector4 &v) {  x = v.x;  y = v.y;  z = v.z;  w = v.w;  return *this;}// Assign from a vector3vector4 &vector4::operator = (const vector3 &v) {  x = v.x;  y = v.y;  z = v.z;  w = 0.0F;  return *this;}// Assign from a vector2vector4 &vector4::operator = (const vector2 &v) {  x = v.x;  y = v.y;  z = 0.0F;  w = 0.0F;  return *this;}// Add a vector4 to this onevector4 &vector4::operator += (const vector4 &v) {  x += v.x;  y += v.y;  z += v.z;  w += v.w;  return *this;}// Subtract a vector4 from this onevector4 &vector4::operator -= (const vector4 &v) {  x -= v.x;  y -= v.y;  z -= v.z;  w -= v.w;  return *this;}// Multiply the vector4 by a floatvector4 &vector4::operator *= (float f) {  x *= f;  y *= f;  z *= f;  w *= f;  return *this;}// Divide the vector4 by a floatvector4 &vector4::operator /= (float f) {  x /= f;  y /= f;  z /= f;  w /= f;  return *this;}// Are these two vector4's equal?bool operator == (const vector4 &a, const vector4 &b) {  return ((a.x == b.x) && (a.y == b.y) &&          (a.z == b.z) && (a.w == b.w));}// Are these two vector4's not equal?bool operator != (const vector4 &a, const vector4 &b) {  return ((a.x != b.x) || (a.y != b.y) ||          (a.z != b.z) || (a.w != b.w));}// Add two vector4'svector4 operator + (const vector4 &a, const vector4 &b) {  vector4 ret(a);  ret += b;  return ret;}// Subtract one vector4 from anothervector4 operator - (const vector4 &a, const vector4 &b) {  vector4 ret(a);  ret -= b;  return ret;}// Multiply vector4 by a floatvector4 operator * (const vector4 &v, float f) {  return vector4(f * v.x, f * v.y, f * v.z, f * v.w);}// Multiply vector4 by a floatvector4 operator * (float f, const vector4 &v) {  return vector4(f * v.x, f * v.y, f * v.z, f * v.w);}// Divide vector4 by a floatvector4 operator / (const vector4 &v, float f) {  return vector4(v.x / f, v.y / f, v.z / f, v.w / f);}// Negate a vector4vector4 operator - (const vector4 &a) {  return vector4(-a.x, -a.y, -a.z, -a.w);}// Get length of a vector4float vector4::length() const {  return (float) sqrt(x*x + y*y + z*z + w*w);}// Get squared length of a vector4float vector4::lengthSqr() const {  return (x*x + y*y + z*z + w*w);}// Does vector4 equal (0, 0, 0, 0)?bool vector4::isZero() const {  return ((x == 0.0F) && (y == 0.0F) && (z == 0.0F) && (w == 0.0F));}// Normalize a vector4vector4 &vector4::normalize() {  float m = length();  if (m > 0.0F)     m = 1.0F / m;  else     m = 0.0F;  x *= m;  y *= m;  z *= m;  w *= m;  return *this;}////////////////////////////////////////////////////////////// Miscellaneous vector functions//// Dot product of two vector2'sfloat DotProduct(const vector2 &a, const vector2 &b) {  return a.x*b.x + a.y*b.y;}// Dot product of two vector3'sfloat DotProduct(const vector3 &a, const vector3 &b) {  return a.x*b.x + a.y*b.y + a.z*b.z;}// Dot product of two vector4'sfloat DotProduct(const vector4 &a, const vector4 &b) {  return a.x*b.x + a.y*b.y + a.z*b.z + a.w*b.w;}// Swap two vector2'svoid SwapVec(vector2 &a, vector2 &b) {  vector2 tmp(a);  a = b;  b = tmp;}// Swap two vector3'svoid SwapVec(vector3 &a, vector3 &b) {  vector3 tmp(a);  a = b;  b = tmp;}// Swap two vector4'svoid SwapVec(vector4 &a, vector4 &b) {  vector4 tmp(a);  a = b;  b = tmp;}// Cross product of two vector3'svector3 CrossProduct(const vector3 &a, const vector3 &b) {  return vector3(a.y*b.z - a.z*b.y,                 a.z*b.x - a.x*b.z,                 a.x*b.y - a.y*b.x);}// Are these two vector2's nearly equal?bool nearlyEquals( const vector2& a, const vector2& b, float r ) {  vector2 diff = a - b; // difference  return (DotProduct(diff, diff) < r*r); // radius}// Are these two vector3's nearly equal?bool nearlyEquals( const vector3& a, const vector3& b, float r ) {  vector3 diff = a - b; // difference  return (DotProduct(diff, diff) < r*r); // radius}// Are these two vector4's nearly equal?bool nearlyEquals( const vector4& a, const vector4& b, float r ) {  vector4 diff = a - b; // difference  return (DotProduct(diff, diff) < r*r); // radius}////////////////////////////////////////////////////////////// matrix33 class//// Constructor with initializing matrix33matrix33::matrix33(const matrix33 &m) {  col[0] = m[0];  col[1] = m[1];  col[2] = m[2];}// Constructor with initializing vector3'smatrix33::matrix33(const vector3 &v0, const vector3 &v1, const vector3 &v2) {  col[0] = v0;  col[1] = v1;  col[2] = v2;}// Array indexingvector3 &matrix33::operator [] (unsigned int i) {  assert (i<3);          return (vector3&)col[i];}// Array indexingconst vector3 &matrix33::operator [] (unsigned int i) const {  assert (i<3);  return (vector3&)col[i];}// Assignmatrix33 &matrix33::operator = (const matrix33 &m) {  col[0] = m[0];  col[1] = m[1];  col[2] = m[2];  return *this;}// Add a matrix33 to this onematrix33 &matrix33::operator += (const matrix33 &m) {  col[0] += m[0];  col[1] += m[1];  col[2] += m[2];  return *this;}// Subtract a matrix33 from this onematrix33 &matrix33::operator -= (const matrix33 &m) {  col[0] -= m[0];  col[1] -= m[1];  col[2] -= m[2];  return *this;}// Multiply the matrix33 by another matrix33matrix33 &matrix33::operator *= (const matrix33 &m) {  matrix33 t;    for (unsigned int r = 0; r < 3; r++)   {    for (unsigned int c = 0; c < 3; c++)     {      float f = 0;                f += col[0][r] * m[c][0];      f += col[1][r] * m[c][1];      f += col[2][r] * m[c][2];                t[c][r] = f;    }  }    *this = t;    return *this;}// Multiply the matrix33 by a floatmatrix33 &matrix33::operator *= (float f) {  col[0] *= f;  col[1] *= f;  col[2] *= f;  return *this;}// Are these two matrix33's equal?bool operator == (const matrix33 &a, const matrix33 &b) {  return ((a[0] == b[0]) && (a[1] == b[1]) && (a[2] == b[2]));}// Are these two matrix33's not equal?bool operator != (const matrix33 &a, const matrix33 &b) {  return ((a[0] != b[0]) || (a[1] != b[1]) || (a[2] != b[2]));}// Add two matrix33'smatrix33 operator + (const matrix33 &a, const matrix33 &b) {  matrix33 ret(a);  ret += b;  return ret;}// Subtract one matrix33 from anothermatrix33 operator - (const matrix33 &a, const matrix33 &b) {  matrix33 ret(a);  ret -= b;  return ret;}// Multiply matrix33 by another matrix33matrix33 operator * (const matrix33 &a, const matrix33 &b) {  matrix33 ret(a);  ret *= b;  return ret;}// Multiply a vector3 by this matrix33vector3 operator * (const matrix33 &m, const vector3 &v) 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -