Colobot
intpoint.h
Go to the documentation of this file.
1 /*
2  * This file is part of the Colobot: Gold Edition source code
3  * Copyright (C) 2001-2016, Daniel Roux, EPSITEC SA & TerranovaTeam
4  * http://epsitec.ch; http://colobot.info; http://github.com/colobot
5  *
6  * This program 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  * This program 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.
14  * See the GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see http://gnu.org/licenses
18  */
19 
25 #pragma once
26 
27 #include <cmath>
28 #include <string>
29 #include <sstream>
30 
31 // Math module namespace
32 namespace Math
33 {
34 
41 struct IntPoint
42 {
44  int x;
46  int y;
47 
48  IntPoint(int aX = 0, int aY = 0) : x(aX), y(aY) {}
49 
50  inline bool operator==(const IntPoint& p) const
51  {
52  return x == p.x && y == p.y;
53  }
54 
55  inline bool operator!=(const IntPoint& p) const
56  {
57  return !operator==(p);
58  }
59 
60  inline float Length() const
61  {
62  return sqrtf(x*x + y*y);
63  }
64 
66  inline void LoadZero()
67  {
68  x = y = 0.0f;
69  }
70 
72  inline int* Array()
73  {
74  return reinterpret_cast<int*>(this);
75  }
76 
78  inline const int* Array() const
79  {
80  return reinterpret_cast<const int*>(this);
81  }
82 
84  inline IntPoint operator-() const
85  {
86  return IntPoint(-x, -y);
87  }
88 
90  inline const IntPoint& operator+=(const IntPoint &right)
91  {
92  x += right.x;
93  y += right.y;
94  return *this;
95  }
96 
98  inline friend const IntPoint operator+(const IntPoint &left, const IntPoint &right)
99  {
100  return IntPoint(left.x + right.x, left.y + right.y);
101  }
102 
104  inline const IntPoint& operator-=(const IntPoint &right)
105  {
106  x -= right.x;
107  y -= right.y;
108  return *this;
109  }
110 
112  inline friend const IntPoint operator-(const IntPoint &left, const IntPoint &right)
113  {
114  return IntPoint(left.x - right.x, left.y - right.y);
115  }
116 
118  inline const IntPoint& operator*=(const float &right)
119  {
120  x *= right;
121  y *= right;
122  return *this;
123  }
124 
126  inline friend const IntPoint operator*(const float &left, const IntPoint &right)
127  {
128  return IntPoint(left * right.x, left * right.y);
129  }
130 
132  inline friend const IntPoint operator*(const IntPoint &left, const int &right)
133  {
134  return IntPoint(left.x * right, left.y * right);
135  }
136 
138  inline const IntPoint& operator/=(const float &right)
139  {
140  x /= right;
141  y /= right;
142  return *this;
143  }
144 
146  inline friend const IntPoint operator/(const IntPoint &left, const int &right)
147  {
148  return IntPoint(left.x / right, left.y / right);
149  }
150 
152  inline std::string ToString() const
153  {
154  std::stringstream s;
155  s << "[" << x << ", " << y << "]";
156  return s.str();
157  }
158 };
159 
160 
161 } // namespace Math
162 
const IntPoint & operator*=(const float &right)
Multiplies by given scalar.
Definition: intpoint.h:118
const int * Array() const
Returns the struct cast to const int* array; use with care!
Definition: intpoint.h:78
friend const IntPoint operator+(const IntPoint &left, const IntPoint &right)
Adds two points.
Definition: intpoint.h:98
int * Array()
Returns the struct cast to int* array; use with care!
Definition: intpoint.h:72
int y
Y coord.
Definition: intpoint.h:46
friend const IntPoint operator/(const IntPoint &left, const int &right)
Divides point by scalar.
Definition: intpoint.h:146
const IntPoint & operator/=(const float &right)
Divides by given scalar.
Definition: intpoint.h:138
const IntPoint & operator+=(const IntPoint &right)
Adds the given point.
Definition: intpoint.h:90
std::string ToString() const
Returns a string "[x, y]".
Definition: intpoint.h:152
void LoadZero()
Sets the zero point: (0,0)
Definition: intpoint.h:66
IntPoint operator-() const
Returns the inverted point.
Definition: intpoint.h:84
Namespace for (new) math code.
Definition: device.h:39
friend const IntPoint operator*(const IntPoint &left, const int &right)
Multiplies point by scalar.
Definition: intpoint.h:132
int x
X coord.
Definition: intpoint.h:44
2D Point with integer coords
Definition: intpoint.h:41
friend const IntPoint operator*(const float &left, const IntPoint &right)
Multiplies point by scalar.
Definition: intpoint.h:126
const IntPoint & operator-=(const IntPoint &right)
Subtracts the given point.
Definition: intpoint.h:104
friend const IntPoint operator-(const IntPoint &left, const IntPoint &right)
Subtracts two points.
Definition: intpoint.h:112