point-in-polygon 0.0.1

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6856f067378b3e269b10a940e4bbc90c5df0c659
4
+ data.tar.gz: e1db949b8ba37386ea0e7c02237c0f16e42f6dbd
5
+ SHA512:
6
+ metadata.gz: 1de4e2d19f13b1334f7b82f05c8c69bc6c5afe7df1590fab6b24f665c56b3ddc6555fc76fb2168b35a77b076ca80d851fa46c2a8affdd2a3ba50f623036fdd18
7
+ data.tar.gz: fc994585481e2720537e5f4d6f82fa1b7f98037652f008b873429dd7ca07d6a7def4dbb8803bb021976b2b18e9db68e775bebfbecc7e64a81a69604dc2008e32
@@ -0,0 +1,2 @@
1
+ require "point-in-polygon/Point"
2
+ require "point-in-polygon/Polygon"
@@ -0,0 +1,13 @@
1
+ require "matrix"
2
+ class Point < Vector
3
+ # attr_accessor :p
4
+
5
+ # def initialize()
6
+ # super
7
+ # end
8
+
9
+ # def reset(x, y)
10
+ # @x = x
11
+ # @y = y
12
+ # end
13
+ end
@@ -0,0 +1,77 @@
1
+ require "matrix"
2
+ class Polygon
3
+
4
+ def initialize(p)
5
+ @sides = p.length
6
+ @vertices = sort(p)
7
+ end
8
+
9
+ def isConvex
10
+ vectors = []
11
+ for i in 1..@sides
12
+ vectors << @vertices[i % @sides] - @vertices[i-1]
13
+ end
14
+ cross_product = each_cross_product(vectors)
15
+ return isSameDir(cross_product) ? true : false
16
+ end
17
+
18
+ def isInside(target)
19
+ if isConvex
20
+ target_vector = target_vector(target)
21
+ cross_product = each_cross_product(target_vector)
22
+ return isSameDir(cross_product) ? 1 : 0
23
+ end
24
+
25
+ return -1
26
+ end
27
+
28
+ protected
29
+ def sort(p)
30
+
31
+ x = y = 0
32
+ for v in p
33
+ x += v[0]
34
+ y += v[1]
35
+ end
36
+ avg_dot = [x/@sides, y/@sides]
37
+
38
+ arctan = []
39
+ for v in p
40
+ arctan << Math.atan2(v[0] - avg_dot[0], v[1] - avg_dot[1])
41
+ end
42
+ ind = arctan.map{|e| arctan.sort.index(e)}
43
+ p = p.sort_by{|x| ind[p.index(x)]}
44
+ return p
45
+ end
46
+
47
+ def target_vector(target)
48
+ target_vector = []
49
+ for v in @vertices
50
+ target_vector << v - target
51
+ end
52
+ return target_vector
53
+ end
54
+
55
+ def each_cross_product(vectors)
56
+
57
+ cross_product = []
58
+ len = vectors.length
59
+
60
+ for i in 1..len
61
+ cross_product << vectors[i-1].cross_product(vectors[i % @sides])
62
+ end
63
+
64
+ return cross_product
65
+ end
66
+
67
+ def isSameDir(cross_product)
68
+ len = cross_product.length
69
+ for i in 1..len
70
+ if cross_product[i-1][2] * cross_product[i % len][2] < 0
71
+ return false
72
+ end
73
+ end
74
+ return true
75
+ end
76
+
77
+ end
metadata ADDED
@@ -0,0 +1,46 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: point-in-polygon
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Even Chang
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-01-09 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Determine if a point is inside a polygon with a Sweep Line method
14
+ email: kiki44552002@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/point-in-polygon.rb
20
+ - lib/point-in-polygon/Point.rb
21
+ - lib/point-in-polygon/Polygon.rb
22
+ homepage: http://rubygems.org/gems/point-in-polygon
23
+ licenses:
24
+ - MIT
25
+ metadata: {}
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubyforge_project:
42
+ rubygems_version: 2.2.2
43
+ signing_key:
44
+ specification_version: 4
45
+ summary: point-in-polygon
46
+ test_files: []