Advertisement
maxim_shlyahtin

Шестиугольник

Dec 4th, 2022
475
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.80 KB | None | 0 0
  1. import PIL
  2. import numpy as np
  3. import math
  4. from PIL import Image, ImageDraw
  5.  
  6. w, h = 300, 300
  7.  
  8.  
  9. def func(figure_type, args):
  10.     if figure_type == "circle":
  11.         img = func_circle(args['radius'], args['x'], args['y'], args['width_line'], args['color_line'], args['is_fill'],
  12.                           args['color_fill'])
  13.     else:
  14.         img = func_square(args['x1'], args['y1'], args['x2'], args['y2'], args['width_line'], args['color_line'],
  15.                           args['is_fill'],
  16.                           args['color_fill'])
  17.     return img
  18.  
  19.  
  20. def func_circle(r, x, y, width_line, color_line, is_fill, color_fill):
  21.     img = Image.new("RGB", (w, h), "black")
  22.     coordinates = []
  23.     sectors = 6
  24.     for i in range(1, sectors + 1):
  25.         angle = 2 * math.pi * i / sectors
  26.         coordinate_x = r * math.cos(angle) + x
  27.         coordinate_y = r * math.sin(angle) + y
  28.         coordinates += [(coordinate_x, coordinate_y)]
  29.     color_fill = color_fill if is_fill else ''
  30.     drawing = ImageDraw.Draw(img)
  31.     drawing.polygon(coordinates, fill=color_fill, outline=color_line, width=width_line)
  32.     return img
  33.  
  34.  
  35. def func_square(x1, y1, x2, y2, width_line, color_line, is_fill, color_fill):
  36.     img = Image.new("RGB", (w, h), "black")
  37.     coordinates = []
  38.     sectors = 6
  39.     radius = abs(x2 - x1) // 2
  40.     center_x = x2 - radius
  41.     center_y = y2 - radius
  42.     for i in range(1, sectors + 1):
  43.         angle = 2 * math.pi * i / sectors
  44.         coordinate_x = radius * math.cos(angle) + center_x
  45.         coordinate_y = radius * math.sin(angle) + center_y
  46.         coordinates += [(coordinate_x, coordinate_y)]
  47.     color_fill = color_fill if is_fill else ''
  48.     drawing = ImageDraw.Draw(img)
  49.     drawing.polygon(coordinates, fill=color_fill, outline=color_line, width=width_line)
  50.     return img
  51.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement