Skip to content

Instantly share code, notes, and snippets.

@sagi-z
Last active July 8, 2017 20:59
Show Gist options
  • Save sagi-z/0339f3251942761607401479717b84e4 to your computer and use it in GitHub Desktop.
Save sagi-z/0339f3251942761607401479717b84e4 to your computer and use it in GitHub Desktop.
class CountingManager
{
public:
CountingManager(Canvas &c)
{
c.notifyOnShapeCreate([&c, this](Shape* shape)
{ // User created a shape
if (shape->getType() == LineCrossing::type)
{ // Add the LineCrossing to out list
LineCrossing *pLineCrossing = (LineCrossing*) shape;
lineCrossings.push_back(make_pair(pLineCrossing, 0));
}
});
c.notifyOnShapeDelete([&c, this](Shape* shape)
{ // User deleted a shape
if (shape->getType() == LineCrossing::type)
{ // remove this LineCrossing
auto i = find_if(lineCrossings.begin(),
lineCrossings.end(),
[shape](const CountingLine &item)
{
return item.first == shape;
});
if (i != lineCrossings.end()) lineCrossings.erase(i);
}
});
}
// count people crossing with/against lines and return true for these cases
bool evaluatePerson(const SimulatedPerson &person)
{
for (CountingLine &lineCrossing : lineCrossings)
{
int res = lineCrossing.first->isCrossedBySegment(person.getFromPos(),
person.getCurrentPos());
lineCrossing.second += res;
lineCrossing.first->setName(CCV_STR("Line was crossed: " << lineCrossing.second << " times"));
if (res) return true;
}
return false;
}
private:
typedef pair<LineCrossing*,int> CountingLine;
list<CountingLine> lineCrossings;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment