| 1 |
package org.sepy.geom |
|---|
| 2 |
{ |
|---|
| 3 |
public class Array2d |
|---|
| 4 |
{ |
|---|
| 5 |
protected var data:Array; |
|---|
| 6 |
protected var _w:uint; |
|---|
| 7 |
protected var _h:uint; |
|---|
| 8 |
protected var dirty:Boolean; |
|---|
| 9 |
|
|---|
| 10 |
public function Array2d(w:uint, h:uint) |
|---|
| 11 |
{ |
|---|
| 12 |
data = new Array(w * h); |
|---|
| 13 |
|
|---|
| 14 |
_w = w; |
|---|
| 15 |
_h = h; |
|---|
| 16 |
|
|---|
| 17 |
dirty = true; |
|---|
| 18 |
} |
|---|
| 19 |
|
|---|
| 20 |
public function get width():uint |
|---|
| 21 |
{ |
|---|
| 22 |
return _w; |
|---|
| 23 |
} |
|---|
| 24 |
|
|---|
| 25 |
public function get height():uint |
|---|
| 26 |
{ |
|---|
| 27 |
return _h; |
|---|
| 28 |
} |
|---|
| 29 |
|
|---|
| 30 |
public function get length():uint |
|---|
| 31 |
{ |
|---|
| 32 |
return _w * _h; |
|---|
| 33 |
} |
|---|
| 34 |
|
|---|
| 35 |
public function get flatData():Array |
|---|
| 36 |
{ |
|---|
| 37 |
return data; |
|---|
| 38 |
} |
|---|
| 39 |
|
|---|
| 40 |
public function fill(value:Number):void |
|---|
| 41 |
{ |
|---|
| 42 |
var i:int; |
|---|
| 43 |
|
|---|
| 44 |
for(i = 0; i < data.length; ++i) |
|---|
| 45 |
data[i] = value; |
|---|
| 46 |
|
|---|
| 47 |
dirty = true; |
|---|
| 48 |
} |
|---|
| 49 |
|
|---|
| 50 |
public function setAt(x:uint, y:uint, value:Number):void |
|---|
| 51 |
{ |
|---|
| 52 |
checkBounds(x, y); |
|---|
| 53 |
|
|---|
| 54 |
data[(y * _w) + x] = value; |
|---|
| 55 |
|
|---|
| 56 |
dirty = true; |
|---|
| 57 |
} |
|---|
| 58 |
|
|---|
| 59 |
public function getAt(x:uint, y:uint):Number |
|---|
| 60 |
{ |
|---|
| 61 |
checkBounds(x, y); |
|---|
| 62 |
|
|---|
| 63 |
return data[(y * _w) + x]; |
|---|
| 64 |
} |
|---|
| 65 |
|
|---|
| 66 |
public function sumValues():Number |
|---|
| 67 |
{ |
|---|
| 68 |
var sum:Number; |
|---|
| 69 |
|
|---|
| 70 |
sum = 0; |
|---|
| 71 |
|
|---|
| 72 |
for(var i:uint = 0; i < data.length; ++i) |
|---|
| 73 |
sum += data[i]; |
|---|
| 74 |
|
|---|
| 75 |
return sum; |
|---|
| 76 |
} |
|---|
| 77 |
|
|---|
| 78 |
public function clone():Array2d |
|---|
| 79 |
{ |
|---|
| 80 |
var cloned:Array2d; |
|---|
| 81 |
|
|---|
| 82 |
cloned = new Array2d(_w, _h); |
|---|
| 83 |
cloned.setFlatData(data); |
|---|
| 84 |
|
|---|
| 85 |
return cloned; |
|---|
| 86 |
} |
|---|
| 87 |
|
|---|
| 88 |
protected virtual function setFlatData(new_data:Array, copy:Boolean=false):void |
|---|
| 89 |
{ |
|---|
| 90 |
if(new_data.length != data.length) |
|---|
| 91 |
throw new ArgumentError("data assigned trought setFlatData MUST be the same length of the array"); |
|---|
| 92 |
|
|---|
| 93 |
if(copy) |
|---|
| 94 |
new_data = new_data.slice(); |
|---|
| 95 |
|
|---|
| 96 |
data = new_data; |
|---|
| 97 |
dirty = true; |
|---|
| 98 |
} |
|---|
| 99 |
|
|---|
| 100 |
private function checkBounds(x:uint, y:uint):void |
|---|
| 101 |
{ |
|---|
| 102 |
if((x < 0 || x > _w) || (y < 0 || y > _h)) |
|---|
| 103 |
throw new ArgumentError("Out of bound Array2d indexes"); |
|---|
| 104 |
} |
|---|
| 105 |
} |
|---|
| 106 |
} |
|---|