$: << '../lib' require 'geo_graf/intersection_calculator' describe GeoGraf::IntersectionCalculator do describe "#intersections" do subject(:intersections) { described_class.new(input).intersections } context "given no shape" do let(:input) { [] } it "returns no intersections" do expect(intersections).to be_empty end end context "given only one shape" do let(:input) { [{ id: 1, polygon_coords: [[-2, -2], [2, -2], [-2, 1], [-2, -2]] }] } it "returns no intersections" do expect(intersections).to be_empty end end context "given two nonintersecting shapes" do let(:input) { [ { id: 1, polygon_coords: [[-2, -2], [2, -2], [-2, 1], [-2, -2]] }, { id: 2, polygon_coords: [[100, 100], [102, 100], [100, 101], [100, 100]] } ] } it "returns no intersections" do expect(intersections).to be_empty end end context "given two identical shapes" do let(:input) { [ { id: 1, polygon_coords: [[-2, -2], [2, -2], [-2, 1], [-2, -2]] }, { id: 2, polygon_coords: [[-2, -2], [2, -2], [-2, 1], [-2, -2]] } ] } it "returns an intersection of the second one containing the first one 100%" do expect(intersections).to match_array([{id: 1, contained_area_percentage: 100, container_id: 2}]) end end context "given one square inside the other" do let(:input) { [ { id: 1, polygon_coords: [[-2, -2], [2, -2], [2, 2], [-2, 2], [-2, -2]] }, { id: 2, polygon_coords: [[-2, -2], [0, -2], [0, 0], [-2, 0], [-2, -2]] } ] } it "returns an intersection of the first one containing the second one 100%" do expect(intersections).to match_array([{id: 2, contained_area_percentage: 100, container_id: 1}]) end end context "given one square intersecting the other 25%" do let(:input) { [ { id: 1, polygon_coords: [[-2, -2], [2, -2], [2, 2], [-2, 2], [-2, -2]] }, { id: 2, polygon_coords: [[-3, -3], [-1, -3], [-1, -1], [-3, -1], [-3, -3]] } ] } it "returns an intersection of the first one containing the second one 25%" do expect(intersections).to match_array([{id: 2, contained_area_percentage: 25, container_id: 1}]) end end context "given one square intersecting the other square of the same size 50%" do let(:input) { [ { id: 1, polygon_coords: [[-2, -2], [2, -2], [2, 2], [-2, 2], [-2, -2]] }, { id: 2, polygon_coords: [[-4, -2], [0, -2], [0, 2], [-4, 2], [-4, -2]] } ] } it "returns an intersection of the second one containing the first one 50%" do expect(intersections).to match_array([{id: 1, contained_area_percentage: 50, container_id: 2}]) end end context "given multiple shapes of which some intersect" do let(:input) { [ { id: 1, polygon_coords: [[-2, -2], [2, -2], [2, 2], [-2, 2], [-2, -2]] }, { id: 2, polygon_coords: [[-1, 1], [-4, 1], [-4, -2], [-1, -2], [-1, 1]] }, { id: 3, polygon_coords: [[-3, -3], [-1, -3], [-1, -1], [-3, -1], [-3, -3]] }, { id: 4, polygon_coords: [[-1, 8], [-3, 8], [-3, 6], [-1, 6], [-1, 8]] }, { id: 5, polygon_coords: [[11, 6], [11, 0], [2, 6], [11, 6]] }, { id: 6, polygon_coords: [[5, 4], [8, 4], [8, 2], [5, 2], [5, 4]] }, { id: 7, polygon_coords: [[3, -3], [1, -3], [1, -1], [3, -1], [3, -3]] } ] } it "returns 4 intersections" do expect(intersections).to match_array([ {id: 2, contained_area_percentage: 33, container_id: 1}, {id: 3, contained_area_percentage: 50, container_id: 2}, {id: 3, contained_area_percentage: 25, container_id: 1}, {id: 6, contained_area_percentage: 49, container_id: 5}, # 49% because of it's not a plane but a sphere {id: 7, contained_area_percentage: 25, container_id: 1} ]) end end context "given multiple shapes on the both sides of 180 degrees line of which some intersect" do let(:input) { [ { id: 1, polygon_coords: [[-2, 178], [2, 178], [2, -178], [-2, -178], [-2, 178]] }, { id: 2, polygon_coords: [[-1, -179], [-4, -179], [-4, 178], [-1, 178], [-1, -179]] }, { id: 3, polygon_coords: [[-3, 177], [-1, 177], [-1, 179], [-3, 179], [-3, 177]] }, { id: 4, polygon_coords: [[-1, -172], [-3, -172], [-3, -174], [-1, -174], [-1, -172]] }, { id: 5, polygon_coords: [[11, -174], [11, 180], [2, -174], [11, -174]] }, { id: 6, polygon_coords: [[5, -176], [8, -176], [8, -178], [5, -178], [5, -176]] }, { id: 7, polygon_coords: [[3, 177], [1, 177], [1, 179], [3, 179], [3, 177]] } ] } it "returns 4 intersections" do expect(intersections).to match_array([ {id: 2, contained_area_percentage: 33, container_id: 1}, {id: 3, contained_area_percentage: 50, container_id: 2}, {id: 3, contained_area_percentage: 25, container_id: 1}, {id: 6, contained_area_percentage: 49, container_id: 5}, # 49% because of it's not a plane but a sphere {id: 7, contained_area_percentage: 25, container_id: 1} ]) end end end end