码迷,mamicode.com
首页 > 其他好文 > 详细

看看样条插值区间查找函数写的多细腻

时间:2014-11-20 12:01:03      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:io   ar   sp   for   on   art   bs   ad   amp   

    优秀的程序员不仅要有深厚理论基础,更要有缜密的思维, 一个简单的函数, 有很多人都写不好,为什么,

    不是做不到,不是想不到,  往往是由于懒而不愿意深入思考.  有句话叫, 天下大事, 必做于细.


    int Spline::findTimeInterval(Number time, int startIndex)

    {
         int length = this->_times.size();
         if (time < this->_times[0] || time > this->_times[length - 1])
         {
              throw  DeveloperError("time is out of range.");
         }

         if (startIndex < 0 || startIndex > length-1)
         {
             throw DeveloperError("length is out of range.");
         }

          // Take advantage of temporal coherence by checking current, next and previous intervals
          // for containment of time.
          if (time >= this->_times[startIndex])
          {
              if (startIndex + 1 < length && time < this->_times[startIndex + 1])
              {
                  return startIndex;
              }
              else if (startIndex + 2 < length && time < this->_times[startIndex + 2])
              {
                  return startIndex + 1;
              }
          }
          else if (startIndex - 1 >= 0 && time >= this->_times[startIndex - 1])
          {
              return startIndex - 1;
          }

          // The above failed so do a linear search. For the use cases so far, the
          // length of the list is less than 10. In the future, if there is a bottle neck,
          // it might be here.

          int i;
          if (time > this->_times[startIndex])
          {
              for (i = startIndex; i < length - 1; ++i) {
                  if (time >= this->_times[i] && time < this->_times[i + 1]) {
                      break;
                  }
              }
          } else {
              for (i = startIndex - 1; i >= 0; --i) {
                  if (time >= this->_times[i] && time < this->_times[i + 1]) {
                      break;
                  }
              }
          }

          if (i == length - 1) {
              i = length - 2;
          }

          return i;
    }

看看样条插值区间查找函数写的多细腻

标签:io   ar   sp   for   on   art   bs   ad   amp   

原文地址:http://blog.csdn.net/zangle260/article/details/41310087

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!