Skip to content

Commit

Permalink
Create Coordinate2D type
Browse files Browse the repository at this point in the history
  • Loading branch information
dxrcy committed Oct 31, 2024
1 parent bc94f6b commit 6c67d4a
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 0 deletions.
108 changes: 108 additions & 0 deletions include/mcpp/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
*
*/
namespace mcpp {

struct Coordinate2D;

/**
* Represented using integers since sub-unit coordinates are not of particular
* relevance. Allows for operations such as addition between coordinates.
Expand All @@ -34,6 +37,15 @@ struct Coordinate {
*/
Coordinate(double x, double y, double z);

/**
* @brief Constructs a Coordinate object from a Coordinate2D object and a
* y value.
*
* @param coord The Coordinate2D object.
* @param y The y value.
*/
constexpr Coordinate(const Coordinate2D& coord, int y);

/**
* @brief Adds two Coordinate objects.
*
Expand Down Expand Up @@ -89,6 +101,102 @@ struct Coordinate {
int z;
};

/**
* @brief Height-agnostic coordinate class.
*
* Represented using integers since sub-unit coordinates are not of particular
* relevance. Allows for operations such as addition between flat coordinates.
*/
struct Coordinate2D {
/**
* @brief Constructs a Coordinate2D object with integer values.
*
* @param x The x-coordinate.
* @param z The z-coordinate.
*/
constexpr Coordinate2D(int x, int z) : x(x), z(z) {}

/**
* @brief Constructs a Coordinate2D object with zero values.
*/
constexpr Coordinate2D() : x(0), z(0) {}

/**
* @brief Constructs a Coordinate2D object with double values.
*
* @param x The x-coordinate as a double.
* @param z The z-coordinate as a double.
*/
constexpr Coordinate2D(double x, double z)
: x(static_cast<int>(x)), z(static_cast<int>(z)) {}

/**
* @brief Constructs a Coordinate2D object from a Coordinate object.
*
* @param coord The Coordinate object.
*/
constexpr Coordinate2D(const Coordinate& coord) : x(coord.x), z(coord.z) {}

/**
* @brief Adds two Coordinate2D objects.
*
* @param obj The Coordinate2D object to add.
* @return A new Coordinate2D object representing the sum of the two
* coordinates.
*/
Coordinate2D operator+(const Coordinate2D& obj) const;

// TODO: Add Coordinate + Coordinate2D

/**
* @brief Checks if two Coordinate2D objects are equal.
*
* @param obj The Coordinate2D object to compare with.
* @return True if the flat coordinates are equal, false otherwise.
*/
bool operator==(const Coordinate2D& obj) const;

/**
* @brief Checks if two Coordinate2D objects are not equal.
*
* @param obj The Coordinate2D object to compare with.
* @return True if the flat coordinates are not equal, false otherwise.
*/
bool operator!=(const Coordinate2D& obj) const;

/**
* @brief Subtracts one Coordinate2D object from another.
*
* @param obj The Coordinate2D object to subtract.
* @return A new Coordinate2D object representing the difference between
* the two coordinates.
*/
Coordinate2D operator-(const Coordinate2D& obj) const;

/**
* @brief Creates a copy of the Coordinate2D object.
*
* @return A new Coordinate2D object that is a copy of the current object.
*/
[[nodiscard]] Coordinate2D clone() const;

/**
* @brief Outputs the Coordinate2D object to an ostream.
*
* @param out The output stream.
* @param coord The Coordinate2D object to output.
* @return The output stream with the Coordinate object's values.
*/
friend std::ostream& operator<<(std::ostream& out,
const Coordinate2D& coord);

int x;
int z;
};

constexpr Coordinate::Coordinate(const Coordinate2D& coord, int y)
: x(coord.x), y(y), z(coord.z) {}

/**
* Stores a 3D cuboid of BlockTypes while preserving their relative location to
* the base point they were gathered at and each other.
Expand Down
36 changes: 36 additions & 0 deletions src/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,42 @@ std::ostream& operator<<(std::ostream& out, const Coordinate& coord) {
return out;
}

Coordinate2D Coordinate2D::operator+(const Coordinate2D& obj) const {
Coordinate2D result;
result.x = this->x + obj.x;
result.z = this->z + obj.z;
return result;
}

bool Coordinate2D::operator==(const Coordinate2D& obj) const {
return (this->x == obj.x) && (this->z == obj.z);
}

bool Coordinate2D::operator!=(const Coordinate2D& obj) const {
return !(*this == obj);
}

Coordinate2D Coordinate2D::operator-(const Coordinate2D& obj) const {
Coordinate2D result;
result.x = this->x - obj.x;
result.z = this->z - obj.z;
return result;
}

Coordinate2D Coordinate2D::clone() const {
return Coordinate2D(this->x, this->z);
}

std::string to_string(const Coordinate2D& coord) {
using std::to_string;
return "(" + to_string(coord.x) + "," + to_string(coord.z) + ")";
}

std::ostream& operator<<(std::ostream& out, const Coordinate2D& coord) {
out << to_string(coord);
return out;
}

Chunk::Chunk(const Coordinate& loc1, const Coordinate& loc2,
const std::vector<BlockType>& block_list) {
Coordinate min{std::min(loc1.x, loc2.x), std::min(loc1.y, loc2.y),
Expand Down

0 comments on commit 6c67d4a

Please sign in to comment.