From 7b7f6808a7b2db2ed21103767434c1445f7815c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Thu, 3 Dec 2020 16:27:06 +0100 Subject: add given files l7 --- labb7/src/Point.cpp | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 labb7/src/Point.cpp (limited to 'labb7/src/Point.cpp') diff --git a/labb7/src/Point.cpp b/labb7/src/Point.cpp new file mode 100644 index 0000000..be42b8b --- /dev/null +++ b/labb7/src/Point.cpp @@ -0,0 +1,61 @@ +/* + * TDDD86 Pattern Recognition + * This file contains the implementation of the Point class. + * 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 +#include "Point.h" + +double Point::slopeTo(const Point& p) const { + if (x == p.x && y == p.y) + return -std::numeric_limits::infinity(); + else if (y == p.y) // horizontal line segment + return 0.0; + else if (x == p.x) // vertical line segment + return std::numeric_limits::infinity(); + else + return (static_cast(p.y) - static_cast(y)) / + (static_cast(p.x) - static_cast(x)); +} + +void Point::draw(QGraphicsScene *scene) const { + QGraphicsEllipseItem *item = new QGraphicsEllipseItem(x / (COORD_MAX + 1.0) * scene->width(), + y / (COORD_MAX + 1.0) * scene->height(), + 1, 1); + item->setBrush(QBrush(QColor(255, 0, 0))); + scene->addItem(item); +} + +void Point::lineTo(QGraphicsScene *scene, const Point& p) const { + QGraphicsLineItem *item = new QGraphicsLineItem(x / (COORD_MAX + 1.0) * scene->width(), + y / (COORD_MAX + 1.0) * scene->height(), + p.x / (COORD_MAX + 1.0) * scene->width(), + p.y / (COORD_MAX + 1.0) * scene->height()); + item->setPen(QPen(QColor(0, 0, 255), 0)); + scene->addItem(item); +} + +bool Point::operator<(const Point& other) const { + if (x == other.x) + return y < other.y; + else + return x < other.x; +} + +bool Point::operator>(const Point& other) const +{ + return other < *this; +} + +ostream& operator<<(std::ostream& out, const Point& p) { + out << "(" << p.x << ", " << p.y << ")"; + return out; +} -- cgit v1.2.1