SAGE
A Simple Academic Game Engine
Loading...
Searching...
No Matches
SageGeometry.h File Reference

Interface for geometry functions.

#include "SageDefines.h"
Include dependency graph for SageGeometry.h:
This graph shows which files directly or indirectly include this file:

Functions

const Vector2 Sage::AngleToVector (const float)
 Convert angle to vector.
 
const float Sage::VectorToAngle (const Vector2 &)
 Convert vector to angle.
 
const Vector2 Sage::VectorNormalCC (const Vector2 &)
 Counterclockwise normal.
 
void Sage::Normalize (float &)
 Normalize angle to \(\pm\pi\).
 
template<class t >
Sage::Normalize (t v)
 
const Vector2 Sage::Rotate (const Vector2 &, const float)
 Rotate vector.
 
const Vector2 Sage::Rotate (const Vector2 &, const Vector2 &, const float)
 Rotate vector.
 
Vector2 Sage::ParallelComponent (const Vector2 &, const Vector2 &)
 Parallel component of vector.
 

Function Documentation

◆ AngleToVector()

const Vector2 Sage::AngleToVector ( const float theta)

Compute a unit vector at an angle measured in radians counterclockwise from the positive X-axis. If \(\vec{v} = [v_x, v_y]\) is a unit vector with tail at the Origin and \(\theta\) is the angle between \(\vec{v}\) and the positive X-axis, then \(\sin \theta = v_y\) and \(\cos \theta = v_x\). Hence, \(\vec{v} = [\cos \theta, \sin \theta]\), as shown below. Therefore, this function returns Vector2(cosf(theta), sinf(theta)).

Parameters
thetaAngle in radians.
Returns
Unit vector at angle theta counterclockwise from positive X.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Normalize() [1/2]

void Sage::Normalize ( float & theta)

Normalize an angle in radians to \(\pm\pi\). If the angle is very large or very small, then this function will be very slow. However, it will be very fast if the angle is not too far out of range.

Parameters
theta[in, out] An angle in radians.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Normalize() [2/2]

template<class t >
t Sage::Normalize ( t v)

The DirectXTK doesn't have a function that returns a normalized vector, so here it is. We will now be able to use it in constant declarations, for example, const Vector2 u = v.Normalize().

Template Parameters
tVector type, typically Vector2 or Vector3.
Parameters
vA vector.
Returns
A unit vector that points in the direction of v.
Here is the call graph for this function:

◆ ParallelComponent()

Vector2 Sage::ParallelComponent ( const Vector2 & v0,
const Vector2 & v1 )

Find the component of one vector parallel to another.

Parameters
v0Vector whose component we want.
v1Vector that the component must be parallel to.
Returns
Component of v0 that is parallel to v1.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Rotate() [1/2]

const Vector2 Sage::Rotate ( const Vector2 & v,
const float a )

Rotate a vector about the Origin.

Parameters
vVector to be rotated.
aAngle to rotate by, counterclockwise, in radians.
Returns
The vector rotated by the angle.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Rotate() [2/2]

const Vector2 Sage::Rotate ( const Vector2 & u,
const Vector2 & v,
const float a )

Rotate a vector about a point.

Parameters
uVector to be rotated.
vCenter of rotation.
aAngle to rotate by, counterclockwise, in radians.
Returns
The vector rotated by the angle about the center of rotation.
Here is the call graph for this function:

◆ VectorNormalCC()

const Vector2 Sage::VectorNormalCC ( const Vector2 & v)

Compute the counterclockwise unit perpendicular to a vector. If \(\vec{v} = [v_x, v_y]\), then both dot products \(\vec{v} \cdot [-v_y, v_x]\) and \(\vec{v} \cdot [v_y, -v_x]\) are equal to zero, and therefore both \([-v_y, v_x]\) and \([v_y, -v_x]\) are perpendicular to \(\vec{v}\). The former (drawn in green below) points counterclockwise from \(\vec{v}\) and the latter (drawn in purple below) points clockwise from \(\vec{v}\). Therefore, this function computes Vector2(-v.y, v.x) and normalizes it before returning it.

Parameters
vA vector, not necessarily normalized.
Returns
The counterclockwise unit perpendicular to v.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ VectorToAngle()

const float Sage::VectorToAngle ( const Vector2 & v)

Compute the angle to a vector measured in radians counterclockwise from the positive X-axis. Suppose \(\theta\) is the angle between a vector \(\vec{v} = [v_x, v_y]\) and the positive X-axis (see the image below). Then \(\tan \theta = v_y/v_x\), that is, \(\theta = \arctan v_y/v_x\).

Notice that we use function atan2f(v.y, v.x) instead of atanf(v.y/v.x) to compute the arc tangent because atanf returns an angle in the first quadrant, that is, \([0, \pi/2]\). For example, atanf(-1/-1) is atanf(1), which returns \(\pi/4\), whereas atan2(-1, -1) returns the correct result, \(-3\pi/4\).

Parameters
vA vector.
Returns
The angle to that vector.
Here is the call graph for this function:
Here is the caller graph for this function: