![]() |
Next: grid rotation
What would be your Pythonic solution for rotating a 3x3 grid counterclockwise by 90 degrees?
|
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 |
I know this isn't the Pythonic answer you're looking for, but it's an improvement:
:
for i in xrange(len(grid)): |
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). |
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.
|
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. ;)) |
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. |
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. ;) |
I would use matrix math and apply a transformation matrix to it.
|
Quote:
|
| 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