Re: cells touched in grid on move between cells

*To*: mathgroup at smc.vnet.net*Subject*: [mg115859] Re: cells touched in grid on move between cells*From*: Daniel Lichtblau <danl at wolfram.com>*Date*: Sun, 23 Jan 2011 05:35:02 -0500 (EST)

----- Original Message ----- > From: "Larz" <wbsurfver at gmail.com> > To: mathgroup at smc.vnet.net > Sent: Saturday, January 22, 2011 2:21:38 AM > Subject: [mg115833] cells touched in grid on move between cells > What is the basic calculation or formula to calculate what cells are > passed through in a grid in a move between cells. > > Say I am at x1,y2 and I want to move to x2,y2. I want to calculate all > the cell coordinates I have to pass through in that move. Sorry if my > math is a bit rusty, but offhand I am not sure what the formula is .. Not sure if this is what you mean. The example below finds intersection points of a certain segment with grid lines that are laid out at integer coordinates on each axis. {x1, y1} = {3.2, 7.1}; {x2, y2} = {11.4, 1.5}; xcoords = Table[j, {j, Ceiling[Min[x1, x2]], Floor[Max[x1, x2]]}]; ycoords = Table[j, {j, Ceiling[Min[y1, y2]], Floor[Max[y1, y2]]}]; segment = (x2 - x1)*(y - y1) - (y2 - y1)*(x - x1); horizintersections = Map[{x, #} /. Solve[(segment == 0) /. y -> #][[1]] &, ycoords] Out[265]= {{10.66785714285714, 2}, {9.203571428571427, 3}, {7.739285714285713, 4}, {6.275, 5}, {4.810714285714285, 6}, {3.346428571428571, 7}} vertintersections = Map[{#, y} /. Solve[(segment == 0) /. x -> #][[1]] &, xcoords] Out[266]= {{4, 6.553658536585366}, {5, 5.870731707317073}, {6, 5.18780487804878}, {7, 4.504878048780488}, {8, 3.821951219512195}, {9, 3.139024390243902}, {10, 2.456097560975609}, {11, 1.773170731707316}} Daniel Lichtblau Wolfram Research