From c4ace382129ee43fffddf54ea1c01818176589bc Mon Sep 17 00:00:00 2001 From: falkTX Date: Sat, 10 May 2014 13:15:57 +0100 Subject: [PATCH] Add Line class --- dgl/Geometry.hpp | 46 +++++++++++ dgl/src/Geometry.cpp | 176 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 222 insertions(+) diff --git a/dgl/Geometry.hpp b/dgl/Geometry.hpp index 1597cf34..47aa93c8 100644 --- a/dgl/Geometry.hpp +++ b/dgl/Geometry.hpp @@ -23,6 +23,7 @@ START_NAMESPACE_DGL // ----------------------------------------------------------------------- +template class Line; template class Rectangle; // ----------------------------------------------------------------------- @@ -54,6 +55,7 @@ public: private: T fX, fY; + template friend class Line; template friend class Rectangle; }; @@ -93,6 +95,50 @@ private: // ----------------------------------------------------------------------- +template +class Line +{ +public: + Line() noexcept; + Line(const T& startX, const T& startY, const T& endX, const T& endY) noexcept; + Line(const T& startX, const T& startY, const Point& endPos) noexcept; + Line(const Point& startPos, const T& endX, const T& endY) noexcept; + Line(const Point& startPos, const Point& endPos) noexcept; + Line(const Line& line) noexcept; + + const T& getStartX() const noexcept; + const T& getStartY() const noexcept; + const T& getEndX() const noexcept; + const T& getEndY() const noexcept; + + const Point& getStartPos() const noexcept; + const Point& getEndPos() const noexcept; + + void setStartX(const T& x) noexcept; + void setStartY(const T& y) noexcept; + void setStartPos(const T& x, const T& y) noexcept; + void setStartPos(const Point& pos) noexcept; + + void setEndX(const T& x) noexcept; + void setEndY(const T& y) noexcept; + void setEndPos(const T& x, const T& y) noexcept; + void setEndPos(const Point& pos) noexcept; + + void moveBy(const T& x, const T& y) noexcept; + void moveBy(const Point& pos) noexcept; + + void draw(); + + Line& operator=(const Line& line) noexcept; + bool operator==(const Line& line) const noexcept; + bool operator!=(const Line& line) const noexcept; + +private: + Point fPosStart, fPosEnd; +}; + +// ----------------------------------------------------------------------- + template class Rectangle { diff --git a/dgl/src/Geometry.cpp b/dgl/src/Geometry.cpp index 03816fc2..29a66118 100644 --- a/dgl/src/Geometry.cpp +++ b/dgl/src/Geometry.cpp @@ -258,6 +258,177 @@ bool Size::operator!=(const Size& size) const noexcept return !operator==(size); } +// ----------------------------------------------------------------------- +// Line + +template +Line::Line() noexcept + : fPosStart(0, 0), + fPosEnd(0, 0) +{ +} + +template +Line::Line(const T& startX, const T& startY, const T& endX, const T& endY) noexcept + : fPosStart(startX, startY), + fPosEnd(endX, endY) +{ +} + +template +Line::Line(const T& startX, const T& startY, const Point& endPos) noexcept + : fPosStart(startX, startY), + fPosEnd(endPos) +{ +} + +template +Line::Line(const Point& startPos, const T& endX, const T& endY) noexcept + : fPosStart(startPos), + fPosEnd(endX, endY) +{ +} + +template +Line::Line(const Point& startPos, const Point& endPos) noexcept + : fPosStart(startPos), + fPosEnd(endPos) +{ +} + +template +Line::Line(const Line& line) noexcept + : fPosStart(line.fPosStart), + fPosEnd(line.fPosEnd) +{ +} + +template +const T& Line::getStartX() const noexcept +{ + return fPosStart.fX; +} + +template +const T& Line::getStartY() const noexcept +{ + return fPosStart.fY; +} + +template +const T& Line::getEndX() const noexcept +{ + return fPosEnd.fX; +} + +template +const T& Line::getEndY() const noexcept +{ + return fPosEnd.fY; +} + +template +const Point& Line::getStartPos() const noexcept +{ + return fPosStart; +} + +template +const Point& Line::getEndPos() const noexcept +{ + return fPosEnd; +} + +template +void Line::setStartX(const T& x) noexcept +{ + fPosStart.fX = x; +} + +template +void Line::setStartY(const T& y) noexcept +{ + fPosStart.fY = y; +} + +template +void Line::setStartPos(const T& x, const T& y) noexcept +{ + fPosStart = Point(x, y); +} + +template +void Line::setStartPos(const Point& pos) noexcept +{ + fPosStart = pos; +} + +template +void Line::setEndX(const T& x) noexcept +{ + fPosEnd.fX = x; +} + +template +void Line::setEndY(const T& y) noexcept +{ + fPosEnd.fY = y; +} + +template +void Line::setEndPos(const T& x, const T& y) noexcept +{ + fPosEnd = Point(x, y); +} + +template +void Line::setEndPos(const Point& pos) noexcept +{ + fPosEnd = pos; +} + +template +void Line::moveBy(const T& x, const T& y) noexcept +{ + fPosStart.fX += x; + fPosStart.fY += y; + fPosEnd.fX += x; + fPosEnd.fY += y; +} + +template +void Line::moveBy(const Point& pos) noexcept +{ + fPosStart += pos; + fPosEnd += pos; +} + +template +void Line::draw() +{ + // TODO +} + +template +Line& Line::operator=(const Line& line) noexcept +{ + fPosStart = line.fPosStart; + fPosEnd = line.fPosEnd; + return *this; +} + +template +bool Line::operator==(const Line& line) const noexcept +{ + return (fPosStart == line.fPosStart && fPosEnd == line.fPosEnd); +} + +template +bool Line::operator!=(const Line& line) const noexcept +{ + return !operator==(line); +} + // ----------------------------------------------------------------------- // Rectangle @@ -532,6 +703,11 @@ template class Size; template class Size; template class Size; +template class Line; +template class Line; +template class Line; +template class Line; + template class Rectangle; template class Rectangle; template class Rectangle;