0

Hello I am a relatively new programmer with C++. I had a question about my code

I have a point 2d class that has a double x and y.

I am trying the following nested loop:

Point2D Dec(float t)
{
    Point2D Temp;
    vector<Point2D>Bcopy=H->B;
    for(int p=0;p<Bcopy.size()-1;p++){
       for(int l=p;l<Bcopy.size();l++){
           Temp=(1-t)*Bcopy.at[p][l-1]+t*Bcopy.at[p+1][l-1];
       }
    }
    return Temp;
}

So essentially there is another class that has a vector with point 2d B and H is the pointer to it. These are storing the points from mouse interaction etc and drawing them. So i just created a copy of it and then did the above nested loop and then I use the points to draw them too.

I keep getting the following two errors:

std::vector<Point2D,std::allocator<-Ty>>::at':function call missing argument list;use'&std::vector<Point2D,std::allocator<_Ty>>:at' to create a pointer to member

and

subscript requires array or pointer.

Both these errors are for the line

Temp=(1-t)*Bcopy.at[p][l-1]+t*Bcopy.at[p+1][l-1]

in the code

I have tried many different things and I either keep getting more errors or just these two. I tried to google and understand the errors but couldn't really. Any help would be much appreciated

Thank you

EDIT After much playing around

I did the following:

vector<2D>NewBcopy; 
 double Rx=0 ,Ry=0; 
 for(int p=0;p<Bcopy.size()-1;p++){ 
 for(int l=p;l<Bcopy.size();l++){ 
 if(l==p)
 {Newcopy.at(l)=Bcopy.at(l); 
 }
 else 
 {Rx=(1-t)*Bcopy.at(p).x+t*Bcopy.at(p+1).x; 
 Ry=(1-t)*Bcopy.at(p).y+t*Bcopy.at(p+1]).y:
 }
 Temp.x=Rx;
 Temp.y=Ry;
 }
 }
 return Temp;
 }
22
  • 4
    BTW, the error message is telling you that at wants round parentheses, not square brackets. Commented Feb 23, 2017 at 16:10
  • Also both your for loops are missing the closing parentheses (after the increments) Commented Feb 23, 2017 at 16:18
  • Sorry for not posting it on stackoverflow. I didn't realize matchexchange and stackoverflow were different. Sorry about that. Commented Feb 23, 2017 at 16:32
  • @UnholySheep I do have the closing parenthesis. I just didn't copy it here by mistake. My bad again Commented Feb 23, 2017 at 16:32
  • 1
    lol... we have a Mathematica user on C++ xD Commented Feb 23, 2017 at 16:35

2 Answers 2

1

You can expand your class representing a 2D point by adding some functions that perform mathematical operations between points and scalars. A minimal example is something like this:

class Point2D
{
public:
    double x, y;

    Point2D(double xx = 0.0, double yy = 0.0) : x{xx}, y{yy} {}
};

Point2D operator*(const Point2D &p, double s)
{
    return Point2D{p.x * s, p.y *s};
}

Point2D operator+(const Point2D &a, const Point2D &b)
{
    return Point2D{a.x + b.x, a.y + b.y};
}

After that, implementing algorithms like the De Castleljau is a bit more easy. The following is a possible (unoptimized) implementation:

Point2D De_Casteljau(std::vector<Point2D> B, double t)
{
    for ( int i = 0; i < B.size() - 1; ++i )
    {
        for ( int j = 0; j < B.size() - 1 - i ; ++j )
        {
            B[j] = B[j] * (1.0 - t) + B[j + 1] * t;
        }
    }
    return B[0];
}

I tested it with this simple program:

#include <iostream>
#include <vector>
#include <iomanip>

//... include here the preceding snippets... 

int main()
{
    using std::setw;
    std::vector<Point2D> B {
        {0,0}, {0,1}, {2,1}, {2,2}
    };

    std::cout << "   t      x       y\n";
    for ( double i = 0; i <= 1.0; i += 0.1 )
    {
        auto p = De_Casteljau(B, i);
        std::cout << std::fixed << std::setprecision(2) << setw(6) << i
                  << std::setprecision(4) << setw(8) << p.x
                  << setw(8) << p.y << '\n';
    }
    return 0;
}

which gave the following results:

   t      x       y
  0.00  0.0000  0.0000
  0.10  0.0560  0.2720
  0.20  0.2080  0.4960
  0.30  0.4320  0.6840
  0.40  0.7040  0.8480
  0.50  1.0000  1.0000
  0.60  1.2960  1.1520
  0.70  1.5680  1.3160
  0.80  1.7920  1.5040
  0.90  1.9440  1.7280
  1.00  2.0000  2.0000
Sign up to request clarification or add additional context in comments.

6 Comments

I like this solution, but unfortunately OP mentioned in the comments that the class Point2D should not be modified (so it should only contain the members x and y)
@UnholySheep Well, I've implemented operator* and operator+ as free functions... It may be enough ;)
@Bob__ I like this solution too but there are other functions and things involved. Plus I have to implement for n number of points. Thank you though. I learned a few things from this code
++i in for statements is no longer required with current compilers... They know what you mean.
@J.H.Bonarius Are you referring to the use of the prefixed version of operator++ instead of the postfixed one? Well, for POD types it has always been a matter of style, rather then a performance concern...
|
1

You should first start learning C++ before you start difficult algorithms. Your (non-working) code should look more like this.

#include <vector>
using std::vector;

struct Point2D
{
    double x, y;
};

Point2D Dec(double t)
{
    Point2D Temp;
    vector<Point2D> Bcopy = H->B; // Fails: what is H?
    for (auto p = 0; p < Bcopy.size() - 1; p++)
    {
        for (auto l = p; l < Bcopy.size(); l++)
        {
            // Fails: Bcopy is not 2-dimensional
            Temp = (1 - t) * Bcopy[p][l - 1] + t * Bcopy[p + 1][l - 1];
        }
    }
    vector<Point2D> NewBcopy;
    double Rx = 0, Ry = 0;
    for (auto p = 0; p < Bcopy.size() - 1; p++)
    {
        for (auto l = p; l < Bcopy.size(); l++)
        {
            if (l == p)
            {
                NewBcopy[l] = Bcopy[l];
            }
            else
            {
                Rx = (1 - t)*Bcopy[p].x + t*Bcopy[p + 1].x;
                Ry = (1 - t)*Bcopy[p].y + t*Bcopy[p + 1].y;
            }
            Temp.x = Rx;
            Temp.y = Ry;
        }
    }
    return Temp;
}

3 Comments

H is a pointer to vector B which is storing all the points from the user. And yes , I plan on learning C++ properly this weekend but the algorithm is from another subject I am learning
@PurCoutinho C++ is definitely not a language you can learn in a weekend (no programming language really is) - it's actually probably one of the harder languages to learn (I've been learning and using C++ for years and I'd still only consider myself to be intermediate at it)
@UnholySheep Haha yah i meant to say starting this weekend. I have been learning it for a while too but not with full focus. I have always used Matlab for everything . But there are many resources online for C++ so I should utilize it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.