Created
April 28, 2012 03:31
-
-
Save Slipyx/2515455 to your computer and use it in GitHub Desktop.
Wrapper and helper lib to the vectorial vec2f class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ============================================================================ | |
// Vec2f.hpp | |
// Wrapper and helper lib to the vectorial vec2f class | |
// Made available under the public domain | |
// ============================================================================ | |
#ifndef __VEC2F_H__ | |
#define __VEC2F_H__ | |
#include "vectorial/vec2f.h" | |
namespace Vec2f { | |
using namespace ::vectorial; | |
// Vec2f helper function. Sets the X value of a vec2f | |
vectorial_inline void setX( vec2f& vec, const float& xVal ) { | |
vec = vec2f( xVal, vec.y() ); | |
} | |
// Vec2f helper function. Sets the Y value of a vec2f | |
vectorial_inline void setY( vec2f& vec, const float& yVal ) { | |
vec = vec2f( vec.x(), yVal ); | |
} | |
// Vec2f helper function. Returns distance between two vec2fs | |
vectorial_inline float distance( const vec2f& vec1, const vec2f& vec2 ) { | |
return length( vec1 - vec2 ); | |
} | |
// Vec2f helper function. Returns the squared distance between two vec2fs | |
vectorial_inline float distance_squared( const vec2f& vec1, const vec2f& vec2 ) { | |
return length_squared( vec1 - vec2 ); | |
} | |
// Vec2f helper function. Normalizes the vec2f in place instead of returning a new vec2f | |
vectorial_inline void normalize_inplace( vec2f& vec ) { | |
vec = normalize( vec ); | |
} | |
// Vec2f helper function. Returns a vec2f of the point half way between two vec2fs | |
vectorial_inline vec2f midpoint( const vec2f& vec1, const vec2f& vec2 ) { | |
return (vec1 + vec2) * 0.5f; | |
} | |
// Vec2f helper function. Returns the cross product of two vec2fs | |
vectorial_inline float cross( const vec2f& vec1, const vec2f& vec2 ) { | |
return vec1.x() * vec2.y() - vec1.y() * vec2.x(); | |
} | |
// Vec2f helper function. Returns a new rotated vec2f. Angle is in radians | |
vectorial_inline vec2f rotate( const vec2f& vec, const float& angle ) { | |
const float cosa = cos( angle ); | |
const float sina = sin( angle ); | |
return vec2f( cosa * vec.x() - sina * vec.y(), sina * vec.x() + cosa * vec.y() ); | |
} | |
// Vec2f helper function. Rotates the vec2f in place instead of returning a new vec2f | |
vectorial_inline void rotate_inplace( vec2f& vec, const float& angle ) { | |
vec = rotate( vec, angle ); | |
} | |
// Vec2f helper function. Returns a vec2f perpendicular to the given vec2f | |
vectorial_inline vec2f perpendicular( const vec2f& vec ) { | |
return vec2f( -vec.y(), vec.x() ); | |
} | |
// Vec2f helper function. Returns a vec2f that is reflected on the given normal. | |
// Assumes vec is facing away from normal. | |
vectorial_inline vec2f reflect( const vec2f& vec, const vec2f& normal ) { | |
return vec2f( vec - (2.0f * dot( vec, normal ) * normal) ); | |
} | |
// Vec2f helper function. Returns the angle in radians between two direction | |
// vec2fs. Really only needed if both vec2fs are not normalized. | |
// acos( dot( v1, v2 ) ) will be enough otherwise | |
vectorial_inline float angle( const vec2f& vec1, const vec2f& vec2 ) { | |
float lenP = length( vec1 ) * length( vec2 ); | |
// Divide by zero check | |
if ( lenP < 1e-06f ) lenP = 1e-06f; | |
const float f = dot( vec1, vec2 ) / lenP; | |
return acos( (f < 1.0f ? f : 1.0f) > -1.0f ? f : -1.0f ); | |
} | |
} // Namespace Vec2f | |
using Vec2f::vec2f; | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment