PDA

View Full Version : star drawing in Star Delegate Example



embeddedmz
23rd July 2020, 16:04
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-itemviews-stardelegate-example.html

Star points :

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));

Star drawing :

painter->drawPolygon(starPolygon, Qt::WindingFill);

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

Thanks.

d_stranz
23rd July 2020, 16:47
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.

embeddedmz
24th July 2020, 17:09
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.

d_stranz
24th July 2020, 20:51
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.

embeddedmz
27th July 2020, 13:18
Ahhhhh OK, everything is clear now !

d_stranz
27th July 2020, 15:37
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.