From 74bb6f30f8c2149c4cffc49218e060a42b722f0c Mon Sep 17 00:00:00 2001
From: IgorekLoschinin <igor.loschinin2014@yandex.ru>
Date: Thu, 9 Sep 2021 22:37:05 +0300
Subject: [PATCH] ref #97 Added method draw square.

---
 src/Core/Crawl/controlpos.cpp | 33 +++++++++++++++++++++++++--------
 1 file changed, 25 insertions(+), 8 deletions(-)

diff --git a/src/Core/Crawl/controlpos.cpp b/src/Core/Crawl/controlpos.cpp
index 86a38ec..be6ed0d 100644
--- a/src/Core/Crawl/controlpos.cpp
+++ b/src/Core/Crawl/controlpos.cpp
@@ -59,8 +59,13 @@ void ControlPos::updatePosition() {
 
 void ControlPos::drawCircle() {
 
+    if (objects().size() == 0) {
+        QuasarAppUtils::Params::log(QString("The number of objects is zero. Add object."));
+        return;
+    }
+
     float step =  360 / objects().size();
-    int temp = 1;
+    int temp = 0;
 
     for (ClasterItem* object: objects()) {
 
@@ -75,16 +80,28 @@ void ControlPos::drawCircle() {
 
 void ControlPos::drawSquare() {
 
-    float step =  360 / objects().size();
-    int temp = 1;
+    if (objects().size() == 0) {
+        QuasarAppUtils::Params::log(QString("The number of objects is zero. Add object."));
+        return;
+    }
 
-    for (ClasterItem* object: objects()) {
+    int height = qFloor(qSqrt(objects().size()));
+    int width = qCeil(qSqrt(objects().size()));
 
-        float x = _distance * qCos(step*temp);
-        float y = _distance * qSin(step*temp);
-        GroupObject::updatePosition(object->guiId(), {x, y, 0});
+    int coutObj = 0;
+    for (int y = 0; y < height; y++) {
 
-        temp++;
+        coutObj = coutObj+y;
+        for (int x = 0; x < width; x++) {
+
+            if (coutObj == objects().size()) {
+                return;
+            }
+            GroupObject::updatePosition(objects()[coutObj]->guiId(), {float(x + _distance),
+                                                                      float(y + _distance),
+                                                                      0});
+            coutObj++;
+        }
     }
 
 }