Cytopia  0.3
A city building simulation game
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
PointFunctions.cxx
Go to the documentation of this file.
1 #include "PointFunctions.hxx"
2 #include <cassert>
3 
4 // forward declarations
5 static std::vector<Point> getLine;
6 static std::vector<Point> getStraightLine;
7 static std::vector<Point> getArea;
8 static std::vector<Point> getNeighbors;
9 
10 std::vector<Point> PointFunctions::getLine(Point isoCoordinatesStart, Point isoCoordinatesEnd)
11 {
12  std::vector<Point> line;
13  int x0 = isoCoordinatesStart.x;
14  int x1 = isoCoordinatesEnd.x;
15  int y0 = isoCoordinatesStart.y;
16  int y1 = isoCoordinatesEnd.y;
17 
18  Point p;
19  p.x = x0;
20  p.y = y0;
21  line.push_back(p);
22 
23  if (x0 != x1 || y0 != y1)
24  {
25  int dx = x1 - x0;
26  int dy = y1 - y0;
27  int stepx = 0;
28  int stepy = 0;
29 
30  if (dx < 0)
31  {
32  dx = -dx;
33  stepx = -1;
34  }
35  else
36  {
37  stepx = 1;
38  }
39 
40  if (dy < 0)
41  {
42  dy = -dy;
43  stepy = -1;
44  }
45  else
46  {
47  stepy = 1;
48  }
49 
50  // dx is now 2*dx
51  dx <<= 1;
52  // dy is now 2*dy
53  dy <<= 1;
54 
55  if (dx > dy)
56  {
57  int fraction = dy - (dx >> 1);
58  while (x0 != x1)
59  {
60  x0 += stepx;
61  if (fraction >= 0)
62  {
63  p.x = x0;
64  p.y = y0;
65  line.push_back(p);
66 
67  y0 += stepy;
68  fraction -= dx;
69  }
70  fraction += dy;
71  if (0 <= x0 && 0 <= y0)
72  {
73  p.x = x0;
74  p.y = y0;
75  line.push_back(p);
76  }
77  }
78  }
79  else
80  {
81  int fraction = dx - (dy >> 1);
82  while (y0 != y1)
83  {
84  if (fraction >= 0)
85  {
86  x0 += stepx;
87  fraction -= dy;
88 
89  p.x = x0;
90  p.y = y0;
91  line.push_back(p);
92  }
93  y0 += stepy;
94  fraction += dx;
95  if (0 <= x0 && 0 <= y0)
96  {
97  p.x = x0;
98  p.y = y0;
99  line.push_back(p);
100  }
101  }
102  }
103  }
104 
105  return line;
106 }
107 
108 std::vector<Point> PointFunctions::getStraightLine(const Point &isoCoordinatesStart, const Point &isoCoordinatesEnd)
109 {
110  std::vector<Point> rectangle;
111  int directionX = isoCoordinatesStart.x < isoCoordinatesEnd.x ? 1 : -1;
112  int directionY = isoCoordinatesStart.y < isoCoordinatesEnd.y ? 1 : -1;
113  int staticX, staticY;
114  bool reverseDirection = false;
115 
116  uint32_t xDist = std::abs(isoCoordinatesStart.x - isoCoordinatesEnd.x);
117  uint32_t yDist = std::abs(isoCoordinatesStart.y - isoCoordinatesEnd.y);
118 
119  if (xDist == 0 && yDist == 1)
120  {
121  reverseDirection = true;
122  }
123  else if (xDist == 1 && yDist == 0)
124  {
125  reverseDirection = false;
126  }
127  else if (xDist == 0 && yDist == 0)
128  {
129  rectangle.push_back(Point{isoCoordinatesStart.x, isoCoordinatesStart.y});
130  }
131 
132  if (reverseDirection)
133  {
134  staticX = isoCoordinatesStart.x;
135  staticY = isoCoordinatesEnd.y;
136  }
137  else
138  {
139  staticX = isoCoordinatesEnd.x;
140  staticY = isoCoordinatesStart.y;
141  }
142 
143  for (int x = isoCoordinatesStart.x; x != isoCoordinatesEnd.x; x += directionX)
144  {
145  rectangle.push_back(Point{x, staticY});
146  }
147 
148  for (int y = isoCoordinatesStart.y; y != isoCoordinatesEnd.y; y += directionY)
149  {
150  rectangle.push_back(Point{staticX, y});
151  }
152 
153  return rectangle;
154 }
155 
156 std::vector<Point> PointFunctions::getArea(const Point &isoCoordinatesStart, const Point &isoCoordinatesEnd)
157 {
158  std::vector<Point> rectangle;
159  SDL_Point startRect;
160  SDL_Point endRect;
161  std::tie(startRect.x, endRect.x) = std::minmax(isoCoordinatesStart.x, isoCoordinatesEnd.x);
162  std::tie(startRect.y, endRect.y) = std::minmax(isoCoordinatesStart.y, isoCoordinatesEnd.y);
163 
164  for (int x = startRect.x; x <= endRect.x; x++)
165  {
166  for (int y = startRect.y; y <= endRect.y; y++)
167  {
168  rectangle.push_back(Point{x, y});
169  }
170  }
171 
172  return rectangle;
173 }
174 
175 std::vector<Point> PointFunctions::getNeighbors(const Point &isoCoordinates, const bool includeCentralNode, int distance)
176 {
177  std::vector<Point> neighbors;
178 
179  for (int xOffset = -distance; xOffset <= distance; ++xOffset)
180  {
181  for (int yOffset = -distance; yOffset <= distance; ++yOffset)
182  {
183  if (!includeCentralNode && (xOffset == 0) && (yOffset == 0))
184  {
185  continue;
186  }
187 
188  Point neighbor;
189  neighbor.x = isoCoordinates.x + xOffset;
190  neighbor.y = isoCoordinates.y + yOffset;
191 
192  if (neighbor.isWithinMapBoundaries())
193  {
194  neighbors.push_back(neighbor);
195  }
196  }
197  }
198 
199  return neighbors;
200 }
201 
202 NeighborNodesPosition PointFunctions::getNeighborPositionToOrigin(const Point &neighboringPoint, const Point &originPoint)
203 {
204  int neighborOffsetX = neighboringPoint.x - originPoint.x;
205  int neighborOffsetY = neighboringPoint.y - originPoint.y;
206 
207  switch (neighborOffsetX)
208  {
209  case 0:
210  switch (neighborOffsetY)
211  {
212  case 0:
213  return NeighborNodesPosition::CENTER;
214  case 1:
215  return NeighborNodesPosition::TOP;
216  case -1:
217  return NeighborNodesPosition::BOTTOM;
218  }
219  break;
220  case 1:
221  switch (neighborOffsetY)
222  {
223  case 0:
224  return NeighborNodesPosition::RIGHT;
225  case 1:
226  return NeighborNodesPosition::TOP_RIGHT;
227  case -1:
228  return NeighborNodesPosition::BOTTOM_RIGHT;
229  }
230  break;
231  case -1:
232  switch (neighborOffsetY)
233  {
234  case 0:
235  return NeighborNodesPosition::LEFT;
236  case 1:
237  return NeighborNodesPosition::TOP_LEFT;
238  case -1:
239  return NeighborNodesPosition::BOTTOM_LEFT;
240  }
241  break;
242  }
243  assert(false); // this should never happen!
244  return NeighborNodesPosition::CENTER;
245 }
getArea
static std::vector< Point > getArea
Definition: PointFunctions.cxx:7
PointFunctions::getArea
static std::vector< Point > getArea(const Point &isoCoordinatesStart, const Point &isoCoordinatesEnd)
Gets all nodes in a rectangular area between start and end point.
Definition: PointFunctions.cxx:156
Point::y
int y
The y coordinate.
Definition: Point.hxx:20
Point::x
int x
The x coordinate.
Definition: Point.hxx:14
PointFunctions::getLine
static std::vector< Point > getLine(Point isoCoordinatesStart, Point isoCoordinatesEnd)
Creates a line between two points using the Bresenham Line algorithm.
Definition: PointFunctions.cxx:10
PointFunctions::getStraightLine
static std::vector< Point > getStraightLine(const Point &isoCoordinatesStart, const Point &isoCoordinatesEnd)
Gets all nodes in a straight line from start and end point.
Definition: PointFunctions.cxx:108
Point::isWithinMapBoundaries
bool isWithinMapBoundaries() const
Definition: Point.hxx:37
PointFunctions::getNeighbors
static std::vector< Point > getNeighbors(const Point &isoCoordinates, const bool includeCentralNode, int distance=1)
Get all neighboring coordinates from provided map node isocoordinate.
Definition: PointFunctions.cxx:175
getStraightLine
static std::vector< Point > getStraightLine
Definition: PointFunctions.cxx:6
PointFunctions.hxx
PointFunctions::getNeighborPositionToOrigin
static NeighborNodesPosition getNeighborPositionToOrigin(const Point &neighboringPoint, const Point &originPoint)
Get the position of the neighboring node to the originpoint (center of the neighborgroup).
Definition: PointFunctions.cxx:202
getNeighbors
static std::vector< Point > getNeighbors
Definition: PointFunctions.cxx:8
reverseDirection
bool reverseDirection
Definition: isoMath.cxx:9
Point
Definition: Point.hxx:7
getLine
static std::vector< Point > getLine
Definition: PointFunctions.cxx:5