The lexicographical (or row-major) order of a map

By on

Storing a tile-based map in a JavaScript array efficiently

Take this 2-dimensional tile-based map:

Tile-based map

As you can see according to these (x,y) coördinates

  • (0,0) = grass
  • (0,1) = water
  • (0,2) = grass
  • ...

Storing this map in memory can happen in two ways, for example you can store every Y-position in an X-row, like this:

map[X][Y] = tile;

So:

  • map[0][0] = grass
  • map[0][1] = water
  • map[0][2] = grass
  • ...

But there's a little problem in Javascript to look this up. For example, if you want to look up a certain value you would do something like this:

if (map[0][1] !== undefined){
    tile = map[0][1];
}

But what if the X isn't defined, and you're directly searching for a Y value in it? It would throw an error in this case. So you actually have to do:

if (map[0] !== undefined){
    if (map[0][1] !== undefined){
        tile = map[0][1];
    }
}

So 2 lookups for 1 coördinate.

A better way to do it would be to store it lexicographically.

Basically, you calculate the ID of the tile, what total number it is on the map, following this formula:

lexicograph = y * width + x

And that gives you this order:

Lexicographically ordered array

Comments

celebrities

Woah! I'm really loving the template/theme of this site. It's simple, yet effective. A lot of times it's tough to get that "perfect balance" between superb usability and visual appeal. I must say you've done a amazing job with this. In addition, the blog loads very quick for me on Opera. Superb Blog!

tbppjqqp

01/01/1967

AaronTup

wh0cd14326

AaronTup

wh0cd338294

TracyGag

wh0cd366390

AaronTup

wh0cd540774

TracyGag

wh0cd649862

TracyGag

wh0cd771350

TracyGag

wh0cd811846

TracyGag

wh0cd892838

TracyGag

wh0cd933334

AaronTup

wh0cd986230

TracyGag

wh0cd151510

TracyGag

wh0cd475478

TracyGag

wh0cd556470

TracyGag

wh0cd839942

AaronTup

wh0cd718454

TracyGag

wh0cd787046

TracyGag

wh0cd248198

AaronTup

wh0cd422582

AaronTup

wh0cd625062

TracyGag

wh0cd734150

AaronTup

wh0cd126710

AaronTup

wh0cd329190

AaronTup

wh0cd572166

AaronTup

wh0cd653158

AaronTup

wh0cd693654

AaronTup

wh0cd815142

Michaelcib

wh0cd5222 <a href=http://buytretinoin.us/>click for source</a> <a href=http://wherecanibuyviagra.link/>where can i buy viagra pills</a> <a href=http://prozac-generic.gdn/>prozac generic cost</a> <a href=http://pfizerviagra100mg.click/>pfizer viagra 100mg price</a> <a href=http://cephalexin-500mg.gdn/>cephalexin</a> <a href=http://citalopram-hbr.gdn/>citalopram hbr 20</a>

TracyGag

wh0cd167206

BennyVigma

wh0cd288694 <a href=http://citalopram-40mg.us/>citalopram 40 mg</a> <a href=http://citalopramhbr20mg.us/>citalopram hbr 20 mg</a> <a href=http://buy-ampicillin.us/>ampicillin on line cheap quick shipment</a> <a href=http://cymbalta-medication.us/>cymbalta medication</a> <a href=http://advair-diskus-250-50.gdn/>advair</a>

Eugenenom

wh0cd369686 <a href=http://buy-phenergan.us/>phenergan cost</a> <a href=http://clomid50mg.link/>50 mg clomid</a> <a href=http://buyadalat.party/>adalat</a> <a href=http://buytenormin.club/>tenormin</a> <a href=http://buyvaltrex7.us/>buy valtrex online without a prescription</a> <a href=http://sildalis.fashion/>example</a>

AaronTup

wh0cd572166

TracyGag

wh0cd734150

TracyGag

wh0cd977126

TracyGag

wh0cd188710

AaronTup

wh0cd269702

AaronTup

wh0cd310198

TracyGag

wh0cd431686

AaronTup

wh0cd958134

TracyGag

wh0cd54822

AaronTup

wh0cd257302

Michaelcib

