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-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 
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 
40 
50 struct Point
51 {
53  float x;
55  float y;
56 
58  inline Point()
59  : x(0.0f)
60  , y(0.0f)
61  {}
62 
64  inline explicit Point(float _x, float _y)
65  : x(_x)
66  , y(_y)
67  {}
68 
70  inline void LoadZero()
71  {
72  x = y = 0.0f;
73  }
74 
76  inline float* Array()
77  {
78  return reinterpret_cast<float*>(this);
79  }
80 
82  inline const float* Array() const
83  {
84  return reinterpret_cast<const float*>(this);
85  }
86 
88  inline float Length()
89  {
90  return sqrtf(x*x + y*y);
91  }
92 
94  inline Point operator-() const
95  {
96  return Point(-x, -y);
97  }
98 
100  inline const Point& operator+=(const Point &right)
101  {
102  x += right.x;
103  y += right.y;
104  return *this;
105  }
106 
108  inline friend const Point operator+(const Point &left, const Point &right)
109  {
110  return Point(left.x + right.x, left.y + right.y);
111  }
112 
114  inline const Point& operator-=(const Point &right)
115  {
116  x -= right.x;
117  y -= right.y;
118  return *this;
119  }
120 
122  inline friend const Point operator-(const Point &left, const Point &right)
123  {
124  return Point(left.x - right.x, left.y - right.y);
125  }
126 
128  inline const Point& operator*=(const float &right)
129  {
130  x *= right;
131  y *= right;
132  return *this;
133  }
134 
136  inline friend const Point operator*(const float &left, const Point &right)
137  {
138  return Point(left * right.x, left * right.y);
139  }
140 
142  inline friend const Point operator*(const Point &left, const float &right)
143  {
144  return Point(left.x * right, left.y * right);
145  }
146 
148  inline const Point& operator/=(const float &right)
149  {
150  x /= right;
151  y /= right;
152  return *this;
153  }
154 
156  inline friend const Point operator/(const Point &left, const float &right)
157  {
158  return Point(left.x / right, left.y / right);
159  }
160 
161 
163  inline std::string ToString() const
164  {
165  std::stringstream s;
166  s.precision(3);
167  s << "[" << x << ", " << y << "]";
168  return s.str();
169  }
170 }; // struct Point
171 
172 
174 inline bool PointsEqual(const Point &a, const Point &b, float tolerance = TOLERANCE)
175 {
176  return IsEqual(a.x, b.x, tolerance) && IsEqual(a.y, b.y, tolerance);
177 }
178 
180 inline void Swap(Point &a, Point &b)
181 {
182  Point c;
183 
184  c = a;
185  a = b;
186  b = c;
187 }
188 
190 inline float Distance(const Point &a, const Point &b)
191 {
192  return sqrtf((a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y));
193 }
194 
195 
196 } // namespace Math
197 
const float TOLERANCE
Tolerance level – minimum accepted float value.
Definition: const.h:37
Point(float _x, float _y)
Constructs a point from given coords: (x,y)
Definition: point.h:64
float x
X coord.
Definition: point.h:53
Point operator-() const
Returns the inverted point.
Definition: point.h:94
const Point & operator-=(const Point &right)
Subtracts the given point.
Definition: point.h:114
float Distance(const Point &a, const Point &b)
Returns the distance between two points.
Definition: point.h:190
friend const Point operator*(const float &left, const Point &right)
Multiplies point by scalar.
Definition: point.h:136
const float * Array() const
Returns the struct cast to const float* array; use with care!
Definition: point.h:82
void LoadZero()
Sets the zero point: (0,0)
Definition: point.h:70
float y
Y coord.
Definition: point.h:55
const Point & operator/=(const float &right)
Divides by given scalar.
Definition: point.h:148
bool IsEqual(float a, float b, float tolerance=Math::TOLERANCE)
Compares a and b within tolerance.
Definition: func.h:41
float * Array()
Returns the struct cast to float* array; use with care!
Definition: point.h:76
Namespace for (new) math code.
Definition: device.h:39
Point()
Constructs a zero point: (0,0)
Definition: point.h:58
void Swap(int &a, int &b)
Swaps two integers.
Definition: func.h:114
float Length()
Returns the distance from (0,0) to the point (x,y)
Definition: point.h:88
2D point
Definition: point.h:50
const Point & operator*=(const float &right)
Multiplies by given scalar.
Definition: point.h:128
Constants used in math functions.
friend const Point operator*(const Point &left, const float &right)
Multiplies point by scalar.
Definition: point.h:142
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:174
friend const Point operator/(const Point &left, const float &right)
Divides point by scalar.
Definition: point.h:156
friend const Point operator+(const Point &left, const Point &right)
Adds two points.
Definition: point.h:108
const Point & operator+=(const Point &right)
Adds the given point.
Definition: point.h:100
std::string ToString() const
Returns a string "[x, y]".
Definition: point.h:163
friend const Point operator-(const Point &left, const Point &right)
Subtracts two points.
Definition: point.h:122