Simple simplify() function
authorunc0rr
Mon, 29 Nov 2010 22:23:56 +0300
changeset 4434 34c305fbc63c
parent 4433 e0092290b7c5
child 4435 996ef18fa396
Simple simplify() function
tools/drawMapTest/drawmapscene.cpp
tools/drawMapTest/drawmapscene.h
tools/drawMapTest/mainwindow.cpp
tools/drawMapTest/mainwindow.h
tools/drawMapTest/mainwindow.ui
--- a/tools/drawMapTest/drawmapscene.cpp	Sun Nov 28 15:07:26 2010 -0500
+++ b/tools/drawMapTest/drawmapscene.cpp	Mon Nov 29 22:23:56 2010 +0300
@@ -5,6 +5,11 @@
 
 #include "drawmapscene.h"
 
+template <class T> T sqr(const T & x)
+{
+    return x*x;
+}
+
 DrawMapScene::DrawMapScene(QObject *parent) :
     QGraphicsScene(parent),
     m_pen(Qt::yellow),
@@ -98,3 +103,25 @@
 
     return b;
 }
+
+void DrawMapScene::simplify()
+{
+    for(int pit = 0; pit < paths.size(); ++pit)
+    {
+        QList<QPoint> points = paths[pit];
+
+        QPoint prevPoint = points.first();
+        int i = 1;
+        while(i < points.size())
+        {
+            if(sqr(prevPoint.x() - points[i].x()) + sqr(prevPoint.y() - points[i].y()) < 1000)
+                points.removeAt(i);
+            else
+                ++i;
+        }
+
+        paths[pit] = points;
+    }
+
+    emit pathChanged();
+}
--- a/tools/drawMapTest/drawmapscene.h	Sun Nov 28 15:07:26 2010 -0500
+++ b/tools/drawMapTest/drawmapscene.h	Mon Nov 29 22:23:56 2010 +0300
@@ -20,6 +20,7 @@
 
 public slots:
     void undo();
+    void simplify();
 
 private:
     QPen m_pen;
--- a/tools/drawMapTest/mainwindow.cpp	Sun Nov 28 15:07:26 2010 -0500
+++ b/tools/drawMapTest/mainwindow.cpp	Mon Nov 29 22:23:56 2010 +0300
@@ -44,3 +44,8 @@
 {
     ui->plainTextEdit->setPlainText(scene->encode().toBase64());
 }
+
+void MainWindow::on_pbSimplify_clicked()
+{
+    scene->simplify();
+}
--- a/tools/drawMapTest/mainwindow.h	Sun Nov 28 15:07:26 2010 -0500
+++ b/tools/drawMapTest/mainwindow.h	Mon Nov 29 22:23:56 2010 +0300
@@ -25,6 +25,7 @@
     virtual void resizeEvent(QResizeEvent * event);
 
 private slots:
+    void on_pbSimplify_clicked();
     void scene_pathChanged();
 };
 
--- a/tools/drawMapTest/mainwindow.ui	Sun Nov 28 15:07:26 2010 -0500
+++ b/tools/drawMapTest/mainwindow.ui	Mon Nov 29 22:23:56 2010 +0300
@@ -50,6 +50,13 @@
         </property>
        </spacer>
       </item>
+      <item>
+       <widget class="QPushButton" name="pbSimplify">
+        <property name="text">
+         <string>Simplify</string>
+        </property>
+       </widget>
+      </item>
      </layout>
     </item>
    </layout>