Home > Net >  Switch QPoint to struct in mathematical expression
Switch QPoint to struct in mathematical expression

Time:04-06

Don't understand how QPoint is being calculated here. QPoint has x and y. I've tried running this with a custom struct but I get errors such as Invalid operands to binary expression.

Works:

void test(QPoint p0, QPoint  p1, QPoint  p2, QPoint  p3) {
    QPoint point;

    for(double t = 0.0; t<=1.0; t =0.001){
        point = pow((1-t),3) * p0   3 * pow((1-t),2) * t * p1   3 * (1-t) * pow(t,2) * p2   pow(t,3) * p3;
    }

}

Doesn't work

struct Point {
  int x;
  int y;
};
void test(Point p0, Point p1, Point p2, Point p3) {
    Point point;

    for(double t = 0.0; t<=1.0; t =0.001){
        point = pow((1-t),3) * p0   3 * pow((1-t),2) * t * p1   3 * (1-t) * pow(t,2) * p2   pow(t,3) * p3;
    }

}

Can I get this to work somehow using the Point structure instead of QPoint?

CodePudding user response:

If you look at the Qt documentation about QPoint, you'll see that the operator*() and operator () (that you use here) are overloaded for QPoint.

If you want to make it work with your class, you will need to overload them as well.

The minimum required overloads you need to make your test() function work are:

Point operator*(double factor, const Point & p)
{
    return Point{
                 static_cast<int>(std::round(p.x * factor)),
                 static_cast<int>(std::round(p.y * factor))
                };
}

Point operator (const Point & p1, const Point & p2)
{
    return Point{p1.x   p2.x, p1.y   p2.y};
}

For more consistency, you could add the symetric:

Point operator*(const Point & p, double factor)
{
    return factor * p;
}

Anyway, if you want to replace QPoint with Point with full arithmetic compatibility, I would recommend you to write all the overloads (for arithmetic comparison) listed in the documentation of QPoint.

  •  Tags:  
  • c qt
  • Related