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. Copying and rotating a polygon
Forum Updated to NodeBB v4.3 + New Features

Copying and rotating a polygon

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 335 Views 1 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 Offline
    S Offline
    serebryakov
    wrote on last edited by
    #1

    Hi all. I need to draw a wind turbine for later manipulations with it:
    Screenshot 2023-03-27 at 20.38.55.png
    So far I've got something like this:

    void Widget::paintEvent(QPaintEvent *event)
    {
        QPainter painter(this);
        painter.setPen(Qt::white);
        painter.setBrush(Qt::NoBrush);
    
        painter.drawRect(362, 500, 55, 25);
    
        painter.drawLine(QPointF(372, 500), QPointF(382, 250));
        painter.drawLine(QPointF(392, 250), QPointF(407, 500));
    
        painter.drawEllipse(QPoint(387, 235), 15, 15);
    
        const QPointF vane1Points[5] = {
            QPointF(382, 220),
            QPointF(367, 190),
            QPointF(382, 90),
            QPointF(397, 75),
            QPointF(392, 220)
        };
        painter.drawPolygon(vane1Points, 5);
    
        QWidget::paintEvent(event);
    }
    

    Result:
    Screenshot 2023-03-27 at 20.42.19.png

    I just can't seem to find the coordinates for the other two blades. Is there some way to copy an existing polygon and rotate it? For example, 90 degrees counterclockwise for the blade on the left.

    SGaistS 1 Reply Last reply
    0
    • S serebryakov

      Hi all. I need to draw a wind turbine for later manipulations with it:
      Screenshot 2023-03-27 at 20.38.55.png
      So far I've got something like this:

      void Widget::paintEvent(QPaintEvent *event)
      {
          QPainter painter(this);
          painter.setPen(Qt::white);
          painter.setBrush(Qt::NoBrush);
      
          painter.drawRect(362, 500, 55, 25);
      
          painter.drawLine(QPointF(372, 500), QPointF(382, 250));
          painter.drawLine(QPointF(392, 250), QPointF(407, 500));
      
          painter.drawEllipse(QPoint(387, 235), 15, 15);
      
          const QPointF vane1Points[5] = {
              QPointF(382, 220),
              QPointF(367, 190),
              QPointF(382, 90),
              QPointF(397, 75),
              QPointF(392, 220)
          };
          painter.drawPolygon(vane1Points, 5);
      
          QWidget::paintEvent(event);
      }
      

      Result:
      Screenshot 2023-03-27 at 20.42.19.png

      I just can't seem to find the coordinates for the other two blades. Is there some way to copy an existing polygon and rotate it? For example, 90 degrees counterclockwise for the blade on the left.

      SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Why not rotate the painter and repaint the polygon you have already defined ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      S 1 Reply Last reply
      1
      • SGaistS SGaist

        Hi,

        Why not rotate the painter and repaint the polygon you have already defined ?

        S Offline
        S Offline
        serebryakov
        wrote on last edited by
        #3

        @SGaist You mean something like that?

        void Widget::paintEvent(QPaintEvent *event)
        {
            QPainter painter(this);
            painter.setPen(Qt::white);
            painter.setBrush(Qt::NoBrush);
        
            painter.drawRect(362, 500, 55, 25);
        
            painter.drawLine(QPointF(372, 500), QPointF(382, 250));
            painter.drawLine(QPointF(392, 250), QPointF(407, 500));
        
            painter.drawEllipse(QPoint(387, 235), 15, 15);
        
            QPolygonF vane1;
            vane1.append(QPointF(382, 220));
            vane1.append(QPointF(367, 190));
            vane1.append(QPointF(382, 90));
            vane1.append(QPointF(397, 75));
            vane1.append(QPointF(392, 220));
        
            painter.drawPolygon(vane1);
        
            QTransform transform;
            transform.rotate(-90); 
            painter.save(); 
            painter.setTransform(transform);
            painter.drawPolygon(vane1);
            painter.restore(); 
        
            QWidget::paintEvent(event);
        }
        

        It's a little strange because it should work according to Qt documentation, but it doesn't work for me. Result the same. What did I do wrong?

        JoeCFDJ 1 Reply Last reply
        0
        • S serebryakov

          @SGaist You mean something like that?

          void Widget::paintEvent(QPaintEvent *event)
          {
              QPainter painter(this);
              painter.setPen(Qt::white);
              painter.setBrush(Qt::NoBrush);
          
              painter.drawRect(362, 500, 55, 25);
          
              painter.drawLine(QPointF(372, 500), QPointF(382, 250));
              painter.drawLine(QPointF(392, 250), QPointF(407, 500));
          
              painter.drawEllipse(QPoint(387, 235), 15, 15);
          
              QPolygonF vane1;
              vane1.append(QPointF(382, 220));
              vane1.append(QPointF(367, 190));
              vane1.append(QPointF(382, 90));
              vane1.append(QPointF(397, 75));
              vane1.append(QPointF(392, 220));
          
              painter.drawPolygon(vane1);
          
              QTransform transform;
              transform.rotate(-90); 
              painter.save(); 
              painter.setTransform(transform);
              painter.drawPolygon(vane1);
              painter.restore(); 
          
              QWidget::paintEvent(event);
          }
          

          It's a little strange because it should work according to Qt documentation, but it doesn't work for me. Result the same. What did I do wrong?

          JoeCFDJ Offline
          JoeCFDJ Offline
          JoeCFD
          wrote on last edited by JoeCFD
          #4

          @serebryakov
          https://stackoverflow.com/questions/8586088/rotate-rectangle-around-its-center
          https://stackoverflow.com/questions/46179769/qt-rotating-shapes

          1 Reply Last reply
          1

          • Login

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