-
Notifications
You must be signed in to change notification settings - Fork 1
/
location.cpp
58 lines (54 loc) · 1.36 KB
/
location.cpp
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
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <cstdlib>
#include <ctime>
#include <math.h>
#include "simulation.h"
#include "location.hpp"
#define AT 0.5
namespace sim{
//Constructors
Location::Location(){
x = rand()%MAX_X;
y = rand()%MAX_Y;
}
Location::Location(double xx, double yy)
:x(xx), y(yy){}
//Getters and setters for the coordinates
double Location::getX() const{
return x;
}
double Location::getY() const{
return y;
}
void Location::setX(double nx){
x = nx;
}
void Location::setY(double ny){
y = ny;
}
//Get distance between this point and another
double Location::getDistance(Location point) const{
double nx = this->x - point.getX();
double ny = this->y - point.getY();
return sqrt((nx*nx)+(ny*ny));
}
//Check if current location is equivalent to another
bool Location::atLocation(Location destination) const{
return (this->getDistance(destination) <= AT);
}
//Move to destination
bool Location::move(Location* destination, double speed){
double distance = this->getDistance(*destination);
double dx = destination->getX() - this->x;
double dy = destination->getY() - this->y;
double alpha = atan2(dy, dx);
if(distance < speed){
x = destination->getX();
y = destination->getY();
return true;
} else{
x += speed*cos(alpha);
y += speed*sin(alpha);
return false;
}
}
}