Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [QT][QML][C++] Draw an interpolated polygon using C++ painter
Forum Updated to NodeBB v4.3 + New Features

[QT][QML][C++] Draw an interpolated polygon using C++ painter

Scheduled Pinned Locked Moved Unsolved General and Desktop
15 Posts 3 Posters 1.5k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Shadow.01

    Hi all!

    in my QT / QML application the following C++ class works perfectly and draw a simple polygon formed by six elements in a QML view.

    Now, I want an interpolated polygon such that I don't see a series of lines with edges but a continous line without any edge. How can I transform my class in order to do that?

    Important: I don't use a chart but a normal graph figure; due to this reason, I think that the QTSpline series I already found in some forum are not the solution. But I am obviously opened to any way.

    Important: the line, as in figure, should touch all the edge. Otherwise, the result is not what I want. Due to this reason, the Bezier algorytm is not correct.

    Edit: at the end of the post I explained by putting an image what result I would obtain; thanks again.

    Thank you so much!

    #include "mydiagram.h"
    #include <QPainter>
    #include <string>
    #include <iostream>
    #include <QtCharts>
    #include <QSplineSeries>
    #include <QPoint>
    
    using namespace QtCharts;
    
    
    MyDiagram::MyDiagram(QQuickItem *parent): QQuickPaintedItem(parent)
    {
    
    }
    
    void MyDiagram::paint(QPainter *painterMyDiagram) {
    
        QBrush myBrush(QColor("transparent"));
        QPen myPen(QColor("grey"), 3, Qt::DashDotLine);
        painterMyDiagram->setBrush(myBrush);
        painterMyDiagram->setPen(myPen);
        painterMyDiagram->setRenderHint(QPainter::Antialiasing);
        
        static const QPointF points[6] = {
            QPointF(10.0, 80.0),
            QPointF(20.0, 10.0),
            QPointF(200.0, 30.0),
            QPointF(300.0, 160.0),
            QPointF(250.0, 250.0),
            QPointF(10.0, 80.0)
        };
    
        painterMyDiagram->drawPolyline(points, 6);
    }
    

    Notebook 7 - page 1.png

    Pl45m4P Offline
    Pl45m4P Offline
    Pl45m4
    wrote on last edited by Pl45m4
    #5

    @Shadow-01

    Something like spline interpolation?!

    This example is for QChart but maybe you can use spline interpolation for your polygon.

    • https://doc.qt.io/qt-5/qsplineseries.html

    Wiki:
    https://en.wikipedia.org/wiki/Spline_interpolation


    If debugging is the process of removing software bugs, then programming must be the process of putting them in.

    ~E. W. Dijkstra

    S 1 Reply Last reply
    0
    • mrjjM mrjj

      @Shadow-01
      Hi
      super with picture.
      But did you try the RoundedPolygon class from the link?

      It does sort of round the edges of a polygon so might look ok and hits the points.

      S Offline
      S Offline
      Shadow.01
      wrote on last edited by
      #6

      @mrjj Thanks; rounded polygon doesn't exist under the item that QT creator suggested me in the list of figure I can draw; should I import something special? Or, have you an example?

      Thanks again for your time!

      mrjjM 1 Reply Last reply
      0
      • Pl45m4P Pl45m4

        @Shadow-01

        Something like spline interpolation?!

        This example is for QChart but maybe you can use spline interpolation for your polygon.

        • https://doc.qt.io/qt-5/qsplineseries.html

        Wiki:
        https://en.wikipedia.org/wiki/Spline_interpolation

        S Offline
        S Offline
        Shadow.01
        wrote on last edited by Shadow.01
        #7

        @Pl45m4 Thanks but not; this is not the way; as told in my question, I already tried but it works only in a chart. And I should draw a free polygon outside a chart. Any other idea?

        (No, SPline in a drawPolyline give an error; only under a chart I can use it).

        Pl45m4P 1 Reply Last reply
        0
        • S Shadow.01

          @mrjj Thanks; rounded polygon doesn't exist under the item that QT creator suggested me in the list of figure I can draw; should I import something special? Or, have you an example?

          Thanks again for your time!

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #8

          @Shadow-01 said in [QT][QML][C++] Draw an interpolated polygon using C++ painter:

          thanks; rounded polygon doesn't exist under the item that QT creator suggested me in the list of figure I can draw; should I import something

          Yes, the link has a that class in the bottom. ITs not a default Qt one.

          The effect sounds like what you want.

          alt text

          but might end being too round. I have not tried it with a shape like yours.

          S 2 Replies Last reply
          1
          • mrjjM mrjj

            @Shadow-01 said in [QT][QML][C++] Draw an interpolated polygon using C++ painter:

            thanks; rounded polygon doesn't exist under the item that QT creator suggested me in the list of figure I can draw; should I import something

            Yes, the link has a that class in the bottom. ITs not a default Qt one.

            The effect sounds like what you want.

            alt text

            but might end being too round. I have not tried it with a shape like yours.

            S Offline
            S Offline
            Shadow.01
            wrote on last edited by Shadow.01
            #9

            @mrjj Wait, for sure the line touch the edge? It seems not. It is Bezier algorythm? From the page, it seems so..

            1 Reply Last reply
            0
            • mrjjM mrjj

              @Shadow-01 said in [QT][QML][C++] Draw an interpolated polygon using C++ painter:

              thanks; rounded polygon doesn't exist under the item that QT creator suggested me in the list of figure I can draw; should I import something

              Yes, the link has a that class in the bottom. ITs not a default Qt one.

              The effect sounds like what you want.

              alt text

              but might end being too round. I have not tried it with a shape like yours.

              S Offline
              S Offline
              Shadow.01
              wrote on last edited by Shadow.01
              #10

              @mrjj Thanks but for sure is not the way. My solution should absolutely touch the edge (given points), otherwise the line is not correct. Other solutions? Thanks for your time!

              mrjjM 1 Reply Last reply
              0
              • S Shadow.01

                @mrjj Thanks but for sure is not the way. My solution should absolutely touch the edge (given points), otherwise the line is not correct. Other solutions? Thanks for your time!

                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #11

                @Shadow-01
                Hi
                but look at the airplane. it does touch the edges.
                Outside of such rounded polygon im not sure what to use.

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  Shadow.01
                  wrote on last edited by
                  #12

                  No, unfortunately don't touch. If you see the internal vertical segment of each engine, is for sure longer in the left one. Anyway, thanks. :-(

                  1 Reply Last reply
                  0
                  • S Shadow.01

                    @Pl45m4 Thanks but not; this is not the way; as told in my question, I already tried but it works only in a chart. And I should draw a free polygon outside a chart. Any other idea?

                    (No, SPline in a drawPolyline give an error; only under a chart I can use it).

                    Pl45m4P Offline
                    Pl45m4P Offline
                    Pl45m4
                    wrote on last edited by Pl45m4
                    #13

                    @Shadow-01 said in [QT][QML][C++] Draw an interpolated polygon using C++ painter:

                    (No, SPline in a drawPolyline give an error; only under a chart I can use it).

                    Read my answer again.
                    I know that you can't use QSplineSeries outside of QChart but you should take a took at it (spline interpolation) and the Wikipedia page, I linked.

                    If there's nothing like this in Qt you have to draw it yourself using mathematics.


                    If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                    ~E. W. Dijkstra

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      Shadow.01
                      wrote on last edited by
                      #14

                      Thanks all so much. I found a solution, by using the Bezier Algorithm with a really small curve factor number. The result is almost the same as desired.

                      mrjjM 1 Reply Last reply
                      2
                      • S Shadow.01

                        Thanks all so much. I found a solution, by using the Bezier Algorithm with a really small curve factor number. The result is almost the same as desired.

                        mrjjM Offline
                        mrjjM Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on last edited by
                        #15

                        @Shadow-01
                        You could show an image of the final result :) 🙄
                        So did you use code from link or did you make some yourself ?

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved