For Programmers: Free Programming Magazines  


Home > Archive > Java Help > December 2006 > Tricky Data Type Needed









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Tricky Data Type Needed
Brian Bagnall

2006-12-19, 4:15 am

Hi,

I'm writing a program that will store map data. Basically a 2-dimensional
array with values for x, y coordinates:
byte [][] pos

So to access data as perhaps x=90 and y=35, I would use:
int val = pos[90][35];

However, in a coordinate system, there are -ve and +ve values for x and y. So
an array really doesn't work too good here since I can't use -ve values to
access points on the map.

Also, the array needs to grow dynamically. Say it starts out as a map with 100
x 100, but later it finds other areas to map, it could grow to 100 x 120, or
perhaps 120 x 100 (depending if the x axis or y axis needs to grow).

Furthermore, the robot will start at 0,0 (origin) and needs to be able to
expand in either direction, either -ve or +ve.

Anyone have any suggestions for a good way to store this data in an orderly,
efficient manner while giving easy access/storage for data?

- Brian


Hendrik Maryns

2006-12-19, 4:15 am

Brian Bagnall schreef:
> Hi,
>
> I'm writing a program that will store map data. Basically a 2-dimensional
> array with values for x, y coordinates:
> byte [][] pos
>
> So to access data as perhaps x=90 and y=35, I would use:
> int val = pos[90][35];
>
> However, in a coordinate system, there are -ve and +ve values for x and y. So
> an array really doesn't work too good here since I can't use -ve values to
> access points on the map.
>
> Also, the array needs to grow dynamically. Say it starts out as a map with 100
> x 100, but later it finds other areas to map, it could grow to 100 x 120, or
> perhaps 120 x 100 (depending if the x axis or y axis needs to grow).
>
> Furthermore, the robot will start at 0,0 (origin) and needs to be able to
> expand in either direction, either -ve or +ve.
>
> Anyone have any suggestions for a good way to store this data in an orderly,
> efficient manner while giving easy access/storage for data?


I would start out with List<List<Whatever>>, and ask this question again
when profiling shows it is necessary. (At first, use the ArrayList
implementation.)

To cater for negative indices, you could use two lists, one for the
positive values, one for the negative, and do a switch on the sign. The
easiest will be to wrap this in a little class that implements List itself.

H.
Oliver Wong

2006-12-19, 7:14 pm


"Brian Bagnall" <bbagnall@mts.net> wrote in message
news:kTJhh.14766$gj2.5136@newsfe23.lga...
> Hi,
>
> I'm writing a program that will store map data. Basically a 2-dimensional
> array with values for x, y coordinates:
> byte [][] pos
>
> So to access data as perhaps x=90 and y=35, I would use:
> int val = pos[90][35];
>
> However, in a coordinate system, there are -ve and +ve values for x and
> y. So an array really doesn't work too good here since I can't use -ve
> values to access points on the map.
>
> Also, the array needs to grow dynamically. Say it starts out as a map with
> 100 x 100, but later it finds other areas to map, it could grow to 100 x
> 120, or perhaps 120 x 100 (depending if the x axis or y axis needs to
> grow).
>
> Furthermore, the robot will start at 0,0 (origin) and needs to be able to
> expand in either direction, either -ve or +ve.
>
> Anyone have any suggestions for a good way to store this data in an
> orderly, efficient manner while giving easy access/storage for data?


If you know the limit of growth goes up to 120, then just set the array
size to be [120][120], and ignore the extra space. If it can grow
arbitrarily large, consider a DataStructure like Hendrik Maryns suggested
(though I'd probably use Map<Pair<Integer,Integer>,Byte> rather than
List<List> ).

Not sure I understand the -ve, +ve thing, but if you're just saying that
the center of a 100x100 grid is located at 0,0 (as opposed to 50,50), and so
the top left corner is at coordinate -50,-50, then the easy fix is to just
add 50 to any coordinates you receive.

getDataAtCoordinate(int x, int y) {
return myArray[x + 50][y + 50];
}

foo = getDataAtCoordinate(-50,-50);

- Oliver


Brian Bagnall

2006-12-19, 7:14 pm

"Oliver Wong" <owong@castortech.com> wrote in message
news:mhThh.85299$_n2.560848@weber.videotron.net...
>
> If you know the limit of growth goes up to 120, then just set the array
> size to be [120][120], and ignore the extra space. If it can grow
> arbitrarily large, consider a DataStructure like Hendrik Maryns suggested
> (though I'd probably use Map<Pair<Integer,Integer>,Byte> rather than
> List<List> ).


Is there a particular implementation of Map that you have in mind?

- Brian


Oliver Wong

2006-12-19, 7:14 pm


"Brian Bagnall" <bbagnall@mts.net> wrote in message
news:1GXhh.22555$RJ.2902@newsfe17.lga...
> "Oliver Wong" <owong@castortech.com> wrote in message
> news:mhThh.85299$_n2.560848@weber.videotron.net...
>
> Is there a particular implementation of Map that you have in mind?


Perhaps HashMap.

- Oliver


Lew

2006-12-19, 10:09 pm

Oliver Wong wrote:
> (though I'd probably use Map<Pair<Integer,Integer>,Byte> ...
>
> Not sure I understand the -ve, +ve thing, but if you're just saying that
> the center of a 100x100 grid is located at 0,0 (as opposed to 50,50), and so
> the top left corner is at coordinate -50,-50, then the easy fix is to just
> add 50 to any coordinates you receive.
>
> getDataAtCoordinate(int x, int y) {
> return myArray[x + 50][y + 50];
> }
>
> foo = getDataAtCoordinate(-50,-50);


A lot of solutions to "robot" scenarios use polar coordinates, ( r, th ). No
negative indices there, but you must know your maths.

- Lew
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com