zoukankan      html  css  js  c++  java
  • 看看样条插值区间查找函数写的多细腻

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

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


        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;
        }
  • 相关阅读:
    VC++ 利用PDB和dump文件定位问题并进行调试
    MFC限制edit控件的字符输入长度
    VC++ 使用CreateProcess创建新进程
    正则表达式验证HTTP地址是否合法
    C++ _access和_waccess的使用方法
    最后一次谈 VirtualBox的安装方法
    解决/var/log下没有messages文件的问题?
    待续未完- 自己写后台内容管理程序的辅助内容
    php中的正则函数:正则匹配,正则替换,正则分割 所有的操作都不会影响原来的字符串.
    未完待续
  • 原文地址:https://www.cnblogs.com/wzjhoutai/p/6875370.html
Copyright © 2011-2022 走看看