Gnash  0.8.11dev
Point2d.h
Go to the documentation of this file.
1 // Point2d template - for gnash
2 //
3 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
4 // Free Software Foundation, Inc
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. See the
14 // 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, write to the Free Software
18 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 
20 //
21 // Original author: Sandro Santilli <strk@keybit.net>
22 //
23 
24 #ifndef GNASH_POINT2DH
25 #define GNASH_POINT2DH
26 
27 #include <ostream>
28 #include <cmath> // for sqrt()
29 #include <cstdint>
30 
31 namespace gnash {
32 namespace geometry {
33 
35 //
38 class Point2d
39 {
40 public:
41 
43  std::int32_t x; // TWIPS
44 
46  std::int32_t y; // TWIPS
47 
49  constexpr Point2d()
50  :
51  x(0), y(0)
52  {
53  }
54 
56  constexpr Point2d(std::int32_t cx, std::int32_t cy)
57  :
58  x(cx), y(cy)
59  {
60  }
61 
63  //
68  Point2d(const Point2d& p0, const Point2d& p1, float t)
69  :
70  x( p0.x + (std::int32_t)((p1.x - p0.x) * t)),
71  y( p0.y + (std::int32_t)((p1.y - p0.y) * t))
72  {
73  }
74 
76  //
79  Point2d& setTo(const std::int32_t cx, const std::int32_t cy)
80  {
81  x = cx;
82  y = cy;
83  return *this;
84  }
85 
87  //
94  Point2d& setTo(const Point2d& p0, const Point2d& p1, float t)
95  {
96  x = p0.x + (std::int32_t)((p1.x - p0.x) * t);
97  y = p0.y + (std::int32_t)((p1.y - p0.y) * t);
98  return *this;
99  }
100 
102  static
103  std::int64_t squareDistance(const Point2d& p0, const Point2d& p1)
104  {
105  std::int64_t hside = p1.x - p0.x;
106  std::int64_t vside = p1.y - p0.y;
107 
108  return hside*hside + vside*vside;
109  }
110 
112  std::int64_t squareDistance(const Point2d& p) const
113  {
114  return squareDistance(*this, p);
115  }
116 
118  std::int32_t distance(const Point2d& p) const
119  {
120  return (std::int32_t)( std::sqrt( static_cast<double>(squareDistance(p)) ) );
121  }
122 
123  bool operator== (const Point2d& p) const
124  {
125  return (x == p.x) && (y == p.y);
126  }
127 
128  bool operator!=(const Point2d& p) const
129  {
130  return ! (*this == p);
131  }
132 };
133 
135 inline std::ostream&
136 operator<< (std::ostream& os, const Point2d& p)
137 {
138  return os << "Point2d(" << p.x << "," << p.y << ")";
139 }
140 
141 } // namespace gnash::geometry
142 
144 
145 } // namespace gnash
146 
147 #endif // GNASH_POINT2DH
148 
149 // Local Variables:
150 // mode: C++
151 // indent-tabs-mode: t
152 // End:
constexpr Point2d()
Construct a Point2d with default x and y coordinates.
Definition: Point2d.h:49
geometry::Point2d point
Definition: Point2d.h:143
Point2d(const Point2d &p0, const Point2d &p1, float t)
Construct a Point2d as an interpolation of the given input points.
Definition: Point2d.h:68
Anonymous namespace for callbacks, local functions, event handlers etc.
Definition: dbus_ext.cpp:40
Point2d & setTo(const std::int32_t cx, const std::int32_t cy)
Set coordinates to given values.
Definition: Point2d.h:79
bool operator==(const Point2d &p) const
Definition: Point2d.h:123
2D Point class
Definition: Point2d.h:38
std::int64_t squareDistance(const Point2d &p) const
Return square distance between this and the given point.
Definition: Point2d.h:112
std::ostream & operator<<(std::ostream &os, const Point2d &p)
Output operator.
Definition: Point2d.h:136
Definition: GnashKey.h:166
bool operator!=(const Point2d &p) const
Definition: Point2d.h:128
std::int32_t distance(const Point2d &p) const
Return distance between this and the given point.
Definition: Point2d.h:118
std::int32_t y
The y coordinate.
Definition: Point2d.h:46
std::int32_t x
The x coordinate.
Definition: Point2d.h:43
Definition: GnashKey.h:162
Point2d & setTo(const Point2d &p0, const Point2d &p1, float t)
Set coordinates to the ones of the interpolation between the given input points.
Definition: Point2d.h:94
constexpr Point2d(std::int32_t cx, std::int32_t cy)
Construct a Point2d with given x and y ordinates.
Definition: Point2d.h:56
static std::int64_t squareDistance(const Point2d &p0, const Point2d &p1)
Return square distance between two given points.
Definition: Point2d.h:103