forked from EcoSimIBM/EcoSim-Default
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPosition.cpp
86 lines (69 loc) · 1.63 KB
/
Position.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*
* Position.cpp
* - Provides each individual with an (x, y)
* position in the environment and methods for calculating
* the distance between points
*/
#include <cmath>
#include "Position.h"
#include "Ecosystem.h"
using namespace std;
Position::Position() {
}
Position::~Position() {
}
/*
* Returns the distance between the calling Position and Position 'p'
*/
float Position::distance(Position p, int Width, int Height) {
int diffxs, diffys, diffxd, diffyd;
if (x > p.x) {
diffxs = x - p.x;
diffxd = x - p.x - Width;
} else {
diffxs = p.x - x;
diffxd = p.x - x - Width;
}
if (y > p.y) {
diffys = y - p.y;
diffyd = y - p.y - Height;
} else {
diffys = p.y - y;
diffyd = p.y - y - Height;
}
if (abs(diffxs) > abs(diffxd)) {
diffxs = diffxd;
}
if (abs(diffys) > abs(diffyd)) {
diffys = diffyd;
}
float res = float (diffxs * diffxs + diffys * diffys);
res = sqrt(res);
return res;
}
/*
* Returns the direction of movement between the calling Position
* and Position 'arrivee'
*/
Direction Position::calculDirection(Position arrivee, int Width, int Height) {
//-- Meisam 11 March 2011 -> Relative position with preserving the diretion
Direction resul;
int diffxs, diffys;
diffxs = arrivee.x - x;
diffys = arrivee.y - y;
if (diffxs > 0){
if ( diffxs > (Width - diffxs) )
diffxs = -(Width - diffxs);
}else
if ( diffxs < (- Width - diffxs) )
diffxs = - Width - diffxs;
if (diffys > 0){
if ( diffys > (Height - diffys) )
diffys = -(Height - diffys);
}else
if ( diffys < (- Height - diffys) )
diffys = - Height - diffys;
resul.x = diffxs;
resul.y = diffys;
return resul;
}