2011-10-19 10 views
0
-(void)userShow{ 
    vector<CGPoint>::iterator it; 
    vector<CGPoint>* xp = x.graphPoints; 
    vector<CGPoint>* yp = y.graphPoints; 
    xVal = new vector<double>(); 
    yVal = new vector<double>(); 
    xyVal = new vector<double>(); 
    xxVal = new vector<double>(); 
    value = new vector<double>(); 
    c = new vector<double>(); 


    for(it = xp->begin(); it != xp->end(); ++it){ 
     xVal->push_back(it->y); 
     xxVal->push_back(it->x); 

    } 
    for(it = yp->begin(); it != yp->end(); ++it){ 
     xyVal->push_back(it->x); 
    } 

    for (int i = 0; i < xVal->size(); i++){ 
     c = xVal[i]; 
     while (xyVal[c] < xxVal[i];){ 
      c++; 
      if ((c-1)<=xxVal[i]<=c){ 
       double value = xp[c-1] + (xp[c] - yp[c-1])*(xxVal[i] - xyVal[c-1])/(xyVal[c] - xyVal[c-1]); 
      } 
      yVal->push_back(value); 
     } 
    } 

    UserGraph->removeAllData(); 
    UserGraph->addDataSet(xVal, yVal, [UIColor redColor], 0, false, true, 2); 
    UserGraph->updateAll(); 
} 

上記は私の擬似コードです。私はまだベクトルを理解する問題を抱えています。上記のように、yVal = "..."はvector<CGPoint>vector<double>というバイナリ式の問題です。このアルゴリズムは、二つのグラフx(t)y(t)に引かれた線を取るされて行うことになっているものベクトルを使用したアルゴリズムの作成に関する問題

はその後x(t)y COORDSをつかみ、新しいベクトルにそれを回しました。その後、2回目には、コードを取るためにy(t)xのコードと比較すると、のコードがx(t)のコードを取ります。 x(t)のxとy(t)xが一致しない場合は、yVal =アルゴリズムを実行する必要があります。

擬似コードを作業コードに変えるのに役立つものはありますか?乾杯

+0

'x.graphPoints'は何を返しますか? – birryree

+0

最初のグラフ上のx(t)の点。 @birryree –

+0

なぜ 'std :: vector'で' new'を使用していますか? 「新しい」を避ける。 – Nawaz

答えて

2

あなたのコード内のいくつかの謎がありますが、何かこれはうまくいけばあなたを始める。私はポインタの使用を取り除きました - vectorと私の変更を説明するためにいくつかのコメントを並べてください。

void userShow() { 

    // I assume that x.graphPoints is just some `std::vector<CGPoint>` and you just want to use it locally 

    // if x.graphPoints returns an "std::vector<CGPoint> *" (pointer-to-vector), 
    // you should probably modify the class/struct/whatever to just use the vector, 
    // not a pointer-to-vector 
    vector<CGPoint>& xp = x.graphPoints; 

    // ditto for y.graphPoints 
    vector<CGPoint>& yp = y.graphPoints; 

    // You almost never use pointers to containers, nor allocate them with new - 
    // it's an atypical practice in C++ 
    /* 
    xVal = new vector<double>(); 
    yVal = new vector<double>(); 
    xyVal = new vector<double>(); 
    */ 

    // instead just create the vectors on the stack 
    std::vector<double> xVal, yVal, xyVal; 

    std::vector<CGPoint>::iterator it; 

    // These have been changed to not use -> member notation, since we're not 
    // using pointers anymore 
    for(it = xp.begin(); it != xp.end(); ++it){ 
     xVal.push_back(it->y); 
     xxVal.push_back(it->x); // I have no idea what xxVal is? I think it's xyVal? 
     // xyVal.push_back(it->x); // like this? 
    } 

    // you can iterate through a vector with this type of loop, or 
    // use an iterator as above 
    for (int i = 0; i < xp.size(); ++i){ 
     int c = 1; 
     while (xyVal[c] < xxVal[i]) { 
      ++c; 

      // (c-1)<=xxVal[i]<=c; // ??? 

      // I think the previous line means...c gets the value of xyVal[i], and 
      // xxVal gets c-1? You'll have to explain this, otherwise it it just a 
      // free-standing conditional comparison 

      // EDIT: I think I understand what you mean 
      // this was a conditional check to do the yVal stuff 
      /** 
      if ((c-1) <= xxVal[i] && xxVal[i] <= c) { 
       // yVal = xp[c-1] + (xp[c] - yp[c-1])*(xxVal[i]-xyVal[c-1])/(xyVal[c] - xyVal[c-1]); 
      } */ 

      // as mentioned, yVal is a vector, so what do you want? 
      yVal = xp[c-1] + (xp[c] - yp[c-1])*(xxVal[i]-xyVal[c-1])/(xyVal[c] - xyVal[c-1]); 

     } 
    } 
} 
+0

このコメントは完璧です。ありがとう@birryree。私はあなたがすべてをコメントしてうれしいです。自分自身でこのコードを読んだり、報告したりできるかどうかを見てみましょう。 –

+0

birryree一緒にチャットルームを開くことはできますか? –

+0

@JohnRiselvato - 私は信じられないほど使えるとは思っていませんが、チャットを開いて私を招待します。私は現在、私の応答時間が少し遅くなるまで働いています。 – birryree

1

yVal = xp[c-1] + ....

yvalを指定は、(あなたはほぼ確実にところで使用することはありません)doubleのベクトルをポイントでない値

関連する問題