Colobot
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
point.h
Go to the documentation of this file.
1 // * This file is part of the COLOBOT source code
2 // * Copyright (C) 2012, Polish Portal of Colobot (PPC)
3 // *
4 // * This program is free software: you can redistribute it and/or modify
5 // * it under the terms of the GNU General Public License as published by
6 // * the Free Software Foundation, either version 3 of the License, or
7 // * (at your option) any later version.
8 // *
9 // * This program is distributed in the hope that it will be useful,
10 // * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // * GNU General Public License for more details.
13 // *
14 // * You should have received a copy of the GNU General Public License
15 // * along with this program. If not, see http://www.gnu.org/licenses/.
16 
22 #pragma once
23 
24 
25 #include "math/const.h"
26 #include "math/func.h"
27 
28 
29 #include <cmath>
30 #include <sstream>
31 
32 
33 // Math module namespace
34 namespace Math {
35 
36 
46 struct Point
47 {
49  float x;
51  float y;
52 
54  inline Point()
55  : x(0.0f)
56  , y(0.0f)
57  {}
58 
60  inline explicit Point(float _x, float _y)
61  : x(_x)
62  , y(_y)
63  {}
64 
66  inline void LoadZero()
67  {
68  x = y = 0.0f;
69  }
70 
72  inline float* Array()
73  {
74  return reinterpret_cast<float*>(this);
75  }
76 
78  inline const float* Array() const
79  {
80  return reinterpret_cast<const float*>(this);
81  }
82 
84  inline float Length()
85  {
86  return sqrtf(x*x + y*y);
87  }
88 
90  inline Point operator-() const
91  {
92  return Point(-x, -y);
93  }
94 
96  inline const Point& operator+=(const Point &right)
97  {
98  x += right.x;
99  y += right.y;
100  return *this;
101  }
102 
104  inline friend const Point operator+(const Point &left, const Point &right)
105  {
106  return Point(left.x + right.x, left.y + right.y);
107  }
108 
110  inline const Point& operator-=(const Point &right)
111  {
112  x -= right.x;
113  y -= right.y;
114  return *this;
115  }
116 
118  inline friend const Point operator-(const Point &left, const Point &right)
119  {
120  return Point(left.x - right.x, left.y - right.y);
121  }
122 
124  inline const Point& operator*=(const float &right)
125  {
126  x *= right;
127  y *= right;
128  return *this;
129  }
130 
132  inline friend const Point operator*(const float &left, const Point &right)
133  {
134  return Point(left * right.x, left * right.y);
135  }
136 
138  inline friend const Point operator*(const Point &left, const float &right)
139  {
140  return Point(left.x * right, left.y * right);
141  }
142 
144  inline const Point& operator/=(const float &right)
145  {
146  x /= right;
147  y /= right;
148  return *this;
149  }
150 
152  inline friend const Point operator/(const Point &left, const float &right)
153  {
154  return Point(left.x / right, left.y / right);
155  }
156 
157 
159  inline std::string ToString() const
160  {
161  std::stringstream s;
162  s.precision(3);
163  s << "[" << x << ", " << y << "]";
164  return s.str();
165  }
166 }; // struct Point
167 
168 
170 inline bool PointsEqual(const Point &a, const Point &b, float tolerance = TOLERANCE)
171 {
172  return IsEqual(a.x, b.x, tolerance) && IsEqual(a.y, b.y, tolerance);
173 }
174 
176 inline void Swap(Point &a, Point &b)
177 {
178  Point c;
179 
180  c = a;
181  a = b;
182  b = c;
183 }
184 
186 inline float Distance(const Point &a, const Point &b)
187 {
188  return sqrtf((a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y));
189 }
190 
191 
192 } // namespace Math
193 
const float TOLERANCE
Tolerance level – minimum accepted float value.
Definition: const.h:33
Point(float _x, float _y)
Constructs a point from given coords: (x,y)
Definition: point.h:60
float x
X coord.
Definition: point.h:49
Point operator-() const
Returns the inverted point.
Definition: point.h:90
const Point & operator-=(const Point &right)
Subtracts the given vector.
Definition: point.h:110
float Distance(const Point &a, const Point &b)
Returns the distance between two points.
Definition: point.h:186
friend const Point operator*(const float &left, const Point &right)
Multiplies point by scalar.
Definition: point.h:132
const float * Array() const
Returns the struct cast to const float* array; use with care!
Definition: point.h:78
void LoadZero()
Sets the zero point: (0,0)
Definition: point.h:66
float y
Y coord.
Definition: point.h:51
const Point & operator/=(const float &right)
Divides by given scalar.
Definition: point.h:144
bool IsEqual(float a, float b, float tolerance=Math::TOLERANCE)
Compares a and b within tolerance.
Definition: func.h:38
float * Array()
Returns the struct cast to float* array; use with care!
Definition: point.h:72
Point()
Constructs a zero point: (0,0)
Definition: point.h:54
void Swap(int &a, int &b)
Swaps two integers.
Definition: func.h:102
float Length()
Returns the distance from (0,0) to the point (x,y)
Definition: point.h:84
2D point
Definition: point.h:46
const Point & operator*=(const float &right)
Multiplies by given scalar.
Definition: point.h:124
Constants used in math functions.
friend const Point operator*(const Point &left, const float &right)
Multiplies point by scalar.
Definition: point.h:138
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:170
friend const Point operator/(const Point &left, const float &right)
Divides point by scalar.
Definition: point.h:152
friend const Point operator+(const Point &left, const Point &right)
Adds two points.
Definition: point.h:104
const Point & operator+=(const Point &right)
Adds the given point.
Definition: point.h:96
std::string ToString() const
Returns a string "[x, y]".
Definition: point.h:159
friend const Point operator-(const Point &left, const Point &right)
Subtracts two points.
Definition: point.h:118