pip 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/pip/polygon.rb +36 -0
- data/lib/pip/version.rb +1 -1
- data/spec/lib/polygon_spec.rb +152 -0
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: aee81813217ab64ca9b10edf1a808f29548a1d6d
|
4
|
+
data.tar.gz: 1f69baac0ba82cae72e7f5aef74311e5abedb76c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0bb21a389cafe4d35f51f1b2c876999bbccba7570554cf25207a71e99688d9cf335fb0d606a499c2da3238e2ae506abd9aca8e6099bd756f3619a3270bdb10f7
|
7
|
+
data.tar.gz: 955c07469d4754e0466044f88554103096f9f3b00d1f62dc900987a27c6718697bfdbdaa6ccfc4d868309c9a63d19bbf1980e0a5eb81d2b7e666f90b4d344054
|
data/lib/pip/polygon.rb
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# I know practically nothing about Geometry, I came up with the
|
2
|
+
# algorithm for this by sketching out a few dots and writing specs.
|
3
|
+
|
4
|
+
module Pip
|
5
|
+
class Polygon
|
6
|
+
attr_accessor :points
|
7
|
+
def initialize(points)
|
8
|
+
@points = points
|
9
|
+
end
|
10
|
+
|
11
|
+
def contains?(x,y)
|
12
|
+
(vertical_boundaries_on(y).map{|k| [k,nil]} << [x,x]).sort_by{|one, two| one}.uniq.find_index([x,x]).odd?
|
13
|
+
end
|
14
|
+
|
15
|
+
def vertical_boundaries_on(target_y)
|
16
|
+
lines.map do |p1, p2|
|
17
|
+
x = Polygon.coord_at(target_y, p1, p2)
|
18
|
+
comparable = [p1[1], p2[1]].sort
|
19
|
+
next if comparable.uniq.size == 1
|
20
|
+
x if target_y.between?(comparable[0], comparable[1])
|
21
|
+
end.compact.sort
|
22
|
+
end
|
23
|
+
|
24
|
+
def lines
|
25
|
+
perms = []
|
26
|
+
points.each_with_index {|n, i| perms << [n, points[i+1] || points[0]] }
|
27
|
+
perms
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.coord_at(y, p1, p2)
|
31
|
+
x1, y1 = p1.map(&:to_f)
|
32
|
+
x2, y2 = p2.map(&:to_f)
|
33
|
+
(y - y1) / (y2 - y1) * (x2 - x1) + x1
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
data/lib/pip/version.rb
CHANGED
@@ -0,0 +1,152 @@
|
|
1
|
+
require_relative "../../lib/pip"
|
2
|
+
|
3
|
+
|
4
|
+
describe Pip::Polygon do
|
5
|
+
let(:poly) { Pip::Polygon.new(points) }
|
6
|
+
describe "square" do
|
7
|
+
let(:points) { [[0,0], [0,2], [2,2], [2,0]] }
|
8
|
+
it "takes args" do
|
9
|
+
expect(poly.points).to eq points
|
10
|
+
end
|
11
|
+
|
12
|
+
it "finds the vertical boundaries" do
|
13
|
+
expect(poly.vertical_boundaries_on(1)).to eq [0,2]
|
14
|
+
end
|
15
|
+
|
16
|
+
it "finds if a point is within it" do
|
17
|
+
expect(poly.contains?(1,1)).to be_true
|
18
|
+
expect(poly.contains?(1,3)).to be_false
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
describe "utah shape" do
|
23
|
+
let(:points) { [[0,0], [4,0], [4,1], [2,1], [2,2], [0,2]] }
|
24
|
+
|
25
|
+
it "has vertical boundaries" do
|
26
|
+
expect(poly.vertical_boundaries_on(0)).to eq [0,4]
|
27
|
+
expect(poly.vertical_boundaries_on(1)).to eq [0, 2, 4]
|
28
|
+
expect(poly.vertical_boundaries_on(2)).to eq [0, 2]
|
29
|
+
expect(poly.vertical_boundaries_on(3)).to eq []
|
30
|
+
end
|
31
|
+
|
32
|
+
it "finds if a point is within a polygon" do
|
33
|
+
expect(poly.contains?(1,1)).to be_true
|
34
|
+
expect(poly.contains?(1,1)).to be_true
|
35
|
+
expect(poly.contains?(0,1)).to be_true
|
36
|
+
expect(poly.contains?(0,0)).to be_true
|
37
|
+
expect(poly.contains?(4,1)).to be_true
|
38
|
+
expect(poly.contains?(4,2)).to be_false
|
39
|
+
expect(poly.contains?(3,2)).to be_false
|
40
|
+
expect(poly.contains?(999,999)).to be_false
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
describe "triangle" do
|
46
|
+
let(:points) { [
|
47
|
+
[0,0],
|
48
|
+
[3,0],
|
49
|
+
[1,3]
|
50
|
+
] }
|
51
|
+
|
52
|
+
it "finds the points within the polygon" do
|
53
|
+
expect(poly.contains?(2,2)).to be_false
|
54
|
+
expect(poly.contains?(0,1)).to be_false
|
55
|
+
expect(poly.contains?(1,2)).to be_true
|
56
|
+
expect(poly.contains?(2,1)).to be_true
|
57
|
+
expect(poly.contains?(1,1)).to be_true
|
58
|
+
expect(poly.contains?(1,0)).to be_true
|
59
|
+
expect(poly.contains?(0,0)).to be_true
|
60
|
+
end
|
61
|
+
|
62
|
+
it "finds point equal to a point" do
|
63
|
+
expect(poly.contains?(1,3)).to be_true
|
64
|
+
end
|
65
|
+
end
|
66
|
+
describe "insane shape" do
|
67
|
+
let(:points) { [
|
68
|
+
[10,10],
|
69
|
+
[15,15],
|
70
|
+
[25,5],
|
71
|
+
[30,20],
|
72
|
+
[30,10],
|
73
|
+
[40,30],
|
74
|
+
[31,15],
|
75
|
+
[31,25],
|
76
|
+
[5,24],
|
77
|
+
[10,10]
|
78
|
+
] }
|
79
|
+
|
80
|
+
it "finds if a point is within a polygon" do
|
81
|
+
expect(poly.contains?(10,10)).to be_true
|
82
|
+
expect(poly.contains?(10,11)).to be_true
|
83
|
+
expect(poly.contains?(25,6)).to be_true
|
84
|
+
expect(poly.contains?(1,1)).to be_false
|
85
|
+
expect(poly.contains?(40,30)).to be_true
|
86
|
+
expect(poly.contains?(40,30)).to be_true
|
87
|
+
expect(poly.contains?(40,31)).to be_false
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
describe "lat longs" do
|
92
|
+
let(:points) { [
|
93
|
+
[47.606973,-122.341175],
|
94
|
+
[47.610098,-122.333536],
|
95
|
+
[47.60408,-122.328086],
|
96
|
+
[47.601707,-122.336283]
|
97
|
+
] }
|
98
|
+
|
99
|
+
it "finds if a point is within the longs" do
|
100
|
+
expect(poly.contains?(47.601765,-122.325339)).to be_false
|
101
|
+
expect(poly.contains?(47.603769,-122.324642)).to be_false
|
102
|
+
expect(poly.contains?(47.606337,-122.334566)).to be_true
|
103
|
+
expect(poly.contains?(47.60489,-122.338943)).to be_true
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
describe "complicated lat longs" do
|
108
|
+
let(:points) { [
|
109
|
+
[47.606973,-122.341175],
|
110
|
+
[47.605353,-122.33345],
|
111
|
+
[47.610098,-122.333536],
|
112
|
+
[47.601765,-122.325339],
|
113
|
+
[47.601707,-122.336283]
|
114
|
+
] }
|
115
|
+
|
116
|
+
it "finds if a point is withing the area" do
|
117
|
+
expect(poly.contains?(47.606452,-122.331519)).to be_true
|
118
|
+
expect(poly.contains?(47.608651,-122.336068)).to be_false
|
119
|
+
expect(poly.contains?(47.606771,-122.334352)).to be_false
|
120
|
+
expect(poly.contains?(47.605121,-122.333064)).to be_true
|
121
|
+
expect(poly.contains?(47.609736,-122.332549)).to be_false
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
describe "a super complicated lat long" do
|
126
|
+
let(:points) { [
|
127
|
+
[47.601736,-122.32079],
|
128
|
+
[47.601794,-122.331991],
|
129
|
+
[47.608333,-122.337999],
|
130
|
+
[47.609693,-122.334652],
|
131
|
+
[47.608796,-122.333665],
|
132
|
+
[47.609606,-122.331562],
|
133
|
+
[47.611255,-122.33109],
|
134
|
+
[47.611342,-122.326927],
|
135
|
+
[47.613541,-122.328944],
|
136
|
+
[47.614062,-122.327399],
|
137
|
+
[47.61409,-122.32079]
|
138
|
+
] }
|
139
|
+
|
140
|
+
it "finds if a point is withing the area" do
|
141
|
+
expect(poly.contains?(47.608709,-122.335639)).to be_true
|
142
|
+
expect(poly.contains?(47.611024,-122.329888)).to be_true
|
143
|
+
expect(poly.contains?(47.613483,-122.321649)).to be_true
|
144
|
+
expect(poly.contains?(47.606279,-122.328215)).to be_true
|
145
|
+
expect(poly.contains?(47.610272,-122.332678)).to be_false
|
146
|
+
expect(poly.contains?(47.612007,-122.328429)).to be_false
|
147
|
+
expect(poly.contains?(47.605035,-122.335854)).to be_false
|
148
|
+
expect(poly.contains?(47.609288,-122.320747)).to be_false
|
149
|
+
expect(poly.contains?(47.610619,-122.320876)).to be_true
|
150
|
+
end
|
151
|
+
end
|
152
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pip
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Bookis Smuin
|
@@ -51,8 +51,10 @@ files:
|
|
51
51
|
- README.md
|
52
52
|
- Rakefile
|
53
53
|
- lib/pip.rb
|
54
|
+
- lib/pip/polygon.rb
|
54
55
|
- lib/pip/version.rb
|
55
56
|
- pip.gemspec
|
57
|
+
- spec/lib/polygon_spec.rb
|
56
58
|
homepage: https://github.com/bookis/pip.git
|
57
59
|
licenses:
|
58
60
|
- MIT
|
@@ -78,4 +80,5 @@ signing_key:
|
|
78
80
|
specification_version: 4
|
79
81
|
summary: Uses a napkin sketch algorithm to determine if a given point is contained
|
80
82
|
with any polygon
|
81
|
-
test_files:
|
83
|
+
test_files:
|
84
|
+
- spec/lib/polygon_spec.rb
|