Pentagram: Difference between revisions

(Added Go)
Line 539:
}</lang>
See [https://gist.github.com/thundergnat/70108a5160dd17dfe374#file-pentagram-svg Pentagram]
 
=={{header|Phix}}==
Resizable and optionally rotating gui (desktop) version
<lang Phix>-- demo\rosetta\Pentagram.exw
include pGUI.e
 
Ihandle dlg, canvas, timer
cdCanvas cddbuffer, 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*/, integer /*posy*/)
integer {w, h} = IupGetIntInt(canvas, "DRAWSIZE"),
cx = floor(w/2),
cy = floor(h/2),
r = floor(min(cx,cy)*0.9)
cdCanvasActivate(cddbuffer)
cdCanvasClear(cddbuffer)
{} = cdCanvasFillMode(cddbuffer, CD_WINDING)
cdCanvasLineWidth(cddbuffer, round(r/100)+1)
for mode=FILL to BORDER do
cdCanvasSetForeground(cddbuffer,colours[mode])
cdCanvasBegin(cddbuffer,modes[mode])
for a=90 to 666 by 144 do
atom ra = (a+rot)*CD_DEG2RAD,
x = r*cos(ra)+cx,
y = r*sin(ra)+cy
cdCanvasVertex(cddbuffer, x, y)
end for
cdCanvasEnd(cddbuffer)
end for
cdCanvasFlush(cddbuffer)
return IUP_DEFAULT
end function
 
function map_cb(Ihandle ih)
cdcanvas = cdCreateCanvas(CD_IUP, ih)
cddbuffer = cdCreateCanvas(CD_DBUFFER, cdcanvas)
cdCanvasSetBackground(cddbuffer, CD_WHITE)
cdCanvasSetForeground(cddbuffer, CD_GRAY)
return IUP_DEFAULT
end function
 
function timer_cb(Ihandle /*ih*/)
rot = mod(rot+359,360)
IupRedraw(canvas)
return IUP_IGNORE
end function
 
function esc_close(Ihandle /*ih*/, atom c)
if c=K_ESC then return IUP_CLOSE end if
if c=' ' then
IupSetInt(timer,"RUN",not IupGetInt(timer,"RUN"))
end if
return IUP_CONTINUE
end function
 
procedure main()
IupOpen()
 
canvas = IupCanvas(NULL)
IupSetAttribute(canvas, "RASTERSIZE", "640x640")
IupSetCallback(canvas, "MAP_CB", Icallback("map_cb"))
IupSetCallback(canvas, "ACTION", Icallback("redraw_cb"))
 
dlg = IupDialog(canvas)
IupSetAttribute(dlg, "TITLE", "Pentagram")
IupSetCallback(dlg, "K_ANY", Icallback("esc_close"))
 
IupShow(dlg)
IupSetAttribute(canvas, "RASTERSIZE", NULL)
timer = IupTimer(Icallback("timer_cb"), 80, active:=false)
IupMainLoop()
IupClose()
end procedure
 
main()</lang>
And a quick svg version
{{trans|Sidef}}
<lang Phix>constant HDR = """
<?xml version="1.0" standalone="no" ?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN"
"http://www.w3.org/TR/2001/PR-SVG-20010719/DTD/svg10.dtd">
<svg height="%d" width="%d" style="" xmlns="http://www.w3.org/2000/svg">
<rect height="100%%" width="100%%" style="fill:black;" />
"""
constant LINE = """
<polyline points="%s"
style="fill:blue; stroke:white; stroke-width:3;"
transform="translate(%d, %d) rotate(-18)" />
"""
 
function pentagram(integer dim=200, sides=5)
 
sequence v = repeat(0,sides)
for i=1 to sides do
atom theta = PI*2*(i-1)/5,
x = cos(theta)*dim,
y = sin(theta)*dim
v[i] = {sprintf("%.3f",x),
sprintf("%.3f",y)}
end for
v = append(v,v[1])
sequence q = {}
for i=1 to length(v) by 2 do
q &= v[i]
end for
for i=2 to length(v) by 2 do
q &= v[i]
end for
string res = sprintf(HDR,dim*2)
res &= sprintf(LINE,{join(q),dim,dim})
res &= "</svg>\n"
return res
end function
puts(1,pentagram())</lang>
Output identical to sidef
 
=={{header|PostScript}}==
7,794

edits