diff options
| author | Gustav Sörnäs <gusso230@student.liu.se> | 2020-08-17 19:34:09 +0200 |
|---|---|---|
| committer | Gustav Sörnäs <gusso230@student.liu.se> | 2020-08-17 19:34:09 +0200 |
| commit | 403ce5391e0fecc51bf50c86070ebc0e6d7e05af (patch) | |
| tree | 248c47b8687baa820d4803784c15a2a6d43405c4 /labb3/tsp/src/Point.cpp | |
| parent | c0a7e9ae20f29bb5d344cbb3923e0967034cadf1 (diff) | |
| download | tddd86-L3.tar.gz | |
add tsp codeL3
Diffstat (limited to 'labb3/tsp/src/Point.cpp')
| -rw-r--r-- | labb3/tsp/src/Point.cpp | 50 |
1 files changed, 50 insertions, 0 deletions
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; +} |
