Line2D.cpp
Go to the documentation of this file.
00001 /*******************************************************************************
00002  *  Line2D.cpp
00003  *
00004  *  (C) 2007 AG Aktives Sehen <agas@uni-koblenz.de>
00005  *           Universitaet Koblenz-Landau
00006  *
00007  *  Information on Code Review state:
00008  *  Author: SM; DevelTest: Date; Reviewer: Initials; Review: Date; State: NOK
00009  *
00010  *  Additional information:
00011  *  $Id: Line2D.cpp 44313 2011-04-06 22:46:28Z agas $
00012  *******************************************************************************/
00013 
00014 #include <iostream>
00015 #include <sstream>
00016 
00017 #include "Line2D.h"
00018 #include "vec2.h"
00019 
00020 #define THIS Line2D
00021 
00022 float THIS::gradient() const
00023 {
00024   float gradient = 10000000.0;
00025   if ( m_Vec[0] != 0.0 )
00026   {
00027     gradient = m_Vec[1]/m_Vec[0];
00028   }
00029   return gradient;
00030 }
00031 
00032 std::vector< Point2D > THIS::vertices ( unsigned substeps )
00033 {
00034   unsigned steps = substeps+2;
00035   std::vector<Point2D> myVertices ( steps );
00036   for ( unsigned i=0; i<steps; i++ )
00037   {
00038     float t= float ( i ) / float ( steps-1 );
00039     myVertices[i] = m_Start + t*m_Vec;
00040   }
00041   return myVertices;
00042 }
00043 
00044 Point2D THIS::getClosestPoint ( Point2D point ) const
00045 {
00046   float t = ( point-m_Start ) * m_Vec;
00047   t /= m_Vec * m_Vec;
00048   if ( t > 1.0 )
00049   {
00050     t = 1.0;
00051   }
00052   else if ( t < 0.0 )
00053   {
00054     t = 0.0;
00055   }
00056   Point2D pointOnLine = m_Start + ( t * m_Vec );
00057   return pointOnLine;
00058 }
00059 
00060 Point2D THIS::getIntersectionPoint ( Line2D line ) const
00061 {
00062   Point2D intersecPoint;
00063   double det1 = m_Vec.x() * ( -line.vec().y() ) - ( -line.vec().x() ) * m_Vec.y();
00064   // lines are not parallel
00065   if ( det1 != 0 )
00066   {
00067     CVec2 startToStart = line.start() -m_Start;
00068     // calculate intersection
00069     double lambda = ( startToStart.x() * ( -line.vec().y() ) - ( -line.vec().x() ) * startToStart.y() ) / det1;
00070     intersecPoint = m_Start + lambda* m_Vec;
00071   }
00072 
00073   return intersecPoint;
00074 }
00075 
00076 float THIS::getIntersectionPointParameter ( Line2D line ) const
00077 {
00078   double lambda = 0.0;
00079   double det1 = m_Vec.x() * ( -line.vec().y() ) - ( -line.vec().x() ) * m_Vec.y();
00080   // lines are not parallel
00081   if ( det1 != 0 )
00082   {
00083     CVec2 startToStart = line.start() -m_Start;
00084     // calculate intersection
00085     lambda = ( startToStart.x() * ( -line.vec().y() ) - ( -line.vec().x() ) * startToStart.y() ) / det1;
00086   }
00087 
00088   return lambda;
00089 }
00090 
00091 std::string THIS::toString() const
00092 {
00093   std::ostringstream str;
00094 //   str << "Startpoint: " << m_Start.x() << " " << m_Start.y() << " Endpoint: " << end().x() << " " << end().y() <<
00095 //   " Vector: " << m_Vec.x() << " " << m_Vec.y() << " ";
00096   str << m_Start.x() << " " << m_Start.y() << std::endl << end().x() << " " << end().y() << std::endl;
00097   return str.str();
00098 }
00099 


robbie_architecture
Author(s): Viktor Seib
autogenerated on Mon Oct 6 2014 02:53:09