From 25d6432dbd42ef2456709ea572567b14b6f38a1d Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Mon, 23 Feb 2009 22:26:46 +0000 Subject: [PATCH] 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 --- shapes/Shape.cc | 39 +++++++++++++++++++++++++++++++++++++++ shapes/Shape.h | 4 ++-- util/Vector.cc | 5 +++++ util/Vector.h | 1 + 4 files changed, 47 insertions(+), 2 deletions(-) diff --git a/shapes/Shape.cc b/shapes/Shape.cc index 2fef45b..e46e478 100755 --- a/shapes/Shape.cc +++ b/shapes/Shape.cc @@ -1,5 +1,8 @@ #include "Shape.h" +#include /* sort() */ +#include /* binary_function */ +using namespace std; Shape::Shape() { @@ -15,4 +18,40 @@ 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 + { + 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; } diff --git a/shapes/Shape.h b/shapes/Shape.h index 9157b97..7116d7f 100644 --- a/shapes/Shape.h +++ b/shapes/Shape.h @@ -27,11 +27,11 @@ class Shape return m_intersections[i]; } size_t size() { return m_intersections.size(); } - std::vector::const_iterator begin() + std::vector::iterator begin() { return m_intersections.begin(); } - std::vector::const_iterator end() + std::vector::iterator end() { return m_intersections.end(); } diff --git a/util/Vector.cc b/util/Vector.cc index d970dc0..d2e0944 100644 --- a/util/Vector.cc +++ b/util/Vector.cc @@ -49,6 +49,11 @@ double Vector::mag2() const + 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 target_normalized = target; diff --git a/util/Vector.h b/util/Vector.h index cd14835..283f7f0 100644 --- a/util/Vector.h +++ b/util/Vector.h @@ -15,6 +15,7 @@ class Vector Vector & normalize(); double mag() const; double mag2() const; + double dist_to(const Vector & other) const; Vector proj(const Vector & target) const; Vector reflect(const Vector & target) const;