summaryrefslogtreecommitdiffstats
path: root/labb7
diff options
context:
space:
mode:
authorGustav Sörnäs <gustav@sornas.net>2020-12-08 10:21:25 +0100
committerGustav Sörnäs <gustav@sornas.net>2020-12-08 10:21:25 +0100
commitc0fab0e941aecc92037eb4e3eed7987afbe4b620 (patch)
treee7d85fd38a93fbfaad53ddb56c1af13ce91b3995 /labb7
parent0c39051ba80f04b1177833a006f2d442a7170b56 (diff)
downloadtddd86-c0fab0e941aecc92037eb4e3eed7987afbe4b620.tar.gz
impl l7
Diffstat (limited to 'labb7')
-rw-r--r--labb7/src/fast.cpp102
1 files changed, 102 insertions, 0 deletions
diff --git a/labb7/src/fast.cpp b/labb7/src/fast.cpp
new file mode 100644
index 0000000..ab7c7b5
--- /dev/null
+++ b/labb7/src/fast.cpp
@@ -0,0 +1,102 @@
+#include <QApplication>
+#include <QGraphicsView>
+#include <QGraphicsScene>
+#include <fstream>
+#include <iostream>
+#include <algorithm>
+#include <vector>
+#include <chrono>
+#include <cmath>
+#include <unordered_map>
+#include "Point.h"
+
+// constants
+static const int SCENE_WIDTH = 512;
+static const int SCENE_HEIGHT = 512;
+
+void render_points(QGraphicsScene* scene, const vector<Point>& points) {
+ for(const auto& point : points) {
+ point.draw(scene);
+ }
+}
+
+void render_line(QGraphicsScene* scene, const Point& p1, const Point& p2) {
+ p1.lineTo(scene, p2);
+}
+
+int main(int argc, char *argv[]) {
+ QApplication a(argc, argv);
+
+ // open file
+ string filename = "grid4x4.txt";
+ ifstream input;
+ input.open(filename);
+
+ // the vector of points
+ vector<Point> points;
+
+ // read points from file
+ int N;
+ int x;
+ int y;
+
+ input >> N;
+
+ for (int i = 0; i < N; ++i) {
+ input >> x >> y;
+ points.push_back(Point(x, y));
+ }
+ input.close();
+
+ // setup graphical window
+ QGraphicsView *view = new QGraphicsView();
+ QGraphicsScene *scene = new QGraphicsScene(0, 0, SCENE_WIDTH, SCENE_HEIGHT);
+ view->setScene(scene);
+ // draw points to screen all at once
+ render_points(scene, points);
+ view->scale(1, -1); //screen y-axis is inverted
+ view->resize(view->sizeHint());
+ view->setWindowTitle("Fast Pattern Recognition");
+ view->show();
+
+ sort(points.begin(), points.end());
+ auto begin = chrono::high_resolution_clock::now();
+
+ vector<pair<double, Point>> angles;
+ for (int p1 = 0; p1 < N-1; p1++) {
+ angles.clear();
+ for (int p2 = p1 + 1; p2 < N; p2++) {
+ double angle = points.at(p1).slopeTo(points.at(p2));
+ angles.push_back({ angle, points.at(p2) });
+ }
+ sort(angles.begin(), angles.end());
+
+ cout << points.at(p1) << endl;
+
+ double prev_angle = angles[0].first;
+ int num_in_row = 0;
+ for (int i = 1; i < angles.size(); i++) {
+ if (angles[i].first == prev_angle) {
+ num_in_row++;
+ if (num_in_row >= 2) {
+ render_line(scene, points.at(p1), angles[i].second);
+ }
+ } else {
+ num_in_row = 0;
+ }
+ cout << i << " " << num_in_row << " " << angles[i].first << " " << angles[i].second << endl;
+ prev_angle = angles[i].first;
+ }
+ if (num_in_row >= 2) {
+ render_line(scene, points.at(p1), angles[angles.size() - 1].second);
+ }
+ cout << endl;
+ }
+
+ auto end = chrono::high_resolution_clock::now();
+ cout << "Computing line segments took "
+ << std::chrono::duration_cast<chrono::milliseconds>(end - begin).count()
+ << " milliseconds." << endl;
+
+ return a.exec(); // start Qt event loop
+}