Align a horizontally-aligned, right-angled ASCII shape along a diagonal

6

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.

Decent Dabbler

Posted 2014-02-22T06:44:44.047

Reputation: 605

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. – Tobia – 2014-02-24T19:04:34.747

Answers

3

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"



 / \ / \ 

 \     / 
  \   /  
   \ /   

aditsu quit because SE is EVIL

Posted 2014-02-22T06:44:44.047

Reputation: 22 326

1

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


DavidC

Posted 2014-02-22T06:44:44.047

Reputation: 24 524

Your output is not "constructed with forward slashes /, backward slashes , spaces and newlines" – aditsu quit because SE is EVIL – 2014-02-23T09:50:24.773

@aditsu. You were correct. However, it is now so constructed. They are rotated, but they remain ASCII characters. – DavidC – 2014-02-23T18:01:58.493

4Nope, those are still underscores and pipes (drawn at an angle) – aditsu quit because SE is EVIL – 2014-02-24T03:02:27.990

-1 because you're not following the rules – Doorknob – 2014-02-24T19:37:47.363

0

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>

Clyde Lobo

Posted 2014-02-22T06:44:44.047

Reputation: 1 395

2Even 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. – aditsu quit because SE is EVIL – 2014-02-24T13:00:54.630