Init
This commit is contained in:
32
src/Node.cpp
Executable file
32
src/Node.cpp
Executable file
@ -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;
|
||||
}
|
47
src/Node.h
Executable file
47
src/Node.h
Executable file
@ -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
|
50
src/Point.cpp
Executable file
50
src/Point.cpp
Executable file
@ -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;
|
||||
}
|
53
src/Point.h
Executable file
53
src/Point.h
Executable file
@ -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
|
50
src/Tour.cpp
Executable file
50
src/Tour.cpp
Executable file
@ -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
|
||||
}
|
29
src/Tour.h
Executable file
29
src/Tour.h
Executable file
@ -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
|
63
src/tsp.cpp
Executable file
63
src/tsp.cpp
Executable file
@ -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
|
||||
}
|
Reference in New Issue
Block a user