Debian Codegraph @

engine/core/util/math/angles.cpp

raw
1 /***************************************************************************
2 * Copyright (C) 2005-2019 by the FIFE team *
3 * http://www.fifengine.net *
4 * This file is part of FIFE. *
5 * *
6 * FIFE is free software; you can redistribute it and/or *
7 * modify it under the terms of the GNU Lesser General Public *
8 * License as published by the Free Software Foundation; either *
9 * version 2.1 of the License, or (at your option) any later version. *
10 * *
11 * This library is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
14 * Lesser General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU Lesser General Public *
17 * License along with this library; if not, write to the *
18 * Free Software Foundation, Inc., *
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
20 ***************************************************************************/
21
22 // Standard C++ library includes
23 #include <iostream>
24
25 // 3rd party library includes
26
27 // FIFE includes
28 // These includes are split up in two parts, separated by one empty line
29 // First block: files included from the FIFE root src directory
30 // Second block: files included from the same folder
31 #include "model/metamodel/grids/cellgrid.h"
32 #include "model/structures/layer.h"
33
34 #include "angles.h"
35
36 namespace FIFE {
37 int32_t getIndexByAngle(int32_t angle, const type_angle2id& angle2id, int32_t& closestMatchingAngle) {
38 if (angle2id.empty()) {
39 return -1;
40 }
41 if (angle2id.size() == 1) {
42 closestMatchingAngle = angle2id.begin()->first;
43 return angle2id.begin()->second;
44 }
45
46 int32_t wangle = (360 + angle) % 360;
47 type_angle2id::const_iterator u(angle2id.upper_bound(wangle));
48 type_angle2id::const_iterator tmp;
49
50 // take care of the forward wrapping case
51 if (u == angle2id.end()) {
52 int32_t ud = wangle - (--u)->first;
53 int32_t ld = 360 - wangle + angle2id.begin()->first;
54 if (ud > ld) {
55 // wrapped value (first)
56 closestMatchingAngle = angle2id.begin()->first;
57 return angle2id.begin()->second;
58 }
59 // non-wrapped value
60 closestMatchingAngle = u->first;
61 return u->second;
62 }
63
64 // take care of the backward wrapping case
65 if (u == angle2id.begin()) {
66 tmp = angle2id.end();
67 tmp--;
68 int32_t ld = u->first - wangle;
69 int32_t ud = 360 - tmp->first + wangle;
70 if (ud > ld) {
71 // non-wrapped value (first)
72 closestMatchingAngle = angle2id.begin()->first;
73 return angle2id.begin()->second;
74 }
75 // wrapped value (last)
76 closestMatchingAngle = tmp->first;
77 return tmp->second;
78 }
79
80 // value in the middle...
81 int32_t ud = u->first - wangle;
82 int32_t ucm = u->first;
83 int32_t ui = u->second;
84 u--;
85 int32_t ld = wangle - u->first;
86 int32_t lcm = u->first;
87 int32_t li = u->second;
88
89 // if ud and ls is equal then lcm is prefered (next angle)
90 if (ud < ld) {
91 closestMatchingAngle = ucm;
92 return ui;
93 }
94 closestMatchingAngle = lcm;
95 return li;
96 }
97
98 int32_t getAngleBetween(const Location& loc1, const Location& loc2) {
99 ExactModelCoordinate c1 = loc1.getMapCoordinates();
100 ExactModelCoordinate c2 = loc2.getMapCoordinates();
101
102 double dy = (c2.y - c1.y);
103 double dx = (c2.x - c1.x);
104 // add grid rotation to angle, to guarantee uniform angles (not grid based)
105 int32_t angle = round(Mathd::ATan2(-dy,dx)*(180.0/Mathd::pi()) + loc1.getLayer()->getCellGrid()->getRotation());
106 if (angle < 0) {
107 angle += 360;
108 }
109 angle %= 360;
110 return angle;
111 }
112
113 Location getFacing(const Location& loc, const int32_t angle) {
114 Location facing(loc);
115 ExactModelCoordinate emc = facing.getMapCoordinates();
116 // remove grid rotation from angle, to guarantee uniform angles (not grid based)
117 double tmpAngle = static_cast<double>(angle) - loc.getLayer()->getCellGrid()->getRotation();
118 emc.x += Mathd::Cos(tmpAngle * (Mathd::pi()/180.0));
119 emc.y -= Mathd::Sin(tmpAngle * (Mathd::pi()/180.0));
120 facing.setMapCoordinates(emc);
121
122 return facing;
123 }
124
125 int32_t getAngleBetween(const ExactModelCoordinate& emc1, const ExactModelCoordinate& emc2) {
126 double dy = (emc2.y - emc1.y);
127 double dx = (emc2.x - emc1.x);
128
129 int32_t angle = round(Mathd::ATan2(-dy,dx)*(180.0/Mathd::pi()));
130 if (angle < 0) {
131 angle += 360;
132 }
133 angle %= 360;
134 return angle;
135 }
136
137 ExactModelCoordinate getFacing(const ExactModelCoordinate& emc, const int32_t angle) {
138 ExactModelCoordinate result = emc;
139 result.x += Mathd::Cos(static_cast<double>(angle) * (Mathd::pi()/180.0));
140 result.y -= Mathd::Sin(static_cast<double>(angle) * (Mathd::pi()/180.0));
141 return result;
142 }
143 }