Write a program rotates some Cartesian coordinates through an angle about the origin (0.0,0.0). The angle and coordinates will be read from a single line of stdin in the following format:

angle x1,y1 x2,y2 x3,y3 ...


3.14159265358979 1.0,0.0 0.0,1.0 1.0,1.0 0.0,0.0  

The results should be printed to stdout in the following format:

x1',y1' x2',y2' x3',y3' ...


-1.0,-3.23108510433268e-15 -3.23108510433268e-15,-1.0 -1.0,-1.0 -0.0,-0.0
  • \$\begingroup\$ Am I allowed a leading space in the output? \$\endgroup\$ – J B Mar 4 '11 at 20:13
  • \$\begingroup\$ @JB yes that is fine \$\endgroup\$ – david4dev Mar 4 '11 at 20:41
  • \$\begingroup\$ Can J unary minus signs (_) be used for output? For input? \$\endgroup\$ – J B Mar 5 '11 at 0:04

Perl, 112 80 78

s/\S*//;$c=cos$&;$s=sin$&;s/([^ ,]*),(\S*)/($1*$c-$2*$s).",".($1*$s+$2*$c)/ge

Run on command-line as perl -pe 'code here', p option counted in code size.

For reference, here's my previous approach. But regexes are too damn powerful.

split/[ ,]/,<>;$_='A;Y,print$x*cos($a)-$y*sin$a,",",$x*sin($a)+$y*cos$a,$"whileX';s/[AXY]/\$\l$&=shift\@_/g;eval

C (124 Characters)

main(){double t,x,y,c,s;scanf("%lf",&t);c=cos(t);s=sin(t);while(~scanf("%lf,%lf",&x,&y))printf("%lf,%lf ",c*x-s*y,s*x+c*y);}
  • \$\begingroup\$ this doesn't compile. \$\endgroup\$ – david4dev Mar 4 '11 at 14:44
  • \$\begingroup\$ @david4dev wild guess: link with -lm \$\endgroup\$ – J B Mar 4 '11 at 14:46
  • \$\begingroup\$ @david4dev ideone.com/nuhLz \$\endgroup\$ – fR0DDY Mar 4 '11 at 14:46
  • \$\begingroup\$ It compiles and runs OK with -lm. \$\endgroup\$ – david4dev Mar 4 '11 at 14:48

Python (157)

from math import*;f=float
def p((x,y)):print"%f,%f"%(c*x-s*y,s*x+c*y),
for l in i[1:]:p(map(f,l.split(',')))

J, 58 (with one bug)

(r.@{.*}.)&.(".@('-_,j'&c :.('j,_-'&c))@stdin)_

Sample run (note I've replaced the origin test case with some negative input, as that was actually a pain to get right with J's representation of negatives):

echo -n '3.14159265358979 1.0,0.0 0.0,1.0 1.0,1.0 -1.0,-1.0' | jconsole rotate.ijs
-1,3.23109e-15 -3.23109e-15,-1 -1,-1 1,1   

There is one family of input that will not render exactly as asked (though calling it wrong would be debatable). Which one exactly is left as an exercise to the reader :-)


APL (Dyalog Extended), 51 bytes

x←⎕⋄g←{(¯1 2⍴1↓x)×⍤1⊢⍵○⊃x}⋄∊⍉↑(⊂-/g⌽⍳2)⍪⊂',',¨+/g⍳2

Try it online!

Reduced by a lot with dzaima's help at The APL Orchard. Apparently there's still room to save a few bytes, which I'll be golfing later.


x←⎕⋄g←{(¯1 2⍴1↓x)×⍤1⊢⍵○⊃x}⋄∊⍉↑(⊂-/g⌽⍳2)⍪⊂',',¨+/g⍳2
x←⎕                                                 store input as x
   ⋄g←{                  }                          define function g:
                       ⊃x                           first element of x
                     ⍵○                             take sine & cosine of it based on argument
                 ×⍤1⊢                               multiply with each row of
       (¯1 2⍴1↓x)                                   the points reshaped into pairs
                          ⋄                         finally,
                                              +/g⍳2 x sin theta + y cos theta
                                        ⊂',',¨      prepend comma to each
                                       ⍪            joined with
                              (⊂-/g⌽⍳2)             x cos theta - y sin theta
                            ⍉↑                      convert to matrix and transpose
                           ∊                        and enlist the elements                                               

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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