update copyright date
[gnash.git] / libbase / Point2d.h
blobf102e3666a7bd1e7217cb5f72946c206ea93d7ab
1 // Point2d template - for gnash
2 //
3 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010,
4 // 2011 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
21 // Original author: Sandro Santilli <strk@keybit.net>
24 #ifndef GNASH_POINT2DH
25 #define GNASH_POINT2DH
27 #include <ostream>
28 #include <cmath> // for sqrt()
29 #include <boost/cstdint.hpp>
31 namespace gnash {
32 namespace geometry {
34 /// 2D Point class
36 /// A point which contains a x and a y coorinate in TWIPS.
37 ///
38 class Point2d
40 public:
42 /// The x coordinate
43 boost::int32_t x; // TWIPS
45 /// The y coordinate
46 boost::int32_t y; // TWIPS
48 /// Construct a Point2d with default x and y coordinates
49 Point2d()
51 x(0), y(0)
55 /// Construct a Point2d with given x and y ordinates
56 Point2d(boost::int32_t cx, boost::int32_t cy)
58 x(cx), y(cy)
62 /// Construct a Point2d as an interpolation of the given input points
64 /// @param p0 first point
65 /// @param p1 second point
66 /// @param t interpolation factor, between 0 and 1
67 ///
68 Point2d(const Point2d& p0, const Point2d& p1, float t)
70 x( p0.x + (boost::int32_t)((p1.x - p0.x) * t)),
71 y( p0.y + (boost::int32_t)((p1.y - p0.y) * t))
75 /// Set coordinates to given values
77 /// @return a reference to this instance
78 ///
79 Point2d& setTo(const boost::int32_t cx, const boost::int32_t cy)
81 x = cx;
82 y = cy;
83 return *this;
86 /// Set coordinates to the ones of the interpolation between the given input points
88 /// @param p0 first point
89 /// @param p1 second point
90 /// @param t interpolation factor, between 0 and 1
91 ///
92 /// @return a reference to this instance
93 ///
94 Point2d& setTo(const Point2d& p0, const Point2d& p1, float t)
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;
101 /// Return square distance between two given points.
102 static
103 boost::int64_t squareDistance(const Point2d& p0, const Point2d& p1)
105 boost::int64_t hside = p1.x - p0.x;
106 boost::int64_t vside = p1.y - p0.y;
108 return hside*hside + vside*vside;
111 /// Return square distance between this and the given point
112 boost::int64_t squareDistance(const Point2d& p) const
114 return squareDistance(*this, p);
117 /// Return distance between this and the given point
118 boost::int32_t distance(const Point2d& p) const
120 return (boost::int32_t)( std::sqrt( static_cast<double>(squareDistance(p)) ) );
123 bool operator== (const Point2d& p) const
125 return (x == p.x) && (y == p.y);
128 bool operator!=(const Point2d& p) const
130 return ! (*this == p);
134 /// Output operator
135 inline std::ostream&
136 operator<< (std::ostream& os, const Point2d& p)
138 return os << "Point2d(" << p.x << "," << p.y << ")";
141 } // namespace gnash::geometry
143 typedef geometry::Point2d point;
145 } // namespace gnash
147 #endif // GNASH_POINT2DH
149 // Local Variables:
150 // mode: C++
151 // indent-tabs-mode: t
152 // End: