1 // Point2d template - for gnash
3 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Free Software
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.
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.
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
28 #include <cmath> // for sqrt()
29 #include <boost/cstdint.hpp>
36 /// A point which contains a x and a y coorinate in TWIPS.
43 boost::int32_t x
; // TWIPS
46 boost::int32_t y
; // TWIPS
48 /// Construct a Point2d with default x and y coordinates
55 /// Construct a Point2d with given x and y ordinates
56 Point2d(boost::int32_t cx
, boost::int32_t 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
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
79 Point2d
& setTo(const boost::int32_t cx
, const boost::int32_t cy
)
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
92 /// @return a reference to this instance
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
);
101 /// Return square distance between two given points.
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
);
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
;
147 #endif // GNASH_POINT2DH
151 // indent-tabs-mode: t