Skip to content

Instantly share code, notes, and snippets.

@JonahMoses
Created March 18, 2014 20:20
Show Gist options
  • Save JonahMoses/9628670 to your computer and use it in GitHub Desktop.
Save JonahMoses/9628670 to your computer and use it in GitHub Desktop.
WrapperRangeSpec
require 'spec_helper'
2
3 describe WrapperRange do
4
5 it "wraps from 175 to 180 to -175" do
6 range = WrapperRange.new(175,-175,180)
7 range.include?(179).should be_true
8 range.include?(-179).should be_true
9 end
10
11 it "wraps from -175 to -180 to 175" do
12 range = WrapperRange.new(-175,175,180)
13 range.include?(-179).should be_true
14 range.include?(179).should be_true
15 end
16
17 it "wraps from 5 to 0 to -5" do
18 range = WrapperRange.new(-5,5,180)
19 range.include?(-4).should be_true
20 range.include?(4).should be_true
21 end
22
23 it "wraps from 80 to 90 to -80" do
24 range = WrapperRange.new(80,-80,90)
25 range.include?(89).should be_true
26 range.include?(90).should be_true
27 range.include?(-89).should be_true
28 range.include?(-90).should be_true
29 end
30
31 end
require 'csv'
2
3 class Rectangle
4 attr_reader :point1, :point2
5
6 def initialize(point1, point2)
7 @point1 = point1
8 @point2 = point2
9 #@lat_range = Range.new(*[point1.lat,point2.lat].sort)
10 #@long_range = Range.new(*[point1.long,normalize_longitude(point2.long)].sort)
11 @lat_range = WrapperRange.new(point1.lat, point2.lat, 90)
12 @long_range = WrapperRange.new(point1.long, point2.long, 180)
13 end
14
15 #comments:
16 #inputs: 175 (left), -175 (right), 180 (max)
17 # includes?(-178)
18 # (175..180).include?(-178) => false
19 # (left..max)
20 # (-180..-175).include?(-178) => true
21 # (-max..right)
22 # => true
23
24 # def normalize_longitude(long)
25 # if (-180..-90).include?(long)
26 # return long += 361
27 # else
28 # return long
29 # end
30 # end
31
32 def valid?
33 [different_lats, different_longs, valid_points?].all?
34 end
35
36 def includes?(point)
37 binding.pry
38 @lat_range.include?(point.lat) && @long_range.include?(point.long)
39 end
40
41 def points
42 [@point1, @point2]
43 end
44
45 def self.from_input(input_pairs)
46 p1_lat, p1_long, p2_lat, p2_long = CSV.parse(input_pairs).flatten
47 new(Point.new(p1_lat, p1_long), Point.new(p2_lat, p2_long))
48 end
49
50 private
51
52 def different_lats
53 @point1.lat != @point2.lat
54 end
55
56 def different_longs
57 @point1.long != @point2.long
58 end
59
60 def valid_points?
61 points.all?(&:valid?)
62 end
63
64 end
require 'spec_helper'
2
3 describe Rectangle do
4 let(:good_point1) { Point.new(25,60) }
5 let(:good_point2) { Point.new(-35,170) }
6 let(:bad_point) { Point.new(-350,-350) }
7 let(:rectangle) {
8 input_pairs = "45,45,-50,-175"
9 Rectangle.from_input(input_pairs)
10 }
11
12 context "validity" do
13 it "is invalid if any point of rectangle input is invalid" do
14 Rectangle.new(good_point1, bad_point).should_not be_valid
15 end
16
17 it "is invalid if both points equal each other" do
18 Rectangle.new(good_point1, good_point1).should_not be_valid
19 end
20
21 it "is valid if both points are good and not equal each other" do
22 Rectangle.new(good_point1, good_point2).should be_valid
23 end
24 end
25
26 context "rectangle inside/outside" do
27 xit "builds rectangle from csv line" do
28 rectangle.point1.lat.should == good_point1.lat
29 rectangle.point1.long.should == good_point1.long
30 rectangle.point2.lat.should == good_point2.lat
31 rectangle.point2.long.should == good_point2.long
32 end
33
34 it "knows what point is inside or not" do
35 rectangle.includes?(good_point1).should be_true
36 rectangle.includes?(good_point2).should be_true
37 end
38
39 xit "knows when a point is outside" do
40 rectangle.includes?(Point.new(35,-35)).should be_false
41 rectangle.includes?(Point.new(-60,135)).should be_false
42 rectangle.includes?(Point.new(-60,135)).should be_false
43 rectangle.includes?(Point.new(65,130)).should be_false
44 rectangle.includes?(Point.new(28,-150)).should be_false
45 rectangle.includes?(Point.new(-30,-150)).should be_false
46 end
47 end
48
49 context "international dateline / prime meridian" do
50 let(:international_battleship1) { Point.new(1,179) }
51 let(:international_rectangle1) {
52 input_pairs = "5,175,-5,-175"
53 Rectangle.from_input(input_pairs)
54 }
55 let(:international_battleship2) { Point.new(1,-179) }
56 let(:international_rectangle2) {
57 input_pairs = "30,140,-30,-140"
58 Rectangle.from_input(input_pairs)
59 }
60
61 let(:prime_battleship1) { Point.new(25,-1) }
62 let(:prime_battleship2) { Point.new(25,1) }
63 let(:prime_rectangle) {
64 input_pairs = "50,-25,0,25"
65 Rectangle.from_input(input_pairs)
66 }
67
68 xit "handles the international dateline correctly" do
69 international_rectangle1.includes?(international_battleship1).should be_true
70 end
71
72 xit "passes through the 180 degrees dateline" do
73 international_rectangle2.includes?(international_battleship1).should be_true
74 international_rectangle2.includes?(international_battleship2).should be_true
75 end
76
77 xit "passes through the 0 degree dateline" do
78 prime_rectangle.includes?(prime_battleship1).should be_true
79 prime_rectangle.includes?(prime_battleship2).should be_true
80 end
81 end
82
83 end
1 class WrapperRange
2 attr_reader :left, :right, :max
3
4 def initialize(left, right, max)
5 @left = left
6 @right = right
7 @max = max
8 end
9
10 def include?(point)
11 (left..max).include?(point) || (-max..right).includ e?(point) 12 end
13
14 end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment