Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void mousePressEvent(QMouseEvent *e)
- {
- Cluster* cluster;
- switch (e->button())
- {
- case Qt::LeftButton:
- //...
- cluster = new Cluster(e->x(), e->y(), QColor(rand() % 255, rand() % 255, rand() % 255));
- computeCluster(cluster);
- clusters.push_back(cluster);
- update(); //redesenarea ferestrei (se apeleaza paintEvent())
- break;
- case Qt::RightButton:
- for (Cluster* c : clusters)
- {
- if ((c->x - 5 <= e->x() && e->x() <= c->x + 5) && (c->y - 5 <= e->y() && e->y() <= c->y + 5))
- {
- for (KPoint* p : c->kpoints)
- {
- p->parentCluster = nullptr;
- }
- clusters.erase(std::remove_if(clusters.begin(), clusters.end(), [&c](Cluster* kc) { return kc == c;}), clusters.end());
- break;
- }
- }
- for (Cluster* c : clusters)
- {
- computeCluster(c);
- }
- update();
- break;
- }
- }
- void computeCluster(Cluster* c)
- {
- for (auto &p : kpoints)
- {
- // First cluster
- if (p->parentCluster == nullptr)
- {
- c->kpoints.push_back(p);
- p->parentCluster = c;
- }
- else
- {
- // Second and so on...
- if (p->get_distance(p->parentCluster) > p->get_distance(c))
- {
- // https://www.fluentcpp.com/2018/09/18/how-to-remove-pointers-from-a-vector-in-cpp/
- p->parentCluster->kpoints.erase(std::remove_if(p->parentCluster->kpoints.begin(), p->parentCluster->kpoints.end(), [&p](KPoint* kp) { return kp == p;}), p->parentCluster->kpoints.end());
- c->kpoints.push_back(p);
- p->parentCluster = c;
- }
- }
- }
- }
- void keyPressEvent(QKeyEvent *e)
- {
- switch (e->key())
- {
- case Qt::Key_Space:
- for (Cluster *c : clusters)
- {
- if (c->kpoints.size() == 0)
- continue;
- int x = 0;
- int y = 0;
- for (KPoint *p : c->kpoints)
- {
- x += p->x;
- y += p->y;
- }
- c->x = x / c->kpoints.size();
- c->y = y / c->kpoints.size();
- computeCluster(c);
- }
- update();
- break;
- case Qt::Key_Escape:
- this->close();
- };
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement