
A pentagram is a star polygon, consisting of a central pentagon of which each side forms the base of an isosceles triangle. The vertex of each triangle, a point of the star, is 36 degrees.




Task

Draw (or print) a regular pentagram, in any orientation. Use a different color (or token) for stroke and fill, and background. For the fill it should be assumed that all points inside the triangles and the pentagon are inside the pentagram.




See also
 Angle sum of a pentagram


 
import libgd
from math import sin, cos, degToRad
 
const
  width = 500
  height = width
  outerRadius = 200
 
proc main() =
 
  proc calcPosition(x: int, y: int, radius: int, posAngle: float): (cint, cint) =
    var width = int(radius.float * sin(degToRad(posAngle)))
    var height = int(radius.float * cos(degToRad(posAngle)))
    return (cast[cint](x + width), cast[cint](y - height))
 
  proc getPentagonPoints(startAngle = 0, radius: int): array[5, array[2, int]] =
    let spacingAngle = 360 / 5
 
    var posAngle = (90 - startAngle).float
 
    var n = 0
    var points: array[5, array[2, int]]
    while n < 5:
      (points[n][0], points[n][1]) = calcPosition(250, 250, radius, posAngle)
      n += 1
      posAngle -= spacingAngle
 
    return points
 
  let outerPentagon = getPentagonPoints(18, outerRadius) # rotate 18 degrees
  let innerPentagon = getPentagonPoints(54, int((cos(degToRad(72.0))/cos(degToRad(36.0))) * outerRadius)) # rotate 54 degrees
 
  var pentagram: array[10, array[2, int]]
  var n = 0
  for i in countup(0, 4):
    pentagram[n] = outerPentagon[i]
    inc(n)
    pentagram[n] = innerPentagon[i]
    inc(n)
 
  withGd imageCreate(width, height) as img:
    discard img.setColor(255, 255, 255)
 
    let black = img.setColor(0x404040)
    let blue = img.setColor(0x6495ed)
 
    img.drawPolygon(
      points=pentagram,
      color=blue,
      fill=true,
      open=false)
 
    img.setThickness(4)
 
    img.drawPolygon(
      points=pentagram,
      color=black,
      fill=false,
      open=false)
 
    img.drawPolygon(
      points=innerPentagon,
      color=black,
      fill=false,
      open=false)
 
    let png_out = open("pentagram.png", fmWrite)
    img.writePng(png_out)
 
    png_out.close()
 
main()
 