17
\$\begingroup\$

Given the number of vertices n ≥ 3 and the "step size" 1 ≤ m < n/2 (indicating the distance between two connected vertices), output a graphical representation of the corresponding regular polygram. If the polygram consist of multiple closed loops, each loop must be rendered in a different line colour. (If this sounds confusing, the examples below should hopefully clear things up.)

Rules

Any reasonable solution to the problem will likely satisfy these rules automatically - they are just there to put some constraints on the parameters of the output in order to prevent answers like "This black block is totally a polygram, but you can't see it because I set the line width to over 9000."

  • You can render the polygram either to a file (which may be written to disc or to the standard output stream) or display it on the screen.
  • You may use either vector or raster graphics. If your output is rasterised, your image must have dimensions of 400x400 pixels or more, and the polygram's radius (the distance from the centre to each vertex) must be between 35% and 50% of the side length.
  • The aspect ratio of the polygram must be 1 (so that its vertices lie on a proper circle) - the canvas of the image may be rectangular.
  • The lines of the polygram must be no thicker than 5% of the radius (and of course, they must have non-zero thickness to be visible).
  • You may render axes or a frame in addition to the polygram, but nothing else.
  • You may choose any (solid) background colour.
  • For polygrams consisting of multiple closed loops, you must support at least 6 visually distinct colours, all of which must be different from the background. (Grey-scale is fine, provided the shades are sufficiently spread out through the spectrum.) Your code must still work for more than 6 loops, but the colours don't have to be distinguishable for any additional loops (i.e. you may also reuse colours from previous loops at that point).

This is code golf, so the shortest answer (in bytes) wins.

Examples

Here are all the outputs up to n = 16 (where the column corresponds to n and the row to m):

enter image description here Click for larger version.

As examples for larger n, here are (n, m) = (29, 11) and (30, 12):

enter image description hereenter image description here

\$\endgroup\$
2
  • \$\begingroup\$ What about anti aliasing and stuff? (Since we are dealing with angled lines) \$\endgroup\$
    – Optimizer
    Commented Oct 13, 2015 at 12:26
  • 1
    \$\begingroup\$ @Optimizer I'm not going to prescribe an anti-aliasing algorithm. The lines may be aliased or anti-aliased as long as they are clearly visible. Use your best judgement. \$\endgroup\$ Commented Oct 13, 2015 at 12:28

3 Answers 3

5
\$\begingroup\$

MATLAB, 85 81

The function displays a plot on the screen.

function f(n,m)
hold all
axis equal
for k=1:gcd(m,n)
plot(i.^(4*(k:m:n^2)/n))
end

Result for n=30, m=12: f(30,12)

\$\endgroup\$
9
  • \$\begingroup\$ I am uncertain this works; if I've done it correctly, it will output this image for 30, 12. See: bit.ly/1GFZni7 \$\endgroup\$
    – durron597
    Commented Oct 13, 2015 at 16:55
  • \$\begingroup\$ @durron597 It actually works in Matlab, but the function is not immediately reusable, because the hold all leads to the next plot being drawn on top of the first... I don't know if we have a consensus about the reusability of functions to be honest. \$\endgroup\$ Commented Oct 13, 2015 at 17:20
  • \$\begingroup\$ @MartinBüttner It should be hold on to save a byte anyway; if you add four bytes it becomes reusable (clf\n) \$\endgroup\$
    – durron597
    Commented Oct 13, 2015 at 17:24
  • \$\begingroup\$ Turns out we have a consensus and I even posted the question and answer myself a few months ago. o.O So by that meta post, this would be invalid without something to release the hold. \$\endgroup\$ Commented Oct 13, 2015 at 18:02
  • \$\begingroup\$ @MartinBüttner If you change hold all to clf\nhold on does that fix the problem? \$\endgroup\$
    – durron597
    Commented Oct 13, 2015 at 18:45
3
\$\begingroup\$

CJam, 114

"P2"N400:ASAN6N0aA*aA*q~:M;:K{:CK,f{M*+K%P*2*K/_[mc)\ms)]199f*}_+2ew{~1$.-Af/A,\ff*\f.+{:mo~_3$=@C6%)tt}/}/}/Sf*N*

It outputs the image in ASCII PGM format.

You can try it online, but the output is pretty long. You can change 400 and 199 to smaller numbers to reduce the image size.

CJam has no concept of images, drawing, lines or shapes, so I generated the image in a square matrix, pixel by pixel (one number representing a grey shade for each pixel).

This is what the result looks like for 30 12:

polygram

\$\endgroup\$
1
\$\begingroup\$

Mathematica, 70 bytes

ListPolarPlot[Table[{2Pi(i+j#2)/#,1},{i,GCD@##},{j,#+1}],Joined->1>0]&

Well... this is my reference implementation which beats both submissions so far. I'm not intent on winning my own challenge, so I'm hoping someone will beat this.

The output is like the plots in the challenge itself, except I'm not removing the axes here:

enter image description here

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.