
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


--
-- demo\rosetta\Pentagram.exw
-- ==========================
--
-- Start/stop rotation by pressing space. Resizeable.
-- ZXYV stop any rotation and orient up/down/left/right.
--
with javascript_semantics
include pGUI.e

Ihandle dlg, canvas, timer
cdCanvas cdcanvas

integer rot = 0
enum FILL,BORDER
constant colours = {CD_BLUE,CD_RED},
         modes = {CD_FILL,CD_CLOSED_LINES}

function redraw_cb(Ihandle /*ih*/, integer /*posx*/, /*posy*/)
    integer {w, h} = IupGetIntInt(canvas, "DRAWSIZE"),
            cx = floor(w/2),
            cy = floor(h/2),
            radius = floor(min(cx,cy)*0.9)
    cdCanvasActivate(cdcanvas)
    cdCanvasClear(cdcanvas)
    cdCanvasSetFillMode(cdcanvas, CD_WINDING)
    cdCanvasSetLineWidth(cdcanvas, round(radius/100)+1) 
    for mode=FILL to BORDER do
        cdCanvasSetForeground(cdcanvas,colours[mode])
        cdCanvasBegin(cdcanvas,modes[mode])
        for a=90 to 666 by 144 do
            atom r = (a+rot)*CD_DEG2RAD,
                 x = floor(radius*cos(r)+cx),
                 y = floor(radius*sin(r)+cy)
            cdCanvasVertex(cdcanvas, x, y)
        end for
        cdCanvasEnd(cdcanvas)
    end for
    cdCanvasFlush(cdcanvas)
    return IUP_DEFAULT
end function

function map_cb(Ihandle ih)
    cdcanvas = cdCreateCanvas(CD_IUP, ih)
    cdCanvasSetBackground(cdcanvas, CD_PARCHMENT)
    return IUP_DEFAULT
end function

function timer_cb(Ihandle /*ih*/)
    rot = mod(rot+359,360)
    IupRedraw(canvas)
    return IUP_IGNORE
end function

function key_cb(Ihandle /*ih*/, atom c)
    if c=K_ESC then return IUP_CLOSE end if
    c = upper(c)
    if c=' ' then
        IupSetInt(timer,"RUN",not IupGetInt(timer,"RUN"))
    else
        c = find(c,"ZYXV")
        if c then
            IupSetInt(timer,"RUN",false)
            rot = (c-1)*90
            IupRedraw(canvas)
        end if
    end if
    return IUP_CONTINUE
end function

procedure main()
    IupOpen()
    canvas = IupCanvas("RASTERSIZE=640x640")
    IupSetCallback(canvas, "MAP_CB", Icallback("map_cb"))
    IupSetCallback(canvas, "ACTION", Icallback("redraw_cb"))
    dlg = IupDialog(canvas,`TITLE="Pentagram"`)
    IupSetCallback(dlg, "KEY_CB", Icallback("key_cb"))
    IupShow(dlg)
    IupSetAttribute(canvas, "RASTERSIZE", NULL)
    timer = IupTimer(Icallback("timer_cb"), 80, active:=false)
    if platform()!=JS then
        IupMainLoop()
        IupClose()
    end if
end procedure

main()
