language-agnosticgeometrycode-golfrosetta-stone

Code Golf: Build Me an Arc


Challenge

The shortest program by character count that accepts standard input of the form X-Y R, with the following guarantees:

And produces on standard output an ASCII "arc" from the starting angle X to the ending angle Y of radius R, where:

The program is not required to produce meaningful output if given invalid input. Solutions in any language are allowed, except of course a language written specifically for this challenge, or one that makes unfair use of an external utility. Extraneous horizontal and vertical whitespace is allowed in the output provided that the format of the output remains correct.

Happy golfing!

Numerous Examples

Input:

0-45 8

Output:

        /
       /x
      /xx
     /xxx
    /xxxx
   /xxxxx
  /xxxxxx
 /xxxxxxx
o--------

Input:

0-135 4

Output:

\xxxxxxxx
 \xxxxxxx
  \xxxxxx
   \xxxxx
    o----

Input:

180-360 2

Output:

--o--
xxxxx
xxxxx

Input:

45-90 0

Output:

o

Input:

0-315 2

Output:

xxxxx
xxxxx
xxo--
xxx\
xxxx\

Solution

  • Perl, 235 211 225 211 207 196 179 177 175 168 160 156 146 chars

    <>=~/-\d+/;for$y(@a=-$'..$'){print+(map$_|$y?!($t=8*($y>0)+atan2(-$y,$_)/atan2 1,1)&-$&/45==8|$t>=$`/45&$t<=-$&/45?qw(- / | \\)[$t%4]:$":o,@a),$/}
    


    Perl using say feature, 161 149 139 chars

    $ echo -n '<>=~/-\d+/;for$y(@a=-$'"'"'..$'"'"'){say map$_|$y?!($t=8*($y>0)+atan2(-$y,$_)/atan2 1,1)&-$&/45==8|$t>=$`/45&$t<=-$&/45?qw(- / | \\)[$t%4]:$":o,@a}' | wc -c
    139
    $ perl -E '<>=~/-\d+/;for$y(@a=-$'"'"'..$'"'"'){say map$_|$y?!($t=8*($y>0)+atan2(-$y,$_)/atan2 1,1)&-$&/45==8|$t>=$`/45&$t<=-$&/45?qw(- / | \\)[$t%4]:$":o,@a}'
    


    Perl without trailing newline, 153 143 chars

    <>=~/-\d+/;for$y(@a=-$'..$'){print$/,map$_|$y?!($t=8*($y>0)+atan2(-$y,$_)/atan2 1,1)&-$&/45==8|$t>=$`/45&$t<=-$&/45?qw(- / | \\)[$t%4]:$":o,@a}
    


    Original version commented:

    $_=<>;m/(\d+)-(\d+) (\d+)/;$e=$1/45;$f=$2/45; # parse angles and radius, angles are 0-8
    for$y(-$3..$3){                               # loop for each row and col
        for$x(-$3..$3){
                $t=atan2(-$y,$x)/atan2 1,1;   # angle of this point
                $t+=8if($t<0);                # normalize negative angles
                @w=split//,"-/|\\"x2;         # array of ASCII symbols for enclosing lines
                $s.=!$x&&!$y?"o":$t==$e||$t==$f?$w[$t]:$t>$e&&$t<$f?"x":$";
                # if it's origin -> "o", if it's enclosing line, get symbol from array
                # if it's between enclosing angles "x", otherwise space
        }
        $s.=$/;
    }
    print$s;
    


    EDIT 1: Inlined sub, relational and equality operators return 0 or 1.
    EDIT 2: Added version with comments.
    EDIT 3: Fixed enclosing line at 360º. Char count increased significantly.
    EDIT 4: Added a shorter version, bending the rules.
    EDIT 5: Smarter fix for the 360º enclosing line. Also, use a number as fill. Both things were obvious. Meh, I should sleep more :/
    EDIT 6: Removed unneeded m from match operator. Removed some semicolons.
    EDIT 7: Smarter regexp. Under 200 chars!
    EDIT 8: Lots of small improvements:


    EDIT 9: A little reordering in the conditional operators saves 2 chars.
    EDIT 10: Use barewords for characters.
    EDIT 11: Moved print inside of loop, inspired by Lowjacker's answer.
    EDIT 12: Added version using say.
    EDIT 13: Reuse angles characters for fill character, as Gwell's answer does. Output isn't as nice as Gwell's though, that would require 5 additional chars :) Also, .. operator doen't need parentheses.
    EDIT 14: Apply regex directly to <>. Assign range operator to a variable, as per Adrian's suggestion to bta's answer. Add version without the final newline. Updated say version.
    EDIT 15: More inlining. map{block}@a -> map expr,@a.