Colobot
point.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-2014, Daniel Roux, EPSITEC SA & TerranovaTeam
4  * http://epsiteс.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 
28 #include "math/const.h"
29 #include "math/func.h"
30 
31 
32 #include <cmath>
33 #include <sstream>
34 
35 
36 // Math module namespace
37 namespace Math {
38 
39 
49 struct Point
50 {
52  float x;
54  float y;
55 
57  inline Point()
58  : x(0.0f)
59  , y(0.0f)
60  {}
61 
63  inline explicit Point(float _x, float _y)
64  : x(_x)
65  , y(_y)
66  {}
67 
69  inline void LoadZero()
70  {
71  x = y = 0.0f;
72  }
73 
75  inline float* Array()
76  {
77  return reinterpret_cast<float*>(this);
78  }
79 
81  inline const float* Array() const
82  {
83  return reinterpret_cast<const float*>(this);
84  }
85 
87  inline float Length()
88  {
89  return sqrtf(x*x + y*y);
90  }
91 
93  inline Point operator-() const
94  {
95  return Point(-x, -y);
96  }
97 
99  inline const Point& operator+=(const Point &right)
100  {
101  x += right.x;
102  y += right.y;
103  return *this;
104  }
105 
107  inline friend const Point operator+(const Point &left, const Point &right)
108  {
109  return Point(left.x + right.x, left.y + right.y);
110  }
111 
113  inline const Point& operator-=(const Point &right)
114  {
115  x -= right.x;
116  y -= right.y;
117  return *this;
118  }
119 
121  inline friend const Point operator-(const Point &left, const Point &right)
122  {
123  return Point(left.x - right.x, left.y - right.y);
124  }
125 
127  inline const Point& operator*=(const float &right)
128  {
129  x *= right;
130  y *= right;
131  return *this;
132  }
133 
135  inline friend const Point operator*(const float &left, const Point &right)
136  {
137  return Point(left * right.x, left * right.y);
138  }
139 
141  inline friend const Point operator*(const Point &left, const float &right)
142  {
143  return Point(left.x * right, left.y * right);
144  }
145 
147  inline const Point& operator/=(const float &right)
148  {
149  x /= right;
150  y /= right;
151  return *this;
152  }
153 
155  inline friend const Point operator/(const Point &left, const float &right)
156  {
157  return Point(left.x / right, left.y / right);
158  }
159 
160 
162  inline std::string ToString() const
163  {
164  std::stringstream s;
165  s.precision(3);
166  s << "[" << x << ", " << y << "]";
167  return s.str();
168  }
169 }; // struct Point
170 
171 
173 inline bool PointsEqual(const Point &a, const Point &b, float tolerance = TOLERANCE)
174 {
175  return IsEqual(a.x, b.x, tolerance) && IsEqual(a.y, b.y, tolerance);
176 }
177 
179 inline void Swap(Point &a, Point &b)
180 {
181  Point c;
182 
183  c = a;
184  a = b;
185  b = c;
186 }
187 
189 inline float Distance(const Point &a, const Point &b)
190 {
191  return sqrtf((a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y));
192 }
193 
194 
195 } // namespace Math
196 
const float TOLERANCE
Tolerance level – minimum accepted float value.
Definition: const.h:36
Point(float _x, float _y)
Constructs a point from given coords: (x,y)
Definition: point.h:63
float x
X coord.
Definition: point.h:52
Point operator-() const
Returns the inverted point.
Definition: point.h:93
const Point & operator-=(const Point &right)
Subtracts the given vector.
Definition: point.h:113
float Distance(const Point &a, const Point &b)
Returns the distance between two points.
Definition: point.h:189
friend const Point operator*(const float &left, const Point &right)
Multiplies point by scalar.
Definition: point.h:135
const float * Array() const
Returns the struct cast to const float* array; use with care!
Definition: point.h:81
void LoadZero()
Sets the zero point: (0,0)
Definition: point.h:69
float y
Y coord.
Definition: point.h:54
const Point & operator/=(const float &right)
Divides by given scalar.
Definition: point.h:147
bool IsEqual(float a, float b, float tolerance=Math::TOLERANCE)
Compares a and b within tolerance.
Definition: func.h:40
float * Array()
Returns the struct cast to float* array; use with care!
Definition: point.h:75
Namespace for (new) math code.
Definition: const.h:32
Point()
Constructs a zero point: (0,0)
Definition: point.h:57
void Swap(int &a, int &b)
Swaps two integers.
Definition: func.h:104
float Length()
Returns the distance from (0,0) to the point (x,y)
Definition: point.h:87
2D point
Definition: point.h:49
const Point & operator*=(const float &right)
Multiplies by given scalar.
Definition: point.h:127
Constants used in math functions.
friend const Point operator*(const Point &left, const float &right)
Multiplies point by scalar.
Definition: point.h:141
Common math functions.
bool PointsEqual(const Point &a, const Point &b, float tolerance=TOLERANCE)
Checks if two vectors are equal within given tolerance.
Definition: point.h:173
friend const Point operator/(const Point &left, const float &right)
Divides point by scalar.
Definition: point.h:155
friend const Point operator+(const Point &left, const Point &right)
Adds two points.
Definition: point.h:107
const Point & operator+=(const Point &right)
Adds the given point.
Definition: point.h:99
std::string ToString() const
Returns a string "[x, y]".
Definition: point.h:162
friend const Point operator-(const Point &left, const Point &right)
Subtracts two points.
Definition: point.h:121