如何实现4个控制点的Neville算法得到非双射多项式?

时间:2014-10-25 00:30:55

标签: algorithm processing

我正在准备一篇关于内维尔算法如何作为数值分析类的分配的简短介绍,因为它是计算机科学中广泛用于通过几个控制点进行插值的算法,我认为实现它是一个好主意用于演示。现在,我选择了4个控制点(使用Processing语言),这就是我得到的:bijective polynome

虽然我想得到这个(取自jsxgraph网站):

non-bijective polynome

我的绘图功能如下:

//Draggable is the class which defines x,y and r of a point and contains methods Draggable::coordx() and Draggable::coordy()
//for fetching x and y of a point, nizd below is an array of points, consider them to be already set on stage when Neville() is called

Draggable[] nizd = new Draggable[4];
float[][] P = new float[4][4];  //matrix used for dynamic solution

void Neville () {
   for (int i=0; i<3; i++)                        //this is where point are sorted according 
     for (int j=i+1; j<4; j++)                    //to their x-coordinate in ascending order
       if (nizd[j].coordx()<nizd[i].coordx()) {   //just in case they overlap
         Draggable temp = nizd[i];                //if I haven't done it, polynome would just
         nizd[i] = nizd[j];                       //not be plotted between not sorted points
         nizd[j] = temp;                          //and that's what I don't want
       }

   float pretx, prety; // variables used for memorizing coordinates of a previous pixel

   for (int i=0; i<4; i++) //setting the first column of the matrix with known y-coordinates
      P[i][0] = nizd[i].coordy();

   pretx = nizd[0].coordx();
   prety = nizd[0].coordy();

   for (int x = nizd[0].coordx(); x<= nizd[3].coordx(); x++) {    //loop that calculates the matrix P for every pixel between all 4 points
      for (int i = 1; i < 4; i++) 
        for (int j = 1; j <= i; j++) 
          P[i][j] = ((x*1.0 - nizd[i - j].coordx())*(P[i][j - 1]) 
                    - (x*1.0 - nizd[i].coordx())*(P[i - 1][j - 1]))/(nizd[i].coordx()*1.0 -         nizd[i - j].coordx());

   line (pretx, prety, x, P[3][4]);
   pretx = x;
   prety = P[3][5];
   }

}

所以,我希望得到第二个类似绳索的多项式,保持点的顺序,而不需要反复排序。我将不胜感激任何帮助。谢谢!

0 个答案:

没有答案