6
\$\begingroup\$

Your program will receive an ascii shape consisting of only underscores _, pipes |, spaces and newlines \n, which, together, represent a horizontally-aligned, right-angled shape. Together with this shape, on a new line, you will also receive either a positive or a negative integer, whose absolute value will be either one of 45, 135, 225 or 315 — signifying the degree that the original image should rotate. For instance:

 _
| |_
|___|
-45

Your program's job is to rotate this shape the provided amount of degrees. Following are the example outputs, for all possible degrees, given the above provided example shape:

  1. for 45° or -315°:

     /\
    / /
    \ \
     \/
    
  2. for 135° or -225°:

     /\
    /  \
    \/\/
    
  3. for 225° or -135°:

    /\
    \ \
    / /
    \/
    
  4. for 315° or -45°:

    /\/\
    \  /
     \/
    

Remember though: the original shape above is just an example. Your program should be able to handle any shape constructed with said characters (within reason, of course — let's say: with a maximum of 100 x 100 characters).

The output of your program should be the rotated image, constructed with forward slashes /, backward slashes \, spaces and newlines.

Shortest code, after one week, wins.

Note: although this challenge offers many similarities with these [1] [2] two challenges, I think this one offers just enough variation (because the original image is not supposed to be rotated at 90° angles) for it to generate interesting new results.

\$\endgroup\$
  • \$\begingroup\$ This question is ill-defined. How should we rotate this shape? __ | | |__| (sp _ _ sp, | sp sp |, | _ _ |) This is a 1.5 by 2 box, judging by your example above. \$\endgroup\$ – Tobia Feb 24 '14 at 19:04
3
\$\begingroup\$

Python 2 - 285

import sys
l=list(sys.stdin)
L=len;R=range
r=L(l)-2
c=L(max(l,key=L))-2
k=r+c+1
d=int(l[-1])/90%4
a=d/2;x=a^d%2
z=1-x*2;t=1-a*2
m=[k*[' ']for i in R(k)]
for i in R(r+1):
 for j in R(L(l[i])-1):m[a*c+x*r+z*i+t*j][r-a*r+x*c-t*i+z*j]='  \/'[ord(l[i][j])/40^d%2]
for s in m:print''.join(s)

I strongly suspect it can be golfed more, tips are welcome.

Examples:

% python r45.py <<< "||
_____
45"
 /    
\ /   
 \    
  \   
   \  
    \ 

% python r45.py <<< " _
| |_
|___|
-45"


    / \
 / \ / 
    /  
 \ /   
  \    

If the 2nd one looks misaligned, that's because of some issues with the question:
First, the underscore is at the bottom of the character "cell", while the pipe, slash and backslash are all in the middle. It would be fairer to use a dash (-) instead of underscore.
Second, with the rotation of the heart in the question, you couldn't represent it in any reasonable way if the first line was ___ (3 underscores) instead of  _, as the 1st and 3rd underscore would be "capping" the pipes in the 2nd line.

Here's a better heart:

% python r45.py <<< " _
| |
|  _
|   |
 ___
-45"



 / \ / \ 

 \     / 
  \   /  
   \ /   
\$\endgroup\$
0
\$\begingroup\$

Mathematica <225> 27

Second Version

Strange as it may seem, both the input and output are ASCII, "constructed with forward slashes /, backward slashes \, spaces and newlines"!

The command works by Rotateing the text within the Mathematica environment, while leaving the string in ASCII. The output is directly editable, as is, with no additional keystrokes or commands required. However, it will not export directly as ASCII. It can, however be exported directly as an image, as I have done here.

inputString = 
 " _
  | |_
  |___|";

The following works by rotating the ASCII string on-screen.

s_~f~d_ := Rotate[s, -d Degree]

Test Cases

f[s, 45]
f[s, -315]

p1


f[s, 135]
f[s, -225]

p2


f[s, -135]
f[s, 225]

p3

f[s, 315]
f[s, -45]

p4


\$\endgroup\$
  • \$\begingroup\$ Your output is not "constructed with forward slashes /, backward slashes \, spaces and newlines" \$\endgroup\$ – aditsu Feb 23 '14 at 9:50
  • \$\begingroup\$ @aditsu. You were correct. However, it is now so constructed. They are rotated, but they remain ASCII characters. \$\endgroup\$ – DavidC Feb 23 '14 at 18:01
  • 4
    \$\begingroup\$ Nope, those are still underscores and pipes (drawn at an angle) \$\endgroup\$ – aditsu Feb 24 '14 at 3:02
  • \$\begingroup\$ -1 because you're not following the rules \$\endgroup\$ – Doorknob Feb 24 '14 at 19:37
0
\$\begingroup\$

HTML/CSS/Javascript

FIDDLE : http://jsfiddle.net/c5h2d/1/

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
<script type="text/javascript">
function rotate(){
    var x = document.querySelector("#i").value, y = document.querySelector("#t").value; document.querySelector("#a").innerHTML = y ;
    document.getElementById("a").style.transform = "rotate("+x+"deg)"

}
</script>

<style type="text/css">
#a{
    transform:rotate(45deg);
    position:absolute;
    top:50%;
    left:50%
}
</style>
</head>

<body>

<pre id="a"></pre>
<textarea id=t cols="30" rows="10"></textarea>
<input type="text" id=i /><button onclick='rotate()'>Rotate</button>
</body>
</html>
\$\endgroup\$
  • 2
    \$\begingroup\$ Even in browsers that support transform:rotate directly (chrome/chromium does not), your output is not "constructed with forward slashes /, backward slashes \, spaces and newlines". Basically you're doing the same thing as David Carraher. \$\endgroup\$ – aditsu Feb 24 '14 at 13:00

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.