From afa02f645056c5823b0d8a29d37c1ff10aedce7a Mon Sep 17 00:00:00 2001 From: root Date: Tue, 27 Feb 2024 21:53:44 -0600 Subject: init --- coordinate.cpp | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 coordinate.cpp (limited to 'coordinate.cpp') diff --git a/coordinate.cpp b/coordinate.cpp new file mode 100644 index 0000000..54a225c --- /dev/null +++ b/coordinate.cpp @@ -0,0 +1,45 @@ +// Programmer: Brett Weiland +// Date: 4/30/23 +// File: coordinate.cpp +// Assignment: SP-B-finalProject +// Purpose: function for computing info and interacting with coordinates and directions. + +#include +#include "coordinate.h" + +int coordinate::get_x() const { return x; } +int coordinate::get_y() const { return y; } +void coordinate::set_x(const int pos_x) { x = pos_x; } +void coordinate::set_y(const int pos_y) { y = pos_y; } +coordinate::coordinate() : x(0), y(0) {} ; +coordinate::coordinate(int pos_x, int pos_y) : x(pos_x), y(pos_y) {}; + +void coordinate::set_xy(const int pos_x, const int pos_y) { + x = pos_x; + y = pos_y; +} + + +int coordinate::getSteps(const coordinate &objective) const { + int steps1, steps2; + steps1 = abs(this->get_x() - objective.get_x()); + steps2 = abs(this->get_y() - objective.get_y()); + return (steps1 > steps2) ? steps1 : steps2; //no max function allowed +} + + +direction::TYPE coordinate::getDirection(const coordinate &objective) const { + return ( + (this->get_y() > objective.get_y()) | + ((this->get_x() < objective.get_x()) << 1) | + ((this->get_y() < objective.get_y()) << 2) | + ((this->get_x() > objective.get_x()) << 3)); +} + +void coordinate::stepDirection(const direction::TYPE path) { + y -= ((path & (1 << direction::N)) >> direction::N); + y += ((path & (1 << direction::S)) >> direction::S); + x += ((path & (1 << direction::E)) >> direction::E); + x -= ((path & (1 << direction::W)) >> direction::W); +} + -- cgit v1.2.3