3

I'm developing a simulator gui in which the user clicks on different points of the map and the program connects these points to each other however the connection should be somehow curved (but preferably the curve should pass from the given points) I can't find a decent way to implement this.

A similar solution which I could not figure out

I have seen similar problems and often they are solved using QPainterPath or implementing a bezier curve. Or should I just compute the control points of the bezier curve (if so, how?) ?

Any help would be appreciated, Thank you in advance

Community
  • 1
  • 1
Vahid Nateghi
  • 576
  • 5
  • 14

2 Answers2

1

A cubic Bézier curve consists of 4 points: Start, End, Control1 and Control2. Depending on the two control points the curve can have different shapes. Since you don't have the control points you should calculate them in some way.

This gives a nice description of how to calculate the control points if you only know the start point, end point and one other point on the curve. In your case the point on the curve could be the mid point between start and end.

Nejat
  • 31,784
  • 12
  • 106
  • 138
1
void Beziertest::Bezier2D(QList<QPoint> points)
{
    QImage area(600,700,QImage::Format_RGB32);
    int n=points.length()-1;
    for(double u = 0.0 ; u <= 1.0 ; u += 0.001)
    {
        //calculate x coordinate 
        double xu=0.0;
        for (int i = n; i >= 0; i--) {
            xu+=points[i].x()*((factorial(n)/(factorial(i)*factorial((n-i))))*pow(u,i)*pow((1-u),(n-i)));
        }

        //calculate y coordinate 
        double yu=0.0;
        for (int i = n; i >= 0; i--) {
            yu+=points[i].y()*((factorial(n)/(factorial(i)*factorial((n-i))))*pow(u,i)*pow((1-u),(n-i)));
        }

        area.setPixel((int)xu , (int)yu , deger);
        setPixmap(QPixmap::fromImage(res));//set image to label
    }
}
Zekeriya Akgül
  • 308
  • 4
  • 14