summaryrefslogtreecommitdiffstats
path: root/labb3/tsp/src/Point.cpp
blob: bb3a87dc32a7d8f7218bf2e66fb35142f350e520 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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;
}