fart/shapes/Shape.cc
Josh Holtrop 7d64dd745d got Shape::IntersectionList::merge() to sort the result with IntersectionComparator successfully
git-svn-id: svn://anubis/fart/trunk@150 7f9b0f55-74a9-4bce-be96-3c2cd072584d
2009-02-24 00:38:09 +00:00

60 lines
1.5 KiB
C++
Executable File

#include "Shape.h"
#include <algorithm> /* sort() */
#include <functional> /* binary_function */
#include <utility>
using namespace std;
Shape::Shape()
{
m_transparency = 0.0;
m_material = new Material(Material::white);
}
Shape::~Shape()
{
}
class IntersectionComparator : public std::binary_function<Shape::Intersection,
Shape::Intersection,
bool>
{
public:
IntersectionComparator(const Vector & refPoint)
{
m_refPoint = refPoint;
}
bool operator()(const Shape::Intersection & i1,
const Shape::Intersection & i2) const
{
return (m_refPoint.dist_to(i1.second)
< m_refPoint.dist_to(i2.second));
}
protected:
Vector m_refPoint;
};
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]);
}
sort(result.begin(), result.end(), IntersectionComparator(startPoint));
return result;
}