summaryrefslogtreecommitdiff
path: root/coordinate.cpp
blob: 54a225c15caa56d2d95c84b5757b2ab2cf825552 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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 <cmath>
#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);
}