📄 mtxlib.cpp
字号:
/* 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.5// Date: April 22, 2000// Authors: Dante Treglia II and Mark A. DeLoura// Thanks to: Miguel Gomez, Stan Melax, Pete Isensee, // Gabor Nagy, Scott Bilas, James Boer, Eric Lengyel//==========================================================#include "mtxlib.h"#include <cmath>#include <cassert>////////////////////////////////////////////////////////////// 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);}// Set Valuesvoid vector2::set(float xIn, float yIn){ x = xIn; y = yIn;}// 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;}// Constructor with initializing vector4vector3::vector3(const vector4 &v) { x = v.x; y = v.y; z = v.z;}// 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);}// Set Valuesvoid vector3::set(float xIn, float yIn, float zIn){ x = xIn; y = yIn; z = zIn;}// 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);}// Set Valuesvoid vector4::set(float xIn, float yIn, float zIn, float wIn){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -