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 <boost/cstdint.hpp>
30 
31 namespace gnash {
32 namespace geometry {
33 
35 //
38 class Point2d
39 {
40 public:
41 
43  boost::int32_t x; // TWIPS
44 
46  boost::int32_t y; // TWIPS
47 
50  :
51  x(0), y(0)
52  {
53  }
54 
56  Point2d(boost::int32_t cx, boost::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 + (boost::int32_t)((p1.x - p0.x) * t)),
71  y( p0.y + (boost::int32_t)((p1.y - p0.y) * t))
72  {
73  }
74 
76  //
79  Point2d& setTo(const boost::int32_t cx, const boost::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 + (boost::int32_t)((p1.x - p0.x) * t);
97  y = p0.y + (boost::int32_t)((p1.y - p0.y) * t);
98  return *this;
99  }
100 
102  static
103  boost::int64_t squareDistance(const Point2d& p0, const Point2d& p1)
104  {
105  boost::int64_t hside = p1.x - p0.x;
106  boost::int64_t vside = p1.y - p0.y;
107 
108  return hside*hside + vside*vside;
109  }
110 
112  boost::int64_t squareDistance(const Point2d& p) const
113  {
114  return squareDistance(*this, p);
115  }
116 
118  boost::int32_t distance(const Point2d& p) const
119  {
120  return (boost::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: