Advertisement
Guest User

Untitled

a guest
Feb 18th, 2020
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.01 KB | None | 0 0
  1. void mousePressEvent(QMouseEvent *e)
  2. {
  3. Cluster* cluster;
  4. switch (e->button())
  5. {
  6. case Qt::LeftButton:
  7.  
  8. //...
  9. cluster = new Cluster(e->x(), e->y(), QColor(rand() % 255, rand() % 255, rand() % 255));
  10. computeCluster(cluster);
  11. clusters.push_back(cluster);
  12.  
  13. update(); //redesenarea ferestrei (se apeleaza paintEvent())
  14. break;
  15. case Qt::RightButton:
  16.  
  17. for (Cluster* c : clusters)
  18. {
  19. if ((c->x - 5 <= e->x() && e->x() <= c->x + 5) && (c->y - 5 <= e->y() && e->y() <= c->y + 5))
  20. {
  21. for (KPoint* p : c->kpoints)
  22. {
  23. p->parentCluster = nullptr;
  24. }
  25.  
  26. clusters.erase(std::remove_if(clusters.begin(), clusters.end(), [&c](Cluster* kc) { return kc == c;}), clusters.end());
  27. break;
  28. }
  29. }
  30.  
  31. for (Cluster* c : clusters)
  32. {
  33. computeCluster(c);
  34. }
  35.  
  36.  
  37. update();
  38. break;
  39. }
  40. }
  41.  
  42. void computeCluster(Cluster* c)
  43. {
  44.  
  45. for (auto &p : kpoints)
  46. {
  47. // First cluster
  48. if (p->parentCluster == nullptr)
  49. {
  50. c->kpoints.push_back(p);
  51. p->parentCluster = c;
  52. }
  53. else
  54. {
  55. // Second and so on...
  56. if (p->get_distance(p->parentCluster) > p->get_distance(c))
  57. {
  58. // https://www.fluentcpp.com/2018/09/18/how-to-remove-pointers-from-a-vector-in-cpp/
  59. 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());
  60. c->kpoints.push_back(p);
  61. p->parentCluster = c;
  62. }
  63. }
  64.  
  65. }
  66. }
  67.  
  68. void keyPressEvent(QKeyEvent *e)
  69. {
  70. switch (e->key())
  71. {
  72.  
  73. case Qt::Key_Space:
  74.  
  75. for (Cluster *c : clusters)
  76. {
  77. if (c->kpoints.size() == 0)
  78. continue;
  79.  
  80. int x = 0;
  81. int y = 0;
  82. for (KPoint *p : c->kpoints)
  83. {
  84. x += p->x;
  85. y += p->y;
  86. }
  87.  
  88. c->x = x / c->kpoints.size();
  89. c->y = y / c->kpoints.size();
  90.  
  91. computeCluster(c);
  92. }
  93.  
  94. update();
  95. break;
  96.  
  97. case Qt::Key_Escape:
  98. this->close();
  99. };
  100. }
  101.  
  102. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement