Results 1 to 6 of 6

Thread: star drawing in Star Delegate Example

  1. #1
    Join Date
    Jun 2019
    Location
    France, Pau
    Posts
    60
    Thanks
    32
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Question star drawing in Star Delegate Example

    Hi everyone,

    Can someone explain me how with only 5 points, they manage to draw a star having 10 points ?

    https://doc.qt.io/qt-5/qtwidgets-ite...e-example.html

    Star points :
    Qt Code:
    1. starPolygon << QPointF(1.0, 0.5);
    2. for (int i = 1; i < 5; ++i)
    3. starPolygon << QPointF(0.5 + 0.5 * std::cos(0.8 * i * 3.14),
    4. 0.5 + 0.5 * std::sin(0.8 * i * 3.14));
    To copy to clipboard, switch view to plain text mode 

    Star drawing :
    Qt Code:
    1. painter->drawPolygon(starPolygon, Qt::WindingFill);
    To copy to clipboard, switch view to plain text mode 

    Apparently, it's "Qt::WindingFill" that does the magic but it's not obvious.

    Thanks.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,229
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: star drawing in Star Delegate Example

    Take out a piece of graph paper, and plot the positions of the 5 points (and draw the line between each new point and the previous one) in the order they are inserted into the polygon, then close the polygon with a line from the last point to the first point. Then consider what would happen if you started at the first point and traced around the lines that form the "outside" of the polygon. That's what winding fill does, and how you can define a star with only 5 points.

    It is important that not only do you plot the five points in order, but you also connect the points in the order they are added.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Jun 2019
    Location
    France, Pau
    Posts
    60
    Thanks
    32
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: star drawing in Star Delegate Example

    Hi d_stranz,

    I still don't understand how it works. In a spreadsheet, I computed the points :

    (1, 0.5)
    (0.095866286365345, 0.794407780983897)
    (0.65329623398455, 0.024079560592157)
    (0.656325008329123, 0.974934197306216)
    (0.093998941415384, 0.208172755850022)

    They form a pentagone, how the 5 points inside this pentagone are computed with the winding fill option ?

    Thanks.

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,229
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: star drawing in Star Delegate Example

    Is this the order that the code also computes the points? If it is, plot them in order on a piece of graph paper, and connect point 1 to point 2, 2 to 3, 3 to 4, 4 to 5, and 5 to 1 in the order the code creates them. You will see that the lines that connect the points do not create a pentagon, they create a 5-pointed star.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  5. The following user says thank you to d_stranz for this useful post:

    embeddedmz (27th July 2020)

  6. #5
    Join Date
    Jun 2019
    Location
    France, Pau
    Posts
    60
    Thanks
    32
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: star drawing in Star Delegate Example

    Ahhhhh OK, everything is clear now !

  7. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,229
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: star drawing in Star Delegate Example

    You can use the same trick to create any star-shaped object with 5 or more points. Plot the points around the outside of the polygon, then connect every other pair of points together.

    If the polygon has an odd number of points, you can draw one line to make the star (as you did with the 5-pointed star). Start with the first point, skip a point, then draw the line to the next point, skip a point, etc. until you are back to the start.

    If there is an even number of points, then you need two lines, one that connects all the even-numbered points in order, and another to connect all the odd-numbered points.

    Try it with 6- and 7-pointed stars.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Star Rating system in QLineEdit or simliar
    By gig-raf in forum Qt Programming
    Replies: 6
    Last Post: 5th January 2016, 12:49
  2. [Bug Report?] Star Delegate Example
    By juarez in forum General Discussion
    Replies: 0
    Last Post: 9th May 2013, 04:06
  3. Star Delegate Example
    By graciano in forum Qt Programming
    Replies: 5
    Last Post: 9th February 2010, 16:55

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.