From 8fb4b0d56a83711cb47d159a9b2ceb99c5c8e782 Mon Sep 17 00:00:00 2001 From: Sverre Rabbelier Date: Mon, 14 Apr 2008 01:42:46 +0200 Subject: [PATCH] Added extra functionality to Coordinate. --- src/Resource/Coordinate.cpp | 62 +++++++++++++++++++++++++++++++++++ src/Resource/Coordinate.h | 79 ++++++++++++++++++++++++++++++--------------- 2 files changed, 115 insertions(+), 26 deletions(-) diff --git a/src/Resource/Coordinate.cpp b/src/Resource/Coordinate.cpp index 1f20322..4c9d9ae 100644 --- a/src/Resource/Coordinate.cpp +++ b/src/Resource/Coordinate.cpp @@ -19,6 +19,7 @@ ***************************************************************************/ #include "Coordinate.h" +#include "StringUtilities.h" Coordinate Coordinate::NORTH (0, 1, 0); Coordinate Coordinate::NUP (0, 1, 1); @@ -68,6 +69,14 @@ Coordinate::~Coordinate() } +Coordinate::Coordinate(const Coordinate& rhs) : +m_xcoordinate(rhs.getXCoordinate()), +m_ycoordinate(rhs.getYCoordinate()), +m_zcoordinate(rhs.getZCoordinate()) +{ + +} + Coordinate Coordinate::operator+= (const Coordinate & rhs) { m_xcoordinate = getXCoordinate()+rhs.getXCoordinate(); @@ -114,3 +123,56 @@ long Coordinate::getZCoordinate() const { return m_zcoordinate; } + +bool Coordinate::isDirection() const +{ + if(abs(m_xcoordinate) > 1) return false; + if(abs(m_ycoordinate) > 1) return false; + if(abs(m_zcoordinate) > 1) return false; + + return true; +} +std::string Coordinate::toString() const +{ + std::string result; + + result.append("x: "); + result.append(String::Get()->fromInt(getXCoordinate())); + + result.append(", y: "); + result.append(String::Get()->fromInt(getYCoordinate())); + + + result.append(", z: "); + result.append(String::Get()->fromInt(getZCoordinate())); + + return result; +} + +std::string Coordinate::toDirectionString() const +{ + Assert(isDirection()); + + std::string result; + + int x = getXCoordinate(); + int y = getYCoordinate(); + int z = getZCoordinate(); + + if(x > 0) + result.append("north"); + if(x < 0) + result.append("south"); + + if(y > 0) + result.append("east"); + if(y < 0) + result.append("west"); + + if(z > 0) + result.append("up"); + if(z < 0) + result.append("down"); + + return result; +} diff --git a/src/Resource/Coordinate.h b/src/Resource/Coordinate.h index 6bcbc43..b3eb17b 100644 --- a/src/Resource/Coordinate.h +++ b/src/Resource/Coordinate.h @@ -27,6 +27,8 @@ * @see Coordinate */ +#include "Types.h" + /** * This file is an 'enumeration' class with all the directions. */ @@ -69,73 +71,96 @@ public: static Coordinate DOWN; /**< The 'down' coordinate. */ /** - * \brief Specialized constructor - * \param X initial X coordinate - * \param Y initial Y coordinate - * \param Z initial Z coordinate + * Specialized constructor + * + * @param X initial X coordinate + * @param Y initial Y coordinate + * @param Z initial Z coordinate */ Coordinate(long X, long Y, long Z); /** - * \brief Copy Constructor - * \param rhs Copy from + * Copy Constructor + * + * @param rhs Copy from */ Coordinate(const Coordinate& rhs); /** - * \brief Default destructor + * Default destructor */ ~Coordinate(); /** - * \brief Overloaded addition operator - * \param rhs Coordinate to be added - * \return this+rhs + * Overloaded addition operator + * + * @param rhs Coordinate to be added + * @return this+rhs */ Coordinate operator+= (const Coordinate& rhs); /** - * \brief Overloaded addition operator - * \param rhs Coordinate to be added - * \return this+rhs + * Overloaded addition operator + * + * @param rhs Coordinate to be added + * @return this+rhs */ Coordinate operator+ (const Coordinate & rhs) const; /** - * \brief Overloaded substraction operator - * \param rhs Coordinate to be substracted - * \return this-rhs + * Overloaded substraction operator + * + * @param rhs Coordinate to be substracted + * @return this-rhs */ Coordinate operator-= (const Coordinate & rhs); /** - * \brief Overloaded substraction operator - * \param rhs Coordinate to be substracted - * \return this-rhs + * Overloaded substraction operator + * + * @param rhs Coordinate to be substracted + * @return this-rhs */ Coordinate operator- (const Coordinate & rhs) const; /** - * \brief Getter - * \return X coordinate + * Getter + * + * @return X coordinate */ long getXCoordinate() const; /** - * \brief Getter - * \return Y Coordinate + * Getter + * + * @return Y Coordinate */ long getYCoordinate() const; /** - * \brief Getter - * \return Z coordinate + * Getter + * + * @return Z coordinate */ long getZCoordinate() const; + + + /** + * Returns whether this coordinate represents a direction (abs(x,y,z) <= 1). + * + * @return Whether this coordinate satisfies "-1 <= i <= 1" for i={x,y,z}. + */ + bool isDirection() const; + + /** Returns a string representation of this coordinate, */ + std::string toString() const; + + /** Returns the direction name of this coordinate if it is one. */ + std::string toDirectionString() const; private: /** - * \brief Hide default constructor + * Hide default constructor */ Coordinate() {}; @@ -143,3 +168,5 @@ private: long m_ycoordinate; long m_zcoordinate; }; + +typedef std::vector Coordinates; /**< The type of multiple Coordinates. */ -- 2.11.4.GIT