看看样条插值区间查找函数写的多细腻
生活随笔
收集整理的這篇文章主要介紹了
看看样条插值区间查找函数写的多细腻
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
? ? 優秀的程序猿不僅要有深厚理論基礎,更要有縝密的思維, 一個簡單的函數, 有非常多人都寫不好,為什么,
? ? 不是做不到,不是想不到,? 往往是因為懶而不愿意深入思考.? 有句話叫, 天下大事, 必做于細.
??? 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;
?? ?}
總結
以上是生活随笔為你收集整理的看看样条插值区间查找函数写的多细腻的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【撸啊撸 Docker】搭建 Jenki
- 下一篇: 荣耀MagicPad13续航怎么样 荣耀