Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

nearest_pt.H

Go to the documentation of this file.
00001 #ifndef NEAREST_PT_H_IS_INCLUDED
00002 #define NEAREST_PT_H_IS_INCLUDED
00003 
00004 /*!
00005  *  \file nearest_pt.H
00006  *  \brief Contains the nearest_pt_to_line_seg function.
00007  *  \ingroup group_MLIB
00008  *
00009  */
00010 
00011 namespace mlib {
00012 
00013 //! \brief Returns the nearest point on the line segment (\p st,\p en) to the
00014 //! point, \p pt.
00015 //!
00016 //! The template paramenter \p P should be some sort of point class.
00017 //!
00018 //! \param[in] pt The point to find the nearest point on the line segment to.
00019 //! \param[in] st The starting endpoint of the line segment.
00020 //! \param[in] en The ending endpoint of the line segment.
00021 //!
00022 //! \return The point on the line segment from \p st to \p en that is nearest to
00023 //! the point \p pt.
00024 template <class P>
00025 inline P
00026 nearest_pt_to_line_seg(const P &pt, const P &st, const P &en)
00027 {
00028    
00029    P npt = st + (en-st)*((en-st)*(pt-st)/(en-st).length_sqrd());
00030    if ((npt - st) * (npt - en) < 0)
00031       return npt;
00032    return ((pt-st).length_sqrd()<(pt-en).length_sqrd()) ? st : en;
00033    
00034 }
00035 
00036 } // namespace mlib
00037     
00038 #endif // NEAREST_PT_H_IS_INCLUDED

Generated on Mon Sep 18 11:39:32 2006 for jot by  doxygen 1.4.4