summaryrefslogtreecommitdiffstats
path: root/labb3/tsp/src
diff options
context:
space:
mode:
authorGustav Sörnäs <gustav@sornas.net>2020-09-21 15:43:38 +0200
committerGustav Sörnäs <gustav@sornas.net>2020-09-21 15:43:38 +0200
commite483926bf15d6a560885c9b26d1c51a796583745 (patch)
tree64d6c5ea8956667e726462bc8262c84a8274c874 /labb3/tsp/src
parentd3e70eb68019f7d4e866dbda6ee8463ec5ad901b (diff)
downloadtddd86-e483926bf15d6a560885c9b26d1c51a796583745.tar.gz
Given files L3 tsp
Diffstat (limited to 'labb3/tsp/src')
-rw-r--r--labb3/tsp/src/Node.cpp32
-rw-r--r--labb3/tsp/src/Node.h47
-rw-r--r--labb3/tsp/src/Point.cpp50
-rw-r--r--labb3/tsp/src/Point.h53
-rw-r--r--labb3/tsp/src/Tour.cpp50
-rw-r--r--labb3/tsp/src/Tour.h29
-rw-r--r--labb3/tsp/src/tsp.cpp63
7 files changed, 324 insertions, 0 deletions
diff --git a/labb3/tsp/src/Node.cpp b/labb3/tsp/src/Node.cpp
new file mode 100644
index 0000000..3b7697b
--- /dev/null
+++ b/labb3/tsp/src/Node.cpp
@@ -0,0 +1,32 @@
+/*
+ * TDDD86 TSP
+ * This file contains the implementation of the Node structure.
+ * See Node.h for comments about each member.
+ * Your code should work properly with an unmodified version of this file.
+ */
+
+#include <iomanip>
+#include <iostream>
+#include <sstream>
+#include "Node.h"
+
+Node::Node(Point p, Node* _next)
+ : point(p.x, p.y), next(_next) {}
+
+string Node::toString() const {
+ stringstream out;
+ out << "Node{addr=" << ((void*) this);
+ out << ", point=" << point;
+ if (next != nullptr) {
+ out << ", next=" << ((void*) next);
+ } else {
+ out << ", next=nullptr";
+ }
+ out << "}";
+ return out.str();
+}
+
+ostream& operator <<(ostream& out, const Node& node) {
+ out << node.toString();
+ return out;
+}
diff --git a/labb3/tsp/src/Node.h b/labb3/tsp/src/Node.h
new file mode 100644
index 0000000..f9908e4
--- /dev/null
+++ b/labb3/tsp/src/Node.h
@@ -0,0 +1,47 @@
+/*
+ * TDDD86 TSP
+ * This file contains the declaration of the Node structure.
+ * See Node.cpp for implementation of each member.
+ * Your code should work properly with an unmodified version of this file.
+ */
+
+#ifndef NODE_H
+#define NODE_H
+
+#include <iostream>
+#include "Point.h"
+using namespace std;
+
+/*
+ * Each Node structure represents a single node in a circular linked list
+ * used in a TSP tour.
+ */
+struct Node {
+ Point point; // this nodes point
+ Node* next; // pointer to next node in the list (nullptr if none)
+
+ /*
+ * Constructs a new node storing the given point and next pointer.
+ */
+ Node(Point p, Node* _next = nullptr);
+
+ /*
+ * Returns a string representation of the Node for debugging, such as
+ * "Node{addr=0x7e83f4, point=(2.5, 3.5), next=0x43b2a0}".
+ *
+ * Note that the toString output includes the node's memory address, as well
+ * as the memory address where its next pointer points.
+ *
+ * Keep in mind that toString won't be called if you try to print a Node*.
+ * You must print the node itself.
+ */
+ string toString() const;
+};
+
+/*
+ * Outputs the given node to the given output stream (e.g. cout) in the same
+ * format as returned by its toString member function.
+ */
+ostream& operator <<(ostream& out, const Node& node);
+
+#endif // END NODE_H
diff --git a/labb3/tsp/src/Point.cpp b/labb3/tsp/src/Point.cpp
new file mode 100644
index 0000000..bb3a87d
--- /dev/null
+++ b/labb3/tsp/src/Point.cpp
@@ -0,0 +1,50 @@
+/*
+ * TDDD86 TSP
+ * This file contains the implementation of the Point structure.
+ * See Point.h for comments about each member.
+ * Your code should work properly with an unmodified version of this file.
+ */
+
+#include <iomanip>
+#include <iostream>
+#include <sstream>
+#include <cmath>
+#include <QGraphicsEllipseItem>
+#include <QGraphicsLineItem>
+#include "Point.h"
+
+Point::Point(double _x, double _y)
+ : x(_x), y(_y) {}
+
+double Point::distanceTo(Point that) const
+{
+ double dx = x - that.x;
+ double dy = y - that.y;
+ return std::sqrt(dx*dx + dy*dy);
+}
+
+void Point::draw(QGraphicsScene *scene) const
+{
+ QGraphicsEllipseItem *item = new QGraphicsEllipseItem(x, y, 1, 1);
+ item->setBrush(QBrush(QColor(255, 0, 0)));
+ scene->addItem(item);
+}
+
+void Point::drawTo(Point that, QGraphicsScene *scene) const
+{
+ QGraphicsLineItem *item = new QGraphicsLineItem(x, y, that.x, that.y);
+ scene->addItem(item);
+}
+
+string Point::toString() const
+{
+ stringstream string;
+ string << "(" << std::fixed << std::setprecision(1) << std::showpoint
+ << x << ", " << y << ")";
+ return string.str();
+}
+
+ostream& operator <<(ostream& out, const Point& p) {
+ out << p.toString();
+ return out;
+}
diff --git a/labb3/tsp/src/Point.h b/labb3/tsp/src/Point.h
new file mode 100644
index 0000000..e0d0f31
--- /dev/null
+++ b/labb3/tsp/src/Point.h
@@ -0,0 +1,53 @@
+/*
+ * TDDD86 TSP
+ * This file contains the declaration of the Point structure.
+ * See Point.cpp for implementation of each member.
+ * Your code should work properly with an unmodified version of this file.
+ */
+
+#ifndef POINT_H
+#define POINT_H
+
+#include <iostream>
+#include <string>
+#include <QGraphicsScene>
+using namespace std;
+
+/*
+ * Each Point structure represents a single point in the Euclidean plane.
+ */
+struct Point {
+ const double x; // x position of point
+ const double y; // y position of point
+
+ Point(double _x, double _y);
+
+ /*
+ * Returns Euclidean distance between this point and that.
+ */
+ double distanceTo(Point that) const;
+
+ /*
+ * Draws the point onto the given QGraphicsScene.
+ */
+ void draw(QGraphicsScene* scene) const;
+
+ /*
+ * Draws the line from this point to that onto the given QGraphicsScene.
+ */
+ void drawTo(Point that, QGraphicsScene* scene) const;
+
+ /*
+ * Returns a string representation of the point, such as
+ * (2.5, 3.5).
+ */
+ string toString() const;
+};
+
+/*
+ * Outputs the given point to the given output stream (e.g. cout) in the same
+ * format as returned by its toString member function.
+ */
+ostream& operator<<(ostream& out, const Point& p);
+
+#endif // POINT_H
diff --git a/labb3/tsp/src/Tour.cpp b/labb3/tsp/src/Tour.cpp
new file mode 100644
index 0000000..2dc3fcc
--- /dev/null
+++ b/labb3/tsp/src/Tour.cpp
@@ -0,0 +1,50 @@
+// This is the .cpp file you will edit and turn in.
+// We have provided a skeleton for you,
+// but you must finish it as described in the spec.
+// Also remove these comments here and add your own.
+// TODO: remove this comment header
+
+#include <iostream>
+#include "Tour.h"
+#include "Node.h"
+#include "Point.h"
+
+Tour::Tour()
+{
+ // TODO: write this member
+}
+
+Tour::~Tour()
+{
+ // TODO: write this member
+}
+
+void Tour::show()
+{
+ // TODO: write this member
+}
+
+void Tour::draw(QGraphicsScene *scene)
+{
+ // TODO: write this member
+}
+
+int Tour::size()
+{
+ // TODO: write this member
+}
+
+double Tour::distance()
+{
+ // TODO: write this member
+}
+
+void Tour::insertNearest(Point p)
+{
+ // TODO: write this member
+}
+
+void Tour::insertSmallest(Point p)
+{
+ // TODO: write this member
+}
diff --git a/labb3/tsp/src/Tour.h b/labb3/tsp/src/Tour.h
new file mode 100644
index 0000000..5262792
--- /dev/null
+++ b/labb3/tsp/src/Tour.h
@@ -0,0 +1,29 @@
+// This is the .h file you will edit and turn in.
+// We have provided a skeleton for you,
+// but you must finish it as described in the spec.
+// Also remove these comments here and add your own, as well as on the members.
+// TODO: remove this comment header
+
+#ifndef TOUR_H
+#define TOUR_H
+
+#include "Node.h"
+#include "Point.h"
+
+class Tour {
+public:
+
+ Tour();
+ ~Tour();
+ void show();
+ void draw(QGraphicsScene* scene);
+ int size();
+ double distance();
+ void insertNearest(Point p);
+ void insertSmallest(Point p);
+
+private:
+
+};
+
+#endif // TOUR_H
diff --git a/labb3/tsp/src/tsp.cpp b/labb3/tsp/src/tsp.cpp
new file mode 100644
index 0000000..566bf7c
--- /dev/null
+++ b/labb3/tsp/src/tsp.cpp
@@ -0,0 +1,63 @@
+/*
+ * TDDD86 TSP
+ * This client program uses your Tour class and contains the 'main'
+ * function to open the input file and set up the program's primitive GUI.
+ */
+
+#include <QApplication>
+#include <QGraphicsView>
+#include <QGraphicsScene>
+#include <chrono>
+#include <thread>
+#include <fstream>
+#include <iostream>
+#include <iomanip>
+#include "Point.h"
+#include "Tour.h"
+
+int main(int argc, char *argv[]) {
+ QApplication a(argc, argv);
+
+ string filename = "tsp10.txt";
+ ifstream input;
+ input.open(filename);
+
+ // get dimensions
+ int width;
+ int height;
+ input >> width;
+ input >> height;
+
+ // setup graphical window
+ QGraphicsView *view = new QGraphicsView();
+ QGraphicsScene *scene = new QGraphicsScene();
+ view->setScene(scene);
+ view->scale(1, -1); //screen y-axis is inverted
+ view->setSceneRect(0, 0, width, height);
+ view->show();
+
+ // run insertion heuristic
+ Tour tour;
+ double x;
+ double y;
+ while (input >> x >> y) {
+ Point p(x, y);
+ tour.insertNearest(p);
+ //uncomment the 4 lines below to animate
+ //tour.draw(scene);
+ //std::chrono::milliseconds dura(50);
+ //std::this_thread::sleep_for(dura);
+ //a.processEvents();
+ }
+ input.close();
+
+ // print tour to standard output
+ cout << "Tour distance: " << std::fixed << std::setprecision(4)
+ << std::showpoint << tour.distance() << endl;
+ cout << "Number of points: " << tour.size() << endl;
+ tour.show();
+
+ // draw tour
+ tour.draw(scene);
+ return a.exec(); // start Qt event loop
+}