Python, build a pentagon

The task is to build a regular pentagon with side a.

The Pillow library must be used here.

Here is my code:

import PIL
from PIL import Image, ImageDraw
a=int(input("Введите размер стороны а: "))

img = Image.new("RGB",(700,700), (2555,255,255))
draw = ImageDraw.Draw(img)
draw.polygon((0.6*a,1.4*a, 1.3*a,0.9*a, 2*a,1.4*a, 1.7*a,2.2*a ,0.9*a,2.2*a), fill="lightblue",outline =(255,0,0))

img.show()

I chose the coordinates myself, and in general, the pentagon looks like a regular one, but it's not easy to prove it. We also made a note that the "a" side should be entered from the keyboard in pixels, and I get my own conditional unit.. Still somehow you need to sign the picture "This is a pentagon".

Here is the result of my program:

enter a description of the image here

Author: demonplus, 2018-12-20

1 answers

Why pick up coordinates if they can be calculated?

import math
from PIL import Image, ImageDraw, ImageFont

x=250 #центр полигона (x)
y=250 #центр полигона (y)
n=5   #число сторон полигона
r=200  #радиус окружности в которую вписываем полигон
#получаем координаты вершин
coords=[(x + r * math.cos(2 * math.pi * i / n), y + r * math.sin(2 * math.pi * i / n)) for i in range(1, n+1)]

img = Image.new("RGB",(500,500), (255,255,255))
draw = ImageDraw.Draw(img)

draw.polygon((coords), fill="lightblue",outline =(255,0,0))
unicode_font = ImageFont.truetype("arial.ttf", 22)
draw.text ((100,40), u'Это многоугольник', font=unicode_font, fill='red' )
img.show()

pentagon

Well, since the length of the side is initially given, the radius of the circumscribed circle is also easy to calculate:

r = side/(2*math.sin(math.pi/n)) #side - длина стороны, n - количество сторон
 11
Author: strawdog, 2018-12-25 09:35:03