Skip to content

Instantly share code, notes, and snippets.

@ErisianArchitect
Created March 5, 2012 00:12
Show Gist options
  • Save ErisianArchitect/1975587 to your computer and use it in GitHub Desktop.
Save ErisianArchitect/1975587 to your computer and use it in GitHub Desktop.
Separating Axis
void getMin(double* values,int c, double & result)
{
//If count is greater than 0, find the min.
if(c > 0)
{
//Set result to the first value.
result = values[0];
//Begin looping through each value
for(int i = 1; i < c; i++)
{
//If value at index is less than result, set result
if(values[i] < result)
result = values[i];
}
}
}
void getMax(double* values, int c, double & result)
{
//If count is greater than 0, find the max.
if(c > 0)
{
//Set result to the first value.
result = values[0];
//Begin looping through each value
for(int i = 1; i < c; i++)
{
//If value at index is greater than result, set result
if(values[i] > result)
result = values[i];
}
}
}
//We take some vertices of a 3-dimensional object (verts), and project them into 1-dimensional space by projecting them onto an axis,
//and take out the minimum and maximum values.
void ProjectOntoAxis(const vector3* verts, const int vcount, const vector3 & axis, double & min, double & max)
{
//Create a new array of values to store the lengths with the same count as the vertices.
double* dubs = new double[vcount];
//Loop through each vertex.
for(int i = 0; i < vcount; i++)
{
//Calculate the dot product of the axis (a normalized 3D vector), and the vertex at the index and store it in our array.
dubs[i] = vector3::Dot(axis,verts[i]);
}
//Get the minimum length, put it into "min"
getMin(dubs,vcount,min);
//Get the maximum length, put it into "max"
getMax(dubs,vcount,max);
//Release the array of lengths.
delete [] dubs;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment