Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Python (http://www.programmingforums.org/forum43.html)
-   -   Next: grid rotation (http://www.programmingforums.org/showthread.php?t=10842)

DaWei Jul 25th, 2006 4:33 PM

Next: grid rotation
 
What would be your Pythonic solution for rotating a 3x3 grid counterclockwise by 90 degrees?

MBirchmeier Jul 25th, 2006 5:08 PM

from a strictly code based solution I'd do something like
:

grid=[[grid[0][2],[grid[1][2],[grid[2][2]],[grid[0][1],[grid[1][1],[grid[2][1]],[grid[0][0],[grid[1][0],[grid[2][0]]]

I dunno about a more programatic solution though.

-MBirchmeier

titaniumdecoy Jul 25th, 2006 8:18 PM

I know this isn't the Pythonic answer you're looking for, but it's an improvement:

:

for i in xrange(len(grid)):
    for j in xrange(len(grid)):
        new_grid[i][j] = grid[j][abs(len(grid) - 1 - i)]


Sane Jul 25th, 2006 10:42 PM

Pythonic? That would be a simple implementation of an inverse function, using list comprehensions. We will need to count down from the max value, since Python counts backwards (if we are considering the cartegean plane).

:

print [[grid[y][l-x] for y in range(h)] for x in range(w)]

Where w is the width, h is the height, and l = w-1 (defined for efficiency purposes).

titaniumdecoy Jul 25th, 2006 10:59 PM

I think the idea is to create a new grid (or modify the original grid), rather than just print the values; this is a little harder. But I may be mistaken.

Sane Jul 25th, 2006 11:09 PM

Err... am I missing something? @_@ ...

Chapter 1 of the Python tutorial?? You can replace "print" with "grid =" ... :confused: ...


---

(Assignment takes place after calculation. It doesn't keep assigning as it's going, so you don't need to worry about that. ;))

titaniumdecoy Jul 25th, 2006 11:55 PM

Oh, sorry, missed that. :(

In that case, that's probably the best way to do it. It's simple, concise, and easy to read. Although I wouldn't define a separate variable for w-1, but that's just my preference.

Sane Jul 26th, 2006 12:21 AM

Well, it's usually a poor choice to blatantly disregard simple optimization... especially when all it calls for is swapping a calculation for a variable. :confused:

I know it barely makes any difference at all, but it would if say, w-1, was instead a function call. I realise in that case, you would have done it differently, but it's always best to be consistent with your style and make good habits.

Although I am being hypocritical, since, in respect to commenting ... I would never be consistent and comment every single program I make. ;)

andro Jul 26th, 2006 1:41 AM

I would use matrix math and apply a transformation matrix to it.

DaWei Jul 26th, 2006 8:29 AM

Quote:

I would use matrix math and apply a transformation matrix to it.
Why? Example?


All times are GMT -5. The time now is 12:41 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC