Portable Half-Life SDK. GoldSource and Xash3D. Crossplatform.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

111 lines
4.3 KiB

9 years ago
/***
*
* Copyright (c) 1996-2002, Valve LLC. All rights reserved.
*
* This product contains software technology licensed from Id
* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc.
* All Rights Reserved.
*
* Use, distribution, and modification of this source code and/or resulting
* object code is restricted to non-commercial enhancements to products from
* Valve LLC. All other use, distribution, or modification is prohibited
* without written permission from Valve LLC.
*
****/
#pragma once
9 years ago
#ifndef VECTOR_H
#define VECTOR_H
//=========================================================
// 2DVector - used for many pathfinding and many other
// operations that are treated as planar rather than 3d.
//=========================================================
class Vector2D
{
public:
inline Vector2D(void): x( 0.0f ), y( 0.0f ) { }
inline Vector2D(float X, float Y): x( 0.0f ), y( 0.0f ) { x = X; y = Y; }
9 years ago
inline Vector2D operator+(const Vector2D& v) const { return Vector2D( x + v.x, y + v.y ); }
inline Vector2D operator-(const Vector2D& v) const { return Vector2D( x - v.x, y - v.y ); }
inline Vector2D operator*(float fl) const { return Vector2D( x * fl, y * fl ); }
inline Vector2D operator/(float fl) const { return Vector2D( x / fl, y / fl ); }
9 years ago
inline float Length(void) const { return sqrt(x * x + y * y ); }
9 years ago
inline Vector2D Normalize ( void ) const
{
//Vector2D vec2;
9 years ago
float flLen = Length();
9 years ago
if( flLen == 0 )
9 years ago
{
return Vector2D( 0, 0 );
}
else
{
flLen = 1 / flLen;
return Vector2D( x * flLen, y * flLen );
}
}
vec_t x, y;
};
9 years ago
inline float DotProduct( const Vector2D& a, const Vector2D& b ) { return( a.x * b.x + a.y * b.y ); }
inline Vector2D operator*( float fl, const Vector2D& v ) { return v * fl; }
9 years ago
//=========================================================
// 3D Vector
//=========================================================
class Vector // same data-layout as engine's vec3_t,
{ // which is a vec_t[3]
public:
// Construction/destruction
inline Vector( void ): x( 0.0f ), y( 0.0f ), z( 0.0f ) { }
inline Vector( float X, float Y, float Z ): x( 0.0f ), y( 0.0f ), z( 0.0f ) { x = X; y = Y; z = Z; }
9 years ago
//inline Vector( double X, double Y, double Z ) { x = (float)X; y = (float)Y; z = (float)Z; }
//inline Vector( int X, int Y, int Z ) { x = (float)X; y = (float)Y; z = (float)Z; }
inline Vector( const Vector& v ): x( 0.0f ), y( 0.0f ), z( 0.0f ) { x = v.x; y = v.y; z = v.z; }
inline Vector( float rgfl[3] ): x( 0.0f ), y( 0.0f ), z( 0.0f ) { x = rgfl[0]; y = rgfl[1]; z = rgfl[2]; }
9 years ago
// Operators
9 years ago
inline Vector operator-( void ) const { return Vector( -x, -y, -z ); }
inline int operator==( const Vector& v ) const { return x==v.x && y==v.y && z==v.z; }
inline int operator!=( const Vector& v ) const { return !( *this==v ); }
inline Vector operator+( const Vector& v ) const { return Vector( x + v.x, y + v.y, z + v.z ); }
inline Vector operator-( const Vector& v ) const { return Vector( x - v.x, y - v.y, z - v.z ); }
inline Vector operator*( float fl) const { return Vector( x * fl, y * fl, z * fl ); }
inline Vector operator/( float fl) const { return Vector( x / fl, y / fl, z / fl ); }
9 years ago
// Methods
9 years ago
inline void CopyToArray( float* rgfl ) const { rgfl[0] = x, rgfl[1] = y, rgfl[2] = z; }
inline float Length( void ) const { return sqrt( x * x + y * y + z * z ); }
9 years ago
operator float *() { return &x; } // Vectors will now automatically convert to float * when needed
operator const float *() const { return &x; } // Vectors will now automatically convert to float * when needed
9 years ago
inline Vector Normalize( void ) const
9 years ago
{
float flLen = Length();
9 years ago
if( flLen == 0 ) return Vector( 0, 0, 1 ); // ????
9 years ago
flLen = 1 / flLen;
9 years ago
return Vector( x * flLen, y * flLen, z * flLen );
9 years ago
}
9 years ago
inline Vector2D Make2D( void ) const
9 years ago
{
Vector2D Vec2;
Vec2.x = x;
Vec2.y = y;
return Vec2;
}
9 years ago
inline float Length2D( void ) const { return sqrt( x * x + y * y ); }
9 years ago
// Members
vec_t x, y, z;
};
9 years ago
inline Vector operator*( float fl, const Vector& v ) { return v * fl; }
inline float DotProduct( const Vector& a, const Vector& b ) { return( a.x * b.x + a.y * b.y + a.z * b.z); }
inline Vector CrossProduct( const Vector& a, const Vector& b ) { return Vector( a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x ); }
#endif