# Thread: star drawing in Star Delegate Example

1. Novice
Join Date
Jun 2019
Location
France, Pau
Posts
56
Thanks
28
Qt products
Platforms

## 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:
`starPolygon << QPointF(1.0, 0.5);    for (int i = 1; i < 5; ++i)        starPolygon << QPointF(0.5 + 0.5 * std::cos(0.8 * i * 3.14),                               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:
`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. ## 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.

3. Novice
Join Date
Jun 2019
Location
France, Pau
Posts
56
Thanks
28
Qt products
Platforms

## 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. ## 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.

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

embeddedmz (27th July 2020)

6. Novice
Join Date
Jun 2019
Location
France, Pau
Posts
56
Thanks
28
Qt products
Platforms

## Re: star drawing in Star Delegate Example

Ahhhhh OK, everything is clear now !

7. ## 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.

#### 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.