wh0cd338294 <a href=http://metformin-500mg.us/>metformin er 1000</a> <a href=http://prednisone20mg.link/>prednisone</a> <a href=http://pfizerviagra100mg.click/>pfizer viagra 100mg price</a> <a href=http://nexium-medication.us/>nexium</a> <a href=http://doxycyclinemonohydrate.link/>doxycycline monohydrate</a>

BennyVigma

wh0cd297798 <a href=http://retinacream.us/>retin a cream</a> <a href=http://toradol.club/>toradol 10mg</a> <a href=http://diclofenac-sodium-75mg.us/>diclofenac</a> <a href=http://trazodone2.top/>desyrel</a> <a href=http://zofran4mg.review/>zofran 4mg</a> <a href=http://acyclovir-online.cricket/>acyclovir</a>

AlfredNox

wh0cd338294 <a href=http://methotrexatecost.site/>methotrexate</a> <a href=http://orlistat.bid/>blue capsule orlistat 60</a> <a href=http://finasteride-5mg.cricket/>propecia</a> <a href=http://generic-lexapro.us/>lexapro</a> <a href=http://levaquin500mg.us/>levaquin without prescription</a> <a href=http://clonidine-adhd.us/>clonidine otc</a> <a href=http://effexor-generic.cricket/>generic of effexor</a> <a href=http://erythromycin250mg.date/>erythromycin</a> <a href=http://ampicillin-online.gdn/>buy ampicillin online</a>

KennethFow

wh0cd540774 <a href=http://buying-zithromax.com/>zithromax</a> <a href=http://buyavodart.us/>avodart</a> <a href=http://buy-tamoxifen.site/>where to buy tamoxifen</a> <a href=http://buy-allopurinol.gdn/>allopurinol</a> <a href=http://buy-avodart.pro/>clicking here</a> <a href=http://cephalexin250mg.review/>cephalexin</a>

Michaelcib

wh0cd621766 <a href=http://tretinoin.christmas/>renova tretinoin cream 0.02 purchase</a> <a href=http://zoloftonline.review/>zoloft</a> <a href=http://seroquel.christmas/>seroquel for depression</a> <a href=http://wellbutrinxl.click/>wellbutrin</a> <a href=http://vermoxonline.us/>clicking here</a> <a href=http://baclofen10.top/>baclofen 10 mg no prescription</a> <a href=http://cafergotonline.us/>cafergot medication</a>

AaronTup

wh0cd702758

TracyGag

wh0cd783750

TracyGag

wh0cd1926

AaronTup

wh0cd82918

Eugenenom

wh0cd42422 <a href=http://buyrevia.club/>revia</a> <a href=http://flagyl.lol/>metronidazole 500 mg</a> <a href=http://buyseroquel.club/>buy seroquel</a> <a href=http://cephalexin250mg.pro/>cephalexin</a>

AaronTup

wh0cd123414 <a href=http://seroquel-generic.us/>read full article</a> <a href=http://provera.lol/>buy provera pills with mastercard</a> <a href=http://amitriptyline10mg.click/>amitriptyline price</a> <a href=http://cialas.us/>where to buy cialis in usa</a> <a href=http://cheapviagra.review/>pfizer viagra 100mg price</a> <a href=http://lasix-online.gdn/>lasix mg</a> <a href=http://tretinoincream01.click/>tretinoin cream</a> <a href=http://wellbutrin-150-mg.us/>wellbutrin 150 mg</a>

AlfredNox

wh0cd42422 <a href=http://diclofenacsod.review/>diclofenac</a> <a href=http://acyclovir75.top/>where can i buy acyclovir online</a> <a href=http://metformin500mgtablets.review/>metformin</a> <a href=http://tretinoin-online.us/>tretinoin online</a>

AaronTup

wh0cd325894

AlfredNox

wh0cd123414 <a href=http://buycafergot2013.top/>website</a> <a href=http://cymbaltacost.review/>cymbalta wellbutrin</a> <a href=http://allopurinolonline.club/>allopurinol</a> <a href=http://clindamycinhcl.pro/>clindamycin 1% topical gel</a> <a href=http://buyamoxicillin75.top/>amoxicillin 875 mg</a> <a href=http://retinaa.review/>where can i purchase retin a</a>

