require 'spec_helper' describe GeometricPolygon do describe '#area' do it 'to be area of an empty polygon' do expect(GeometricPolygon.new([]).area).to eq(0) end it 'to be area of a point' do expect(GeometricPolygon.new([GeometricPoint.new(1,1)]).area).to eq(0) end it 'to be area of a line' do expect(GeometricPolygon.new([GeometricPoint.new(0,0), GeometricPoint.new(1,1)]).area).to eq(0) end it 'to be area of a triangle' do expect(GeometricPolygon.new([GeometricPoint.new(0,0), GeometricPoint.new(1,1), GeometricPoint.new(0,1)]).area).to eq(0.5) end it 'to be area of a square' do expect(GeometricPolygon.new([GeometricPoint.new(0,0), GeometricPoint.new(0,2), GeometricPoint.new(2,2), GeometricPoint.new(2,0)]).area).to eq(4) end it 'to be area of a quadralateral' do expect(GeometricPolygon.new([GeometricPoint.new(4,10), GeometricPoint.new(9,7), GeometricPoint.new(11,2), GeometricPoint.new(2,2)]).area).to eq(45.5) end end describe '#bounding_box' do it 'to be a rectangle' do polygon = GeometricPolygon.new([ GeometricPoint.new(-1, -1), GeometricPoint.new(1, -1), GeometricPoint.new(1, 1), GeometricPoint.new(-1, 1) ]) expect(polygon.bounding_box.leftbottom).to eq(GeometricPoint.new(-1, -1)) expect(polygon.bounding_box.righttop).to eq(GeometricPoint.new(1, 1)) end it 'to be a sloped rectangle' do polygon = GeometricPolygon.new([ GeometricPoint.new(-1, 0), GeometricPoint.new(0, -1), GeometricPoint.new(1, 0), GeometricPoint.new(0, 1) ]) expect(polygon.bounding_box.leftbottom).to eq(GeometricPoint.new(-1, -1)) expect(polygon.bounding_box.righttop).to eq(GeometricPoint.new(1, 1)) end it 'to be nonconvex' do polygon = GeometricPolygon.new([ GeometricPoint.new(0, 2), GeometricPoint.new(2, 0), GeometricPoint.new(5, 1), GeometricPoint.new(3, 8), GeometricPoint.new(3, 1), GeometricPoint.new(1, 4) ]) expect(polygon.bounding_box.leftbottom).to eq(GeometricPoint.new(0, 0)) expect(polygon.bounding_box.righttop).to eq(GeometricPoint.new(5, 8)) end end describe '#contains?' do it 'to be convex' do polygon = GeometricPolygon.new([ GeometricPoint.new(0, 0), GeometricPoint.new(1, 0), GeometricPoint.new(1, 1), GeometricPoint.new(0, 1) ]) inner = GeometricPoint.new(0.5, 0.5) outer = GeometricPoint.new(1.5, 1.5) on_edge = GeometricPoint.new(0.5, 1) at_vertex = GeometricPoint.new(1, 1) expect(polygon.contains?(inner)).to eq(true) expect(polygon.contains?(outer)).to eq(false) expect(polygon.contains?(on_edge)).to eq(true) expect(polygon.contains?(at_vertex)).to eq(true) end it 'to be nonconvex' do polygon = GeometricPolygon.new([ GeometricPoint.new(0, 0), GeometricPoint.new(0, 6), GeometricPoint.new(4, 6), GeometricPoint.new(4, 4), GeometricPoint.new(2, 4), GeometricPoint.new(2, 2), GeometricPoint.new(4, 2), GeometricPoint.new(4, 0) ]) inner = GeometricPoint.new(1, 5) outer = GeometricPoint.new(7, 5) expect(polygon.contains?(inner)).to eq(true) expect(polygon.contains?(outer)).to eq(false) end end describe '#edges' do it 'to be regular' do vertices = [ GeometricPoint.new(0, 0), GeometricPoint.new(1, 0), GeometricPoint.new(1, 1), GeometricPoint.new(0, 1) ] polygon = GeometricPolygon.new(vertices) expected_edges = [ GeometricSegment.new_by_arrays(vertices[0], vertices[1]), GeometricSegment.new_by_arrays(vertices[1], vertices[2]), GeometricSegment.new_by_arrays(vertices[2], vertices[3]), GeometricSegment.new_by_arrays(vertices[3], vertices[0]) ] expect(polygon.edges).to eq(expected_edges) end it 'to be self intersecting' do vertices = [ GeometricPoint.new(0, 0), GeometricPoint.new(4, 0), GeometricPoint.new(3, 2), GeometricPoint.new(1, -2) ] polygon = GeometricPolygon.new(vertices) expected_edges = [ GeometricSegment.new_by_arrays(vertices[0], vertices[1]), GeometricSegment.new_by_arrays(vertices[1], vertices[2]), GeometricSegment.new_by_arrays(vertices[2], vertices[3]), GeometricSegment.new_by_arrays(vertices[3], vertices[0]) ] expect(polygon.edges).to eq(expected_edges) end end end