Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
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
00065 if ( det1 != 0 )
00066 {
00067 CVec2 startToStart = line.start() -m_Start;
00068
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
00081 if ( det1 != 0 )
00082 {
00083 CVec2 startToStart = line.start() -m_Start;
00084
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
00095
00096 str << m_Start.x() << " " << m_Start.y() << std::endl << end().x() << " " << end().y() << std::endl;
00097 return str.str();
00098 }
00099