astar 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -1,6 +1,7 @@
1
1
  # Astar
2
2
 
3
- Pathfinding library using the astar algorithm
3
+ Pathfinding library using the astar algorithm, supports manhhaten and euclidean
4
+ distance heuristics.
4
5
 
5
6
  ## Installation
6
7
 
@@ -33,6 +34,8 @@ Pass in your start and destination tiles
33
34
  Astar::FindPath.from(tile1).to(tile3)
34
35
  => [tile1, tile2, tile3]
35
36
 
37
+ Astar::FindPath.from(tile1).to(tile).use_euclidean_distance
38
+
36
39
  The specs also have an example of how to use the lib.
37
40
 
38
41
  ## Contributing
@@ -1,3 +1,6 @@
1
+ require "astar/heuristic"
2
+ require "astar/heuristic/manhatten_distance"
3
+ require "astar/heuristic/euclidean_distance"
1
4
  require "astar/node"
2
5
  require "astar/version"
3
6
 
@@ -13,6 +16,10 @@ module Astar
13
16
  calculate_route
14
17
  end
15
18
 
19
+ def use_euclidean_distance
20
+ @strategy = EuclideanDistance
21
+ end
22
+
16
23
  def calculate_route
17
24
  until destination_reached? || @open_list.empty?
18
25
  current_node = @open_list.pop
@@ -31,7 +38,7 @@ module Astar
31
38
  end
32
39
 
33
40
  def calculate_fastest
34
- @open_list.sort_by! {|node| node.score }.reverse
41
+ @open_list.sort_by! {|node| @strategy.score(node) }.reverse
35
42
  end
36
43
 
37
44
  def destination_reached?
@@ -43,6 +50,7 @@ module Astar
43
50
  @from_node = Node.new(node, nil)
44
51
  @open_list = []
45
52
  @closed_list = []
53
+ @strategy = ManhattenDistance
46
54
  end
47
55
  end
48
56
  end
@@ -0,0 +1,20 @@
1
+ module Astar
2
+ class Heuristic
3
+ def self.score(node)
4
+ new(node).score
5
+ end
6
+
7
+ def relative_to_parent
8
+ return 10 if (@node.x > @node.parent.x && @node.y == @node.parent.y)
9
+ return 10 if (@node.x < @node.parent.x && @node.y == @node.parent.y)
10
+ return 10 if (@node.y > @node.parent.y && @node.x == @node.parent.x)
11
+ return 10 if (@node.y < @node.parent.y && @node.x == @node.parent.x)
12
+ 14
13
+ end
14
+
15
+ private
16
+ def initialize(node)
17
+ @node = node
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,9 @@
1
+ module Astar
2
+ class EuclideanDistance < Heuristic
3
+ def score
4
+ f = relative_to_parent
5
+ g = Math.sqrt((@node.destination.x - @node.x)^2 + (@node.destination.y - @node.y)^2)
6
+ f + g
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ module Astar
2
+ class ManhattenDistance < Heuristic
3
+ def score
4
+ f = relative_to_parent
5
+ g = (@node.destination.x - @node.x).abs + (@node.destination.y - @node.y).abs
6
+ f + g
7
+ end
8
+ end
9
+ end
@@ -1,6 +1,6 @@
1
1
  module Astar
2
2
  class Node
3
- attr_reader :tile, :parent
3
+ attr_reader :tile, :parent, :destination
4
4
  def initialize(tile, destination, parent=nil)
5
5
  @tile = tile
6
6
  @parent = parent
@@ -18,21 +18,5 @@ module Astar
18
18
  def y
19
19
  @tile.y
20
20
  end
21
-
22
- def score
23
- relative_to_parent + manhatten_distance
24
- end
25
-
26
- def manhatten_distance
27
- (@destination.x - @tile.x) + (@destination.y - @tile.y)
28
- end
29
-
30
- def relative_to_parent
31
- return 10 if (@tile.x > @parent.x && @tile.y == @parent.y)
32
- return 10 if (@tile.x < @parent.x && @tile.y == @parent.y)
33
- return 10 if (@tile.y > @parent.y && @tile.x == @parent.x)
34
- return 10 if (@tile.y < @parent.y && @tile.x == @parent.x)
35
- 14
36
- end
37
21
  end
38
22
  end
@@ -1,3 +1,3 @@
1
1
  module Astar
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: astar
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-09 00:00:00.000000000 Z
12
+ date: 2012-11-10 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -57,6 +57,9 @@ files:
57
57
  - Rakefile
58
58
  - astar.gemspec
59
59
  - lib/astar.rb
60
+ - lib/astar/heuristic.rb
61
+ - lib/astar/heuristic/euclidean_distance.rb
62
+ - lib/astar/heuristic/manhatten_distance.rb
60
63
  - lib/astar/node.rb
61
64
  - lib/astar/version.rb
62
65
  - spec/lib/astar_spec.rb