keira's code has the conditions connected the wrong way (it succeeds if the condition is true for any, when it needs to be true for all).
Here's one way:
Code:
for(int k = 0; k < V.size(); ++k) {
bool larger = true;
for(int inner = 0; larger && inner < W.size(); ++inner) {
larger = (angle(&V[k], &W[inner]) > theta);
}
if(larger) {
W.push_back(V[k]);
}
}
Note that using the checked at() for a trivial index loop is a waste of time.
Here's another way to write this:
Code:
#include <boost/lambda.hpp>
namespace ll = boost::lambda;
// Returns true if Predicate is true for all elements in the range [first,last).
template <typename InputIterator, typename Predicate>
bool all(InputIterator first, InputIterator last, Predicate p)
{
for( ; first != last; ++first) {
if(!p(*first)) {
return false;
}
}
return true;
}
for(std::vector<vectorType>::const_iterator it = V.begin(); it != V.end(); ++it) {
if(all(W.begin(), W.end(), ll::bind(&angle, &*it, &ll::_1) > theta)) {
W.push_back(*it);
}
}
Could be made even shorter by writing a copy_if and using that, but then it starts to get unreadable.