OpenXcom  1.0
Open-source clone of the original X-Com
GraphSubset.h
1 #pragma once
2 /*
3  * Copyright 2010-2016 OpenXcom Developers.
4  *
5  * This file is part of OpenXcom.
6  *
7  * OpenXcom is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * OpenXcom is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with OpenXcom. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #ifndef OPENXCOM_GRAPHSUBSET_H
22 #define OPENXCOM_GRAPHSUBSET_H
23 
24 #include <utility>
25 #include <algorithm>
26 
27 namespace OpenXcom
28 {
29 
30 
31 
33 {
34 
35  //define part of surface
36  int beg_x, end_x;
37  int beg_y, end_y;
38 
39  GraphSubset(int max_x, int max_y):
40  beg_x(0), end_x(max_x),
41  beg_y(0), end_y(max_y)
42  {
43 
44  }
45 
46 
47  GraphSubset(std::pair<int, int> range_x, std::pair<int, int> range_y):
48  beg_x(range_x.first), end_x(range_x.second),
49  beg_y(range_y.first), end_y(range_y.second)
50  {
51 
52  }
53 
54  GraphSubset(const GraphSubset& r):
55  beg_x(r.beg_x), end_x(r.end_x),
56  beg_y(r.beg_y), end_y(r.end_y)
57  {
58 
59  }
60 
61  inline GraphSubset offset(int x, int y) const
62  {
63  GraphSubset ret = *this;
64  ret.beg_x += x;
65  ret.end_x += x;
66  ret.beg_y += y;
67  ret.end_y += y;
68  return ret;
69  }
70 
71  inline int size_x() const
72  {
73  return end_x - beg_x;
74  }
75 
76  inline int size_y() const
77  {
78  return end_y - beg_y;
79  }
80 
81 
82  static inline void intersection_range(int& begin_a, int& end_a, const int& begin_b, const int& end_b)
83  {
84  if (begin_a >= end_b || begin_b >= end_a)
85  {
86  //intersection is empty
87  end_a = begin_a;
88  }
89  else
90  {
91  begin_a = std::max(begin_a, begin_b);
92  end_a = std::min(end_a, end_b);
93  }
94  }
95  static inline GraphSubset intersection(const GraphSubset& a, const GraphSubset& b)
96  {
97  GraphSubset ret = a;
98  intersection_range(ret.beg_x, ret.end_x, b.beg_x, b.end_x);
99  intersection_range(ret.beg_y, ret.end_y, b.beg_y, b.end_y);
100  return ret;
101  }
102  static inline GraphSubset intersection(const GraphSubset& a, const GraphSubset& b, const GraphSubset& c)
103  {
104  GraphSubset ret = intersection(a, b);
105  intersection_range(ret.beg_x, ret.end_x, c.beg_x, c.end_x);
106  intersection_range(ret.beg_y, ret.end_y, c.beg_y, c.end_y);
107  return ret;
108  }
109  static inline GraphSubset intersection(const GraphSubset& a, const GraphSubset& b, const GraphSubset& c, const GraphSubset& d)
110  {
111  GraphSubset ret = intersection(a, b, c);
112  intersection_range(ret.beg_x, ret.end_x, d.beg_x, d.end_x);
113  intersection_range(ret.beg_y, ret.end_y, d.beg_y, d.end_y);
114  return ret;
115  }
116 
117 };
118 
119 }//namespace OpenXcom
120 
121 #endif /* OPENXCOM_GRAPHSUBSET_H */
122 
Definition: GraphSubset.h:32
Definition: BaseInfoState.cpp:40