From 403ce5391e0fecc51bf50c86070ebc0e6d7e05af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Mon, 17 Aug 2020 19:34:09 +0200 Subject: add tsp code --- labb3/tsp/src/Point.cpp | 50 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 labb3/tsp/src/Point.cpp (limited to 'labb3/tsp/src/Point.cpp') 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 +#include +#include +#include +#include +#include +#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; +} -- cgit v1.2.1