00001 #ifndef NEAREST_PT_H_IS_INCLUDED
00002 #define NEAREST_PT_H_IS_INCLUDED
00003
00004
00005
00006
00007
00008
00009
00010
00011 namespace mlib {
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
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 }
00037
00038 #endif // NEAREST_PT_H_IS_INCLUDED