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
This commit is contained in:
parent
56278a8337
commit
25d6432dbd
@ -1,5 +1,8 @@
|
|||||||
|
|
||||||
#include "Shape.h"
|
#include "Shape.h"
|
||||||
|
#include <algorithm> /* sort() */
|
||||||
|
#include <functional> /* binary_function */
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
Shape::Shape()
|
Shape::Shape()
|
||||||
{
|
{
|
||||||
@ -15,4 +18,40 @@ Shape::IntersectionList
|
|||||||
Shape::IntersectionList::merge(const IntersectionList & other,
|
Shape::IntersectionList::merge(const IntersectionList & other,
|
||||||
const Vector & startPoint)
|
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;
|
||||||
}
|
}
|
||||||
|
@ -27,11 +27,11 @@ class Shape
|
|||||||
return m_intersections[i];
|
return m_intersections[i];
|
||||||
}
|
}
|
||||||
size_t size() { return m_intersections.size(); }
|
size_t size() { return m_intersections.size(); }
|
||||||
std::vector<Intersection>::const_iterator begin()
|
std::vector<Intersection>::iterator begin()
|
||||||
{
|
{
|
||||||
return m_intersections.begin();
|
return m_intersections.begin();
|
||||||
}
|
}
|
||||||
std::vector<Intersection>::const_iterator end()
|
std::vector<Intersection>::iterator end()
|
||||||
{
|
{
|
||||||
return m_intersections.end();
|
return m_intersections.end();
|
||||||
}
|
}
|
||||||
|
@ -49,6 +49,11 @@ double Vector::mag2() const
|
|||||||
+ m_array[2] * m_array[2];
|
+ m_array[2] * m_array[2];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
double Vector::dist_to(const Vector & other) const
|
||||||
|
{
|
||||||
|
return (other - *this).mag();
|
||||||
|
}
|
||||||
|
|
||||||
Vector Vector::proj(const Vector & target) const
|
Vector Vector::proj(const Vector & target) const
|
||||||
{
|
{
|
||||||
Vector target_normalized = target;
|
Vector target_normalized = target;
|
||||||
|
@ -15,6 +15,7 @@ class Vector
|
|||||||
Vector & normalize();
|
Vector & normalize();
|
||||||
double mag() const;
|
double mag() const;
|
||||||
double mag2() const;
|
double mag2() const;
|
||||||
|
double dist_to(const Vector & other) const;
|
||||||
Vector proj(const Vector & target) const;
|
Vector proj(const Vector & target) const;
|
||||||
Vector reflect(const Vector & target) const;
|
Vector reflect(const Vector & target) const;
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user