AaronTup

wh0cd366390

TracyGag

wh0cd406886

BennyVigma

wh0cd123414 <a href=http://apcalis.party/>apcalis without prescription</a> <a href=http://prednisolone5mg.link/>prednisolone 5 mg</a> <a href=http://chlomid.eu/>clomid steroids</a> <a href=http://zestril.gdn/>zestril</a> <a href=http://amitriptyline-hcl.trade/>amitriptyline</a> <a href=http://vpxl-online.in.net/>buy vpxl</a> <a href=http://buybentyl.pro/>bentyl</a>

TracyGag

wh0cd447382

KennethFow

wh0cd285398 <a href=http://lasix.christmas/>lasix 40mg to buy</a> <a href=http://abilifygeneric.us/>abilify</a> <a href=http://levitrageneric.click/>levitra generic</a> <a href=http://zithromax.christmas/>zithromax</a> <a href=http://buylisinopril.click/>buy lisinopril</a>

Charlesagola

wh0cd986230 <a href=http://acyclovir-400-mg.us/>acyclovir</a> <a href=http://inderalonline.click/>order inderal</a> <a href=http://cephalexin250mg.click/>cephalexin</a> <a href=http://propranololonline.link/>propranolol</a>

TracyGag

wh0cd528374

TracyGag

wh0cd649862

Eugenenom

wh0cd649862 <a href=http://phenergan75.top/>phenergan drug</a> <a href=http://buy-tetracycline.click/>full report</a> <a href=http://provera250.top/>found it for you</a> <a href=http://ventolinhfa.us/>go here</a> <a href=http://atenolol50mg.review/>atenolol generic</a>

AlfredNox

wh0cd487878 <a href=http://trazodonehydrochloride.link/>trazodone hydrochloride</a> <a href=http://furosemide.fashion/>furosemide</a> <a href=http://alli-online.gdn/>alli</a> <a href=http://vermox-online.gdn/>where to buy vermox</a> <a href=http://cialiscostperpill.click/>generic cialis us pharmacy</a> <a href=http://zofrangeneric.us/>zofran</a> <a href=http://abilify-cost.us/>abilify</a> <a href=http://cialis-10mg.gdn/>cialis 36 hour</a> <a href=http://sildenafil-100mg.us/>where to buy sildenafil online</a> <a href=http://cialisprofessional.gdn/>cialis professional</a>

AaronTup

wh0cd811846

TracyGag

wh0cd892838

AaronTup

wh0cd892838

KennethFow

wh0cd690358 <a href=http://buy-motrin.gdn/>buy motrin</a> <a href=http://levaquin-online.trade/>levaquin</a> <a href=http://augmentin-500-mg.cricket/>augmentin</a> <a href=http://amoxicillin-875.us/>amoxicillin brand name</a> <a href=http://ciprofloxacin-500-mg.party/>ciprofloxacin 500 mg</a> <a href=http://zoloft-50-mg.trade/>zoloft</a> <a href=http://elimitecream.link/>elimite</a>

BennyVigma

wh0cd811846 <a href=http://caverta.party/>Caverta</a>

TracyGag

wh0cd515974 <a href=http://endep.party/>buy endep</a>

Eugenenom

wh0cd434982 <a href=http://apcalis.science/>apcalis online</a>

Eugenenom

wh0cd475478 <a href=http://torsemide.us/>Buy Torsemide</a> <a href=http://innopranxl.cricket/>buy innopran</a>

TracyGag

wh0cd139110 <a href=http://endep.party/>endep online</a> <a href=http://medrol.us.com/>medrol 4mg</a>

Eugenenom

wh0cd98614 <a href=http://neurotin.science/>neurotin 400mg</a>

AlfredNox

wh0cd920934 <a href=http://neurotin.science/>buy neurotin</a>

Eugenenom

wh0cd815142 <a href=http://indocin.club/>indocin generic</a>

Eugenenom

wh0cd855638 <a href=http://aygestin.top/>Cheap Aygestin</a>

Michaelcib

wh0cd325894 <a href=http://endep.party/>buy endep</a> <a href=http://trecator.top/>home page</a>

MichaelEnurb

Forex

Jelle De Loecker