fart/shapes/Shape.cc
Josh Holtrop 25d6432dbd trying to get a custom comparator to sort my IntersectionList after a merge()
git-svn-id: svn://anubis/fart/trunk@149 7f9b0f55-74a9-4bce-be96-3c2cd072584d
2009-02-23 22:26:46 +00:00

58 lines
1.4 KiB
C++
Executable File

#include "Shape.h"
#include <algorithm> /* sort() */
#include <functional> /* binary_function */
using namespace std;
Shape::Shape()
{
m_transparency = 0.0;
m_material = new Material(Material::white);
}
Shape::~Shape()
{
}
Shape::IntersectionList
Shape::IntersectionList::merge(const IntersectionList & other,
const Vector & startPoint)
{
Shape::IntersectionList result;
for (size_t i = 0, sz = m_intersections.size();
i < sz;
i++)
{
result.add(m_intersections[i]);
}
for (size_t i = 0, sz = other.m_intersections.size();
i < sz;
i++)
{
result.add(other.m_intersections[i]);
}
class Comparator : public binary_function<Intersection,
Intersection,
bool>
{
public:
Comparator(const Vector & refPoint)
{
m_refPoint = refPoint;
}
bool operator()(Intersection const & i1, Intersection const & i2) const
{
return (m_refPoint.dist_to(i1.second)
< m_refPoint.dist_to(i2.second));
}
protected:
Vector m_refPoint;
};
sort(result.m_intersections.begin(), result.m_intersections.end(), Comparator(startPoint));
return result;
}