core/vgl/vgl_line_segment_2d.txx
Go to the documentation of this file.
00001 // This is core/vgl/vgl_line_segment_2d.txx
00002 #ifndef vgl_line_segment_2d_txx_
00003 #define vgl_line_segment_2d_txx_
00004 
00005 #include "vgl_line_segment_2d.h"
00006 #include <vcl_iostream.h>
00007 #include <vcl_cmath.h>
00008 // stream operators
00009 template <class Type>
00010 vcl_ostream& operator<<(vcl_ostream& s, vgl_line_segment_2d<Type> const & p)
00011 {
00012   return s << "<vgl_line_segment_2d " << p.point1() << " to " << p.point2() << " >";
00013 }
00014 
00015 template <class Type>
00016 vcl_istream& operator>>(vcl_istream& s, vgl_line_segment_2d<Type>& p)
00017 {
00018   vgl_point_2d<Type> p1, p2;
00019   s >> p1 >> p2;
00020   p.set(p1, p2);
00021   return s;
00022 }
00023 
00024 template <class Type>
00025 Type vgl_line_segment_2d<Type>::a() const
00026 {
00027   return point1_.y()-point2_.y();
00028 }
00029 
00030 template <class Type>
00031 Type vgl_line_segment_2d<Type>::b() const
00032 {
00033   return point2_.x()-point1_.x();
00034 }
00035 
00036 template <class Type>
00037 Type vgl_line_segment_2d<Type>::c() const
00038 {
00039   return point1_.x()*point2_.y()-point2_.x()*point1_.y();
00040 }
00041 
00042 template <class Type>
00043 vgl_vector_2d<Type>  vgl_line_segment_2d<Type>::direction() const
00044 {
00045   vgl_vector_2d<Type> v(point2_.x()-point1_.x(),point2_.y()-point1_.y());
00046   return normalized(v);
00047 }
00048 
00049 template <class Type>
00050 vgl_vector_2d<Type>  vgl_line_segment_2d<Type>::normal() const
00051 {
00052   vgl_vector_2d<Type> v(point1_.y()-point2_.y(),point2_.x()-point1_.x());
00053   return normalized(v);
00054 }
00055 
00056 template <class Type>
00057 double vgl_line_segment_2d<Type>::slope_degrees() const
00058 {
00059   static const double deg_per_rad = 45.0/vcl_atan2(1.0,1.0);
00060   double dy = point2_.y()-point1_.y();
00061   double dx = point2_.x()-point1_.x();
00062   // do special cases separately, to avoid rounding errors:
00063   if (dx == 0) return dy<0 ? -90.0 : 90.0;
00064   if (dy == 0) return dx<0 ? 180.0 : 0.0;
00065   if (dy == dx) return dy<0 ? -135.0 : 45.0;
00066   if (dy+dx == 0) return dy<0 ? -45.0 : 135.0;
00067   // general case:
00068   return deg_per_rad * vcl_atan2(dy,dx);
00069 }
00070 
00071 template <class Type>
00072 double vgl_line_segment_2d<Type>::slope_radians() const
00073 {
00074   double dy = point2_.y()-point1_.y();
00075   double dx = point2_.x()-point1_.x();
00076   return vcl_atan2(dy,dx);
00077 }
00078 
00079 #undef VGL_LINE_SEGMENT_2D_INSTANTIATE
00080 #define VGL_LINE_SEGMENT_2D_INSTANTIATE(Type) \
00081 template class vgl_line_segment_2d<Type >;\
00082 template vcl_istream& operator>>(vcl_istream&, vgl_line_segment_2d<Type >&);\
00083 template vcl_ostream& operator<<(vcl_ostream&, vgl_line_segment_2d<Type > const&)
00084 
00085 #endif // vgl_line_segment_2d_txx_