Advertisement
Guest User

Untitled

a guest
Jan 26th, 2015
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.93 KB | None | 0 0
  1. package ;
  2. import haxe.Log;
  3. import haxe.ds.Vector;
  4. import haxe.io.Bytes;
  5.  
  6. class Fixer {
  7. static public function main() {
  8. var m = Matrix.fromRows([
  9. [0, 0, 0, 1, 0, 1, 1],
  10. [0, 0, 0, 1, 0, 0, 0],
  11. [0, 0, 0, 1, 0, 0, 0],
  12. [0, 0, 0, 0, 0, 0, 0],
  13. [0, 0, 0, 0, 0, 0, 0]
  14. ]).transform(function(x, y, v) {
  15. //trace(v);
  16. var node = new Node(x, y, (v != 0));
  17. if (node.value) node.nearest = node;
  18. return node;
  19. });
  20. var nearestNode = null;
  21. var nearestNode2 = null;
  22.  
  23. for (kind in [
  24. { columnsRows: false, reverse: false },
  25. { columnsRows: false, reverse: true },
  26. { columnsRows: true, reverse: false },
  27. { columnsRows: true, reverse: true }
  28. ]) {
  29. m.iterate(kind.columnsRows, kind.reverse, function(x, y) {
  30. nearestNode = null;
  31. nearestNode2 = null;
  32. }, function(x, y, current) {
  33. if (current.value) {
  34. nearestNode = current;
  35. } else {
  36. current.tryUpdateWithNode(nearestNode);
  37. current.tryUpdateWithNode(nearestNode2);
  38. }
  39. nearestNode2 = current.nearest;
  40. });
  41. }
  42.  
  43. m.dump(',');
  44. }
  45. }
  46.  
  47. class Node {
  48. public var value:Bool;
  49. public var x:Int;
  50. public var y:Int;
  51. public var nearest(default, set):Node = null;
  52. public var distance(default, null):Float;
  53.  
  54. public function new(x:Int, y:Int, value:Bool) {
  55. this.x = x;
  56. this.y = y;
  57. this.value = value;
  58. this.distance = Math.POSITIVE_INFINITY;
  59. }
  60.  
  61. private function set_nearest(node:Node):Node {
  62. this.nearest = node;
  63. this.distance = calculateDistance(this, node);
  64. return node;
  65. }
  66.  
  67. public function tryUpdateWithNode(possibleNearestNode:Node) {
  68. if (possibleNearestNode == null) return;
  69. if (Node.calculateDistance(possibleNearestNode, this) < this.distance) {
  70. this.nearest = possibleNearestNode;
  71. }
  72. }
  73.  
  74. static public function calculateDistance(a:Node, b:Node):Float {
  75. if (a == null || b == null) return Math.POSITIVE_INFINITY;
  76. var x = a.x - b.x;
  77. var y = a.y - b.y;
  78. return Math.sqrt(x * x + y * y);
  79. }
  80.  
  81. public function toString() {
  82. return '' + Std.int(distance);
  83. }//return value ? '1' : '0';
  84. }
  85.  
  86. class Matrix<T> {
  87. private var data:Vector<T>;
  88. public var width(default, null):Int;
  89. public var height(default, null):Int;
  90.  
  91. @:generic static public function fromRows<T>(rows:Array<Array<T>>):Matrix<T> {
  92. var height = rows.length;
  93. var width = rows[0].length;
  94. var matrix = new Matrix(width, height);
  95. for (row in 0 ... height) {
  96. for (column in 0 ... width) {
  97. matrix.set(column, row, rows[row][column]);
  98. }
  99. }
  100. return matrix;
  101. }
  102.  
  103. public function new(width:Int, height:Int) {
  104. this.data = new Vector<T>(width * height);
  105. this.width = width;
  106. this.height = height;
  107. }
  108.  
  109. public function transform<T2>(transformer: Int -> Int -> T -> T2):Matrix<T2> {
  110. var that = new Matrix<T2>(width, height);
  111. for (y in 0 ... height) {
  112. for (x in 0 ... width) {
  113. that.set(x, y, transformer(x, y, this.get(x, y)));
  114. }
  115. }
  116. return that;
  117. }
  118.  
  119. private inline function getIndex(x:Int, y:Int) {
  120. return y * width + x;
  121. }
  122.  
  123. public function get(x:Int, y:Int):T {
  124. return this.data[getIndex(x, y)];
  125. }
  126.  
  127. public function iterate(columnsRows:Bool, reverse:Bool, startLine: Int -> Int -> Void, cell: Int -> Int -> T -> Void) {
  128. if (columnsRows) {
  129. iterateColumns(reverse, startLine, cell);
  130. } else {
  131. iterateRows(reverse, startLine, cell);
  132. }
  133. }
  134.  
  135. public function iterateColumns(reverse:Bool, startLine: Int -> Int -> Void, cell: Int -> Int -> T -> Void) {
  136. for (x in 0 ... width) {
  137. startLine(x, 0);
  138. for (y in 0 ... height) {
  139. var yy = (reverse) ? height - y - 1 : y;
  140. cell(x, yy, get(x, yy));
  141. }
  142. }
  143. }
  144.  
  145. public function iterateRows(reverse:Bool, startLine: Int -> Int -> Void, cell: Int -> Int -> T -> Void) {
  146. for (y in 0 ... height) {
  147. startLine(0, y);
  148. for (x in 0 ... width) {
  149. var xx = (reverse) ? width - x - 1 : x;
  150. cell(xx, y, get(xx, y));
  151. }
  152. }
  153. }
  154.  
  155. public function set(x:Int, y:Int, value:T):T {
  156. this.data[getIndex(x, y)] = value;
  157. return value;
  158. }
  159.  
  160. public function dump(separator = '') {
  161. for (y in 0 ... height) {
  162. var parts = [];
  163. for (x in 0 ... width) {
  164. parts.push('' + get(x, y));
  165. }
  166. trace(parts.join(separator));
  167. }
  168. }
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement