The main problem isn't finding $mod, it's getting it to work, and you've done that. Congratulations!

However, there are a couple of things you might find it useful to change.

Firstly, "use strict" at the top of your script. Always. Until you know enough to ignore that advice.

Secondly, try having a short main body of code calling a couple of top-level functions:

#!/usr/bin/perl ... my @polygon = build_polygon($num_points, $radius); print_svg_polygon(@polygon); exit; sub build_polygon { ...

It makes it easy to see what the script does at a glance, and lets you re-use bits more easily.

Thirdly, make sure you scope your variables. Use "my" to restrict them to their defining block. Otherwise, the $n in calcMagic for example is the same as the $n at the top of your script. If you changed it inside the sub it would change in the main script too.

sub calcMagic { my ($n) = @_; my $m = -1; my $mod = 1; for(1..$n) { $mod += $m; $m = nextMagic($m); } return $mod; }

Finally, read up on Getopt::Long (http://search.cpan.org/~jv/Getopt-Long-2.38/lib/Getopt/Long.pm). That will let you call add named options to your script so you can do something like:

draw_star.pl --points=5 --radius=200

It's a well-known and well tested module, and has good documentation.

You could also replace the multiple "if" statements with a lookup into an array, but I'm not sure it would make the code any clearer really.

Hope that's helpful.


In reply to Re: RFC: Creating unicursal stars by huxtonr
in thread RFC: Creating unicursal stars by Fox

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.