include/vector.h
branchpyrit
changeset 74 09aedbf5f95f
parent 46 6493fb65f0b1
child 78 9569e9f35374
equal deleted inserted replaced
73:a5127346fbcd 74:09aedbf5f95f
    64 		Float f = 1.0f / mag();
    64 		Float f = 1.0f / mag();
    65 		x *= f;
    65 		x *= f;
    66 		y *= f;
    66 		y *= f;
    67 		z *= f;
    67 		z *= f;
    68 		return *this;
    68 		return *this;
    69 	}
    69 	};
    70 
    70 
    71 	// get normalized copy
    71 	// get normalized copy
    72 	Vector3 unit() const
    72 	Vector3 unit() const
    73 	{
    73 	{
    74 		Vector3 u(*this);
    74 		Vector3 u(*this);
    75 		return u.normalize();;
    75 		return u.normalize();
    76 	}
    76 	};
    77 
    77 
    78 	// square magnitude, magnitude
    78 	// square magnitude, magnitude
    79 	Float mag2() const	{ return x * x + y * y + z * z; }
    79 	Float mag2() const	{ return x * x + y * y + z * z; };
    80 	Float mag() const	{ return sqrtf(mag2()); }
    80 	Float mag() const	{ return sqrtf(mag2()); };
    81 
    81 
    82 	// negative
    82 	// negative
    83 	Vector3 operator-() const { return Vector3(-x, -y, -z); }
    83 	Vector3 operator-() const { return Vector3(-x, -y, -z); };
    84 
    84 
    85 	// accumulate
    85 	// accumulate
    86 	Vector3 operator+=(const Vector3 &v)
    86 	Vector3 operator+=(const Vector3 &v)
    87 	{
    87 	{
    88 		x += v.x;
    88 		x += v.x;
   128 
   128 
   129 	// product of vector and scalar
   129 	// product of vector and scalar
   130 	friend Vector3 operator*(const Vector3 &v, const Float &f)
   130 	friend Vector3 operator*(const Vector3 &v, const Float &f)
   131 	{
   131 	{
   132 		return Vector3(f * v.x, f * v.y, f * v.z);
   132 		return Vector3(f * v.x, f * v.y, f * v.z);
   133 	}
   133 	};
   134 
   134 
   135 	friend Vector3 operator*(const Float &f, const Vector3 &v)
   135 	friend Vector3 operator*(const Float &f, const Vector3 &v)
   136 	{
   136 	{
   137 		return v * f;
   137 		return v * f;
   138 	};
   138 	};
   139 
   139 
       
   140 	// scalar division
       
   141 	friend Vector3 operator/(const Vector3 &v, const Float &f)
       
   142 	{
       
   143 		return Vector3(v.x / f, v.y / f, v.z / f);
       
   144 	};
       
   145 
       
   146 	friend Vector3 operator/(const Float &f, const Vector3 &v)
       
   147 	{
       
   148 		return Vector3(f / v.x, f / v.y, f / v.z);
       
   149 	};
       
   150 
   140 	// vector plus scalar
   151 	// vector plus scalar
   141 	friend Vector3 operator+(const Vector3 &v, const Float &f)
   152 	friend Vector3 operator+(const Vector3 &v, const Float &f)
   142 	{
   153 	{
   143 		return Vector3(v.x + f, v.y + f, v.z + f);
   154 		return Vector3(v.x + f, v.y + f, v.z + f);
   144 	}
   155 	};
   145 
   156 
   146 	// vector minus scalar
   157 	// vector minus scalar
   147 	friend Vector3 operator-(const Vector3 &v, const Float &f)
   158 	friend Vector3 operator-(const Vector3 &v, const Float &f)
   148 	{
   159 	{
   149 		return Vector3(v.x - f, v.y - f, v.z - f);
   160 		return Vector3(v.x - f, v.y - f, v.z - f);
   150 	}
   161 	};
   151 
   162 
   152 	// cell by cell product (only usable for colours)
   163 	// cell by cell product (only usable for colours)
   153 	friend Vector3 operator*(const Vector3 &a,  const Vector3 &b)
   164 	friend Vector3 operator*(const Vector3 &a,  const Vector3 &b)
   154 	{
   165 	{
   155 		return Vector3(a.x * b.x, a.y * b.y, a.z * b.z);
   166 		return Vector3(a.x * b.x, a.y * b.y, a.z * b.z);
   157 
   168 
   158 	// print
   169 	// print
   159 	friend ostream & operator<<(ostream &st, const Vector3 &v)
   170 	friend ostream & operator<<(ostream &st, const Vector3 &v)
   160 	{
   171 	{
   161 		return st << "(" << v.x << ", " << v.y  << ", " << v.z << ")";
   172 		return st << "(" << v.x << ", " << v.y  << ", " << v.z << ")";
   162 	}
   173 	};
   163 };
   174 };
   164 
   175 
   165 typedef Vector3 Colour;
   176 typedef Vector3 Colour;
   166 
   177 
   167 #endif
   178 #endif