From: Todd Benson Date: 2008-03-01T03:57:59+09:00 Subject: Re: Beginner Help with code example from book On Fri, Feb 29, 2008 at 10:58 AM, Joseph L. Casale wrote: > > > def continent_size world, x, y > > > if world[y][x] != ' land' > > > # Either it' s water or we already > > > # counted it, but either way, we don' t > > > # want to count it now. > > > return 0 > > > end > > > : > > > world[y][x] = ' counted land' > > > > The world is changed. When ' land' is counted, it turns into ' counted > > land' so on iteration, the "if" sees that it is not land and returns 0. > > > > -- > > Posted via http://www.ruby-forum.com/. > > Appreciate the pointer, thanks for bearing with me :) > Why has the author reversed the "X" and "Y"? on a square world it would not matter, but... Was there any significance to this? I'm pretty sure it doesn't matter on any surface as long all tiles are described in 2 dimensions (which they would have to, if you use a grid of x's and y's.) Think, for example, the surface of a 3-D globe cut up into 4-sided tiles of different size. The only drawback is that you have to pick poles to topologically shrink a sphere into a surface (this is often done by turning it into a torus/donut, but doesn't have to be) I haven't read Pine's book, but the x's and y's switcheroo can be explained, I suppose, like this... On a mathematical graph, a larger x means further right, and a larger y means further up. In a computer image, you have to remember, of course, larger y usually means further down. So, think of [0,3]. On an xy grid, that point would lie 3 units up on the vertical axis. For a multi-dimensional array [0,3] refers to the zeroth array, third item (or, first array and fourth item, depending on how your mind works). In your visual multi-dimensional array world map, that would be the fourth tile to the right, which would -- at least for most people -- be the x dimension, and not the y. Hope that makes some sense, Todd