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

📄 vector.cpp

📁 The goal of this project is to explore the idea of point-based radiosity, which is a shooting radio
💻 CPP
字号:
/*+----------------------------------------------------------------------------
  Ben Landon
  CSCI E-235
  Assignment 1

  Vector.cpp
  
  This is the implementation file for a basic Vector class.  This 
  Vector class doesn't (currently) include a homogeneous component.

*/

#include <math.h>
#include "Vector.hpp"


/*+----------------------------------------------------------------------------
  Vector default constructor
  
  This constructor set the x, y and z componets of the Vector to 
  zero.
  
*/
Vector::Vector (void)
    : m_x(0.0), m_y(0.0), m_z(0.0)
{
		    
}


/*+----------------------------------------------------------------------------
	Vector copy constructor
	
*/
Vector::Vector (const Vector& vec)
{
	m_x = vec.m_x;
	m_y = vec.m_y;
	m_z = vec.m_z;
}


/*+----------------------------------------------------------------------------
  Vector constructor. 
  
  This constructor for Vector takes three float parameters, which 
  are assignned to the x, y and z members of the Vector instance.
  
*/
Vector::Vector (float x, float y, float z)
	: m_x(x), m_y(y), m_z(z)
{
}
   

/*+----------------------------------------------------------------------------
  Vector destructor
  
  This destructor doesn't really do anything since the Vector
  class doesn't allocate memory or any system resources.  
  
*/
Vector::~Vector ()
{
    // Nothing to do, there is no dynamic allocation to free,
    // or system resources to release.
}

   
/*+-------------------------------------------------------------------
  Vector::operator= 

  Overloaded assignment operator for the Vector class.

*/
const Vector& Vector::operator=(const Vector& vec)
{
    m_x = vec.m_x;
    m_y = vec.m_y;
    m_z = vec.m_z;
    return *this;
}

  
/*+-------------------------------------------------------------------
  Vector::set

  Sets the components of this Vector object.

*/
void Vector::set (float x, float y, float z)
{
    m_x = x;
    m_y = y;
    m_z = z;
}
        

/*+-------------------------------------------------------------------
  Vector::normalize

  Normalize this vector.  Scales each component of the vector
  such that the length is one.

*/
void Vector::normalize (void)
{
    float len = this->length();

    m_x /= len;
    m_y /= len;
    m_z /= len;
}


/*+----------------------------------------------------------------------------
    operator*  (cross product)
    
    This operator computes the cross product (this cross vec)
    and returns a new Vector object representing the 
    result of the cross product.

*/
Vector Vector::operator* (const Vector& vec) const
{

    float result_x = (m_y * vec.m_z) - (vec.m_y * m_z);
    float result_y = (vec.m_x * m_z) - (m_x * vec.m_z);
    float result_z = (m_x * vec.m_y) - (vec.m_x * m_y);
    
    return Vector(result_x, result_y, result_z);
}



/*+----------------------------------------------------------------------------
    Vector::operator*
    
    Multiply a scalar times a vector
*/
Vector Vector::operator* (float t) const
{
    return Vector(t * m_x, t * m_y, t * m_z);
}

Vector Vector::operator- (const Vector& vec) const
{
    return Vector(m_x - vec.m_x, m_y - vec.m_y, m_z - vec.m_z);
}


float Vector::length (void) const
{
    float length_squared = m_x * m_x + m_y * m_y + m_z * m_z;
    
    if (length_squared > 0.0)
    {
        float length = (float)sqrt(length_squared);
    
        return length;
    }
    else
    {
        return 1.0;
    }
}

 
void Vector::set_length (float len)
{
    this->normalize();
    m_x *= len;
    m_y *= len;
    m_z *= len;
}

/*+-------------------------------------------------------------------
  Vector::dot
  
  Compute the dot product of the other vector and this.
*/
float Vector::dot (const Vector& vec)
{
    float result = 0.0f;
    result += (m_x * vec.m_x);
    result += (m_y * vec.m_y);
    result += (m_z * vec.m_z);
    return result;
}


/*+-------------------------------------------------------------------
  Vector::get_perpendicular_vector (method)

  Computes a vector that has the same length as this vector,
  and is perpendicular to this vector.  

*/
void Vector::get_perpendicular_vector (Vector& vec) const 
{
    if (m_x != 0.0f)
    {
	vec.set(-m_y, m_x, 0.0f);
    }
    else if (m_y != 0.0f)
    {
	vec.set(0.0f, -m_z, m_y);
    }
    else 
    {
	vec.set(-m_z, 0.0f, m_x);
    }
}


/*+-------------------------------------------------------------------
  Vector::interpolate (static method)

*/
Vector Vector::interpolate (const Vector& A, float t, const Vector& B)
{
  float one_minus_t = 1 - t;

  float x = (one_minus_t * (A.m_x)) + (t * (B.m_x));
  float y = (one_minus_t * (A.m_y)) + (t * (B.m_y));
  float z = (one_minus_t * (A.m_z)) + (t * (B.m_z));
  
  return Vector(x, y, z);
}


/*+-------------------------------------------------------------------
  operator* (float, Vector&) (friend function)

  This function is here as a complement to Vector::operator* (float
  t).  This is so that scalar multiplication of a Vector object
  using t * vec works too.
  
*/
Vector operator*(float t, const Vector& vec)
{
  return Vector(t * (vec.m_x), t * (vec.m_y), t * (vec.m_z));
}


⌨️ 快捷键说明

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