Gnash  0.8.10
Point2d.h
Go to the documentation of this file.
00001 // Point2d template - for gnash
00002 // 
00003 //   Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
00004 //   Free Software Foundation, Inc
00005 // 
00006 // This program is free software; you can redistribute it and/or modify
00007 // it under the terms of the GNU General Public License as published by
00008 // the Free Software Foundation; either version 3 of the License, or
00009 // (at your option) any later version.
00010 // 
00011 // This program is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 // 
00016 // You should have received a copy of the GNU General Public License
00017 // along with this program; if not, write to the Free Software
00018 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00019 
00020 //
00021 // Original author: Sandro Santilli <strk@keybit.net>
00022 //
00023 
00024 #ifndef GNASH_POINT2DH
00025 #define GNASH_POINT2DH
00026 
00027 #include <ostream>
00028 #include <cmath>    // for sqrt()
00029 #include <boost/cstdint.hpp>
00030 
00031 namespace gnash {
00032 namespace geometry { 
00033     
00035 //
00038 class Point2d
00039 {
00040 public:
00041 
00043         boost::int32_t  x;  // TWIPS
00044 
00046         boost::int32_t  y;  // TWIPS
00047 
00049         Point2d()
00050                 :
00051                 x(0), y(0)
00052         {
00053         }
00054 
00056         Point2d(boost::int32_t cx, boost::int32_t cy)
00057                 :
00058                 x(cx), y(cy)
00059         {
00060         }
00061 
00063         //
00068         Point2d(const Point2d& p0, const Point2d& p1, float t)
00069                 :
00070                 x( p0.x + (boost::int32_t)((p1.x - p0.x) * t)),
00071                 y( p0.y + (boost::int32_t)((p1.y - p0.y) * t))
00072         {
00073         }
00074 
00076         //
00079         Point2d& setTo(const boost::int32_t cx, const boost::int32_t cy)
00080         {
00081                 x = cx;  
00082         y = cy;
00083                 return *this;
00084         }
00085 
00087         //
00094         Point2d& setTo(const Point2d& p0, const Point2d& p1, float t)
00095         {
00096                 x = p0.x + (boost::int32_t)((p1.x - p0.x) * t);
00097                 y = p0.y + (boost::int32_t)((p1.y - p0.y) * t);
00098                 return *this;
00099         }
00100 
00102         static
00103         boost::int64_t squareDistance(const Point2d& p0, const Point2d& p1)
00104         {
00105                 boost::int64_t hside = p1.x - p0.x;
00106                 boost::int64_t vside = p1.y - p0.y;
00107 
00108                 return hside*hside + vside*vside;
00109         }
00110 
00112         boost::int64_t squareDistance(const Point2d& p) const
00113         {
00114                 return squareDistance(*this, p);
00115         }
00116 
00118         boost::int32_t distance(const Point2d& p) const
00119         {
00120             return (boost::int32_t)( std::sqrt( static_cast<double>(squareDistance(p)) ) );
00121         }
00122 
00123         bool operator== (const Point2d& p) const
00124         {
00125                 return (x == p.x) && (y == p.y);
00126         }
00127 
00128         bool operator!=(const Point2d& p) const
00129         {
00130                 return ! (*this == p);
00131         }
00132 };
00133 
00135 inline std::ostream&
00136 operator<< (std::ostream& os, const Point2d& p)
00137 {
00138         return os << "Point2d(" << p.x << "," << p.y << ")";
00139 }
00140 
00141 } // namespace gnash::geometry
00142 
00143 typedef geometry::Point2d  point;
00144 
00145 } // namespace gnash
00146 
00147 #endif // GNASH_POINT2DH
00148 
00149 // Local Variables:
00150 // mode: C++
00151 // indent-tabs-mode: t
00152 // End: