用向量编写算法的问题

发布于 2024-12-10 19:16:36 字数 1667 浏览 0 评论 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 = "..." 使用 vectorvector 的二进制表达式存在问题。

该算法的作用是在两个图 x(t)y(t) 上画一条线,然后获取 x(t) code> 的 y 坐标并将其转换为新的向量。之后的第二段时间,它需要将 x(t)x 坐标与 y(t) 进行比较 x 坐标来获取 y 坐标。当x(t)的x和y(t) x不匹配时,需要执行yVal = 算法。

有人可以帮我将伪代码转换为工作代码吗?干杯

-(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();
}

Above is my pseudo code for what I'd like to have happen. I am still having issues understanding vectors. As you can see above with the yVal = "..." theres an issue with binary expressions with vector<CGPoint> and vector<double>.

what this algorithm is supposed to do is take a drawn line on two graphs x(t) and y(t) then grabs the x(t)'s y coords and turned it into a new vector. After that in the second while, it takes the x(t)'s x coords to compare to the y(t) x coords to grab y coords. When x(t)'s x and y(t) x don't match it needs to do the yVal = algorithm.

Can some one help me turn my pseudo code into working code? Cheers

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

乖乖兔^ω^ 2024-12-17 19:16:36

您的代码中存在一些谜团,但希望这样的内容可以帮助您入门。我删除了指向向量的指针的使用,并添加了一些注释来解释我的更改。

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]);

        }
    }
}

There are some mysteries in your code, but something like this hopefully gets you started. I removed the use of pointers-to-vector and put some comments in line to explain my changes.

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]);

        }
    }
}
找回味觉 2024-12-17 19:16:36

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

yVal 是一个指向双精度向量的点(顺便说一句,你几乎肯定不会使用它)而不是一个值

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

yVal is a point to a vector of doubles (which you almost certainly don't to be using btw) not a value

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文