Reputation: 33
I have watched Professor Craven's video about drawing a polygon in Python:
https://www.youtube.com/watch?v=7qvsevlb5pg&list=PL1D91F4E6E79E73E1&index=22
His videos are very useful for beginners. His explanations are very helpful, so are the examples. When I tried his example:
pygame.draw.polygon(screen, black, [[100,100],[0,200],[200,200]], 5)
It worked just fine. However, when I tried something of my own, it drew only a line:
pygame.draw.polygon(screen, black, [[300,200],[150,100],[450,300]],6)
What is my mistake? Thanks in advance.
Upvotes: 3
Views: 10092
Reputation: 76194
The points (150, 100), (300,200), and (450,300) are collinear. The polygon they form is effectively an extremely flat triangle.
Try varying one of the points so that it doesn't line up with the other two.
pygame.draw.polygon(screen, black, [[300,400],[150,100],[450,300]],6)
In the future, it may be useful to check for collinearity before drawing, so you know you'll get a real shape. Generally, you can determine whether a collection of points are collinear, by comparing the slopes of the line segments they form together.
Line AB's slope is (200-100)/(300-150) = 2/3.
Line BC's slope is (300-100)/(450-150) = 2/3.
Line AC's slope is (300-200)/(450-300) = 2/3.
The slopes are all equal, so the points must all lie on the same line.
Upvotes: 5