-
array question
Ok, so what i want to do is store a list of (x,y) points within an array. So far i've thought of two ways
1. xCoords = new Array(x1,x2,x3...)
yCoords = new Array(y1,y2,y3...)
2. coords = new Array((x1,y1),(x2,y2),(x3,y3)...)
The second one seems nicer with the "Array within the array" type of structure. My question is, is one of these two methods better? or is there a better way of storing a list of x,y points? Thanks in advance.
-
Senior Member
I like the second choice better, because it keeps the data elements together, although I would code it like this (I hardly every use the Array() constructor):
coords = [[x1,y1], [x2,y2], [x3,y3]];
Another way to code it is to use named elements, like so:
coords = [{x:x1,y:y1}, {x:x2,y:y2}, {x:x3,y:y3}];
This makes the code that accesses the array more readable, e.g.:
x = coords[2].x;
y = coords[2].y;
but it uses up more memory, and is probably slightly slower to access. I would probably use this method, however, if there was a good chance I would add other things to the data structure. For example, in my Asteroids game I used a data structure that looks like this, for creating little 2d wireframe models.
rockModel = [{mx:1, my:1, x:0,y:2, bf:0, bfa:50},
{x:-1,y:2}, {x:-2,y:1}, {x:-3,y:0}, {x:-3,y:-1},
{x:-2,y:-2}, {x:-1,y:-2}, {x:0,y:-3}, {x:2,y:-3},
{x:2,y:-2}, {x:2,y:0}, {x:1,y:1,ef:1}];
- Jim
Last edited by jbum; 10-07-2004 at 02:15 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|