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