home *** CD-ROM | disk | FTP | other *** search
/ Hackers Magazine 57 / CdHackersMagazineNr57.iso / Software / Multimedia / k3d-setup-0.7.11.0.exe / include / k3d / k3dsdk / point2.h < prev    next >
C/C++ Source or Header  |  2008-11-07  |  4KB  |  201 lines

  1. #ifndef K3DSDK_POINT2_H
  2. #define K3DSDK_POINT2_H
  3.  
  4. // K-3D
  5. // Copyright (c) 1995-2007, Timothy M. Shead
  6. //
  7. // Contact: tshead@k-3d.com
  8. //
  9. // This library is free software; you can redistribute it and/or
  10. // modify it under the terms of the GNU General Public
  11. // License as published by the Free Software Foundation; either
  12. // version 2 of the License, or (at your option) any later version.
  13. //
  14. // This library is distributed in the hope that it will be useful,
  15. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  17. // General Public License for more details.
  18. //
  19. // You should have received a copy of the GNU General Public
  20. // License along with this library; if not, write to the Free Software
  21. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  22.  
  23. /****************************************************************
  24. *
  25. * C++ Vector and Matrix Algebra routines
  26. * Author: Jean-Francois DOUE
  27. * Version 3.1 --- October 1993
  28. *
  29. ****************************************************************/
  30.  
  31. //
  32. //    From "Graphics Gems IV / Edited by Paul S. Heckbert
  33. //    Academic Press, 1994, ISBN 0-12-336156-9
  34. //    "You are free to use and modify this code in any way
  35. //    you like." (p. xv)
  36. //
  37. //    Modified by J. Nagle, March 1997
  38. //    -    All functions are inline.
  39. //    -    All functions are const-correct.
  40. //    -    All checking is via the standard "assert" macro.
  41. //
  42.  
  43. // Modified by Tim Shead for use with K-3D, January 1998
  44.  
  45. #include "almost_equal.h"
  46. #include "result.h"
  47.  
  48. #include <boost/io/ios_state.hpp>
  49. #include <iomanip>
  50.  
  51. namespace k3d
  52. {
  53.  
  54. class vector2;
  55.  
  56. /////////////////////////////////////////////////////////////////////////////
  57. // point2
  58.  
  59. /// Encapsulates a location in two-dimensional space
  60. class point2
  61. {
  62. public:
  63.     /// Stores the point values
  64.     double n[2];
  65.  
  66.     point2()
  67.     {
  68.         n[0] = n[1] = 0.0;
  69.     }
  70.     
  71.     point2(const double x, const double y)
  72.     {
  73.         n[0] = x;
  74.         n[1] = y;
  75.     }
  76.  
  77.     point2(const point2& v)
  78.     {
  79.         n[0] = v.n[0];
  80.         n[1] = v.n[1];
  81.     }
  82.  
  83.     /// Addition of a vector
  84.     point2& operator+=(const vector2& v);
  85.  
  86.     /// Subtraction of a vector
  87.     point2& operator-=(const vector2& v);
  88.  
  89.     /// Multiplication by a constant
  90.     point2& operator*=(const double d)
  91.     {
  92.         n[0] *= d;
  93.         n[1] *= d;
  94.         return *this;
  95.     }
  96.     
  97.     /// Division by a constant
  98.     point2& operator/=(const double d)
  99.     {
  100.         return_val_if_fail(d, *this);
  101.  
  102.         double d_inv = 1./d;
  103.         n[0] *= d_inv;
  104.         n[1] *= d_inv;
  105.  
  106.         return *this;
  107.     }
  108.  
  109.     /// Returns an indexed dimension by reference
  110.     double& operator[](int i)
  111.     {
  112.         return n[i];
  113.     }
  114.  
  115.     /// Returns an indexed dimension by value
  116.     double operator[](int i) const
  117.     {
  118.         return n[i];
  119.     }
  120.  
  121.     friend std::ostream& operator<<(std::ostream& Stream, const point2& RHS)
  122.     {
  123.         boost::io::ios_flags_saver stream_state(Stream);
  124.         Stream << std::setprecision(17) << RHS.n[0] << " " << RHS.n[1];
  125.         return Stream;
  126.     }
  127.  
  128.     friend std::istream& operator>>(std::istream& Stream, point2& RHS)
  129.     {
  130.         Stream >> RHS.n[0] >> RHS.n[1];
  131.         return Stream;
  132.     }
  133. };
  134.  
  135. /// Negation
  136. inline const point2 operator-(const point2& a)
  137. {
  138.     return point2(-a.n[0], -a.n[1]);
  139. }
  140.  
  141. /// Addition
  142. /** \note We implement this as a convenience for the benefit of linear interpolation - adding two points has no geometric meaning */
  143. inline const point2 operator+(const point2& a, const point2& b)
  144. {
  145.     return point2(a.n[0] + b.n[0], a.n[1] + b.n[1]);
  146. }
  147.  
  148. /// Multiplication by a constant
  149. inline const point2 operator*(const point2& a, const double d)
  150. {
  151.     return point2(d * a.n[0], d * a.n[1]);
  152. }
  153.  
  154. /// Multiplication by a constant
  155. inline const point2 operator*(const double d, const point2& a)
  156. {
  157.     return a * d;
  158. }
  159.  
  160. /// Division by a constant
  161. inline const point2 operator/(const point2& a, const double d)
  162. {
  163.     return_val_if_fail(d, point2());
  164.  
  165.     double d_inv = 1./d;
  166.     return point2(a.n[0]*d_inv, a.n[1]*d_inv);
  167. }
  168.  
  169. /// Equality
  170. inline const bool operator==(const point2& a, const point2& b)
  171. {
  172.     return a.n[0] == b.n[0] && a.n[1] == b.n[1];
  173. }
  174.  
  175. /// Non-equality
  176. inline const bool operator!=(const point2& a, const point2& b)
  177. {
  178.     return !(a == b);
  179. }
  180.  
  181. /// Specialization of almost_equal that tests two point2 objects for near-equality
  182. template<>
  183. class almost_equal<point2>
  184. {
  185.     typedef point2 T;
  186. public:
  187.     almost_equal(const boost::uint64_t Threshold) : threshold(Threshold) { }
  188.     inline const bool operator()(const T& A, const T& B) const
  189.     {
  190.         return std::equal(A.n, A.n + 2, B.n, almost_equal<double>(threshold));
  191.     }
  192.  
  193. private:
  194.     const boost::uint64_t threshold;
  195. };
  196.  
  197. } // namespace k3d
  198.  
  199. #endif // !K3DSDK_POINT2_H
  200.  
  201.