require 'spec_helper' describe GeometricSegment do describe '#initialize_by_points' do point1 = GeometricPoint.new(1, 2) point2 = GeometricPoint.new(3, 4) segment = GeometricSegment.new(point1, point2) it 'to be equal' do expect(segment.point1).to eq(point1) expect(segment.point2).to eq(point2) end it 'to be not equal' do expect(segment.point1).not_to eq(point2) expect(segment.point2).not_to eq(point1) end end describe '#initialize_by_arrays' do point1 = GeometricPoint.new(1, 2) point2 = GeometricPoint.new(3, 4) segment = GeometricSegment.new_by_arrays([1, 2], [3, 4]) it 'to be equal' do expect(segment.point1).to eq(point1) expect(segment.point2).to eq(point2) end it 'to be not equal' do expect(segment.point1).not_to eq(point2) expect(segment.point2).not_to eq(point1) end end describe '#bounds' do point1 = GeometricPoint.new(1, 1) point2 = GeometricPoint.new(2, 2) segment = GeometricSegment.new(point1, point2) it 'to be the bottommost point' do expect(segment.bottommost_endpoint).to eq(point1) end it 'to be the leftmost point' do expect(segment.leftmost_endpoint).to eq(point1) end it 'to be the rightmost point' do expect(segment.rightmost_endpoint).to eq(point2) end it 'to be the topmost point' do expect(segment.topmost_endpoint).to eq(point2) end end describe '#contains_point?' do it 'to be belongs to horizontal segment' do expect(GeometricSegment.new_by_arrays([2, 1], [4, 1]).contains_point?(GeometricPoint.new(3, 1))).to eq(true) end it 'to be belongs to inclined segment' do expect(GeometricSegment.new_by_arrays([2, 2], [4, 4]).contains_point?(GeometricPoint.new(3, 3))).to eq(true) end it 'to be is segment endpoint' do expect(GeometricSegment.new_by_arrays([2, 1], [4, 1]).contains_point?(GeometricPoint.new(2, 1))).to eq(true) end it 'to be not belonging to segment' do expect(GeometricSegment.new_by_arrays([2, 1], [4, 1]).contains_point?(GeometricPoint.new(2, 3))).to eq(false) end it 'to be on same horizontal line' do expect(GeometricSegment.new_by_arrays([2, 1], [4, 1]).contains_point?(GeometricPoint.new(1, 1))).to eq(false) end it 'to be on same inclined line' do expect(GeometricSegment.new_by_arrays([2, 2], [4, 4]).contains_point?(GeometricPoint.new(1, 1))).to eq(false) end end describe '#distance_to' do it 'to be distance to point on segment' do expect(GeometricSegment.new_by_arrays([0, 2], [2, 0]).distance_to(GeometricPoint.new(1, 1))).to eq(0) end it 'to be distance to flat segment' do expect(GeometricSegment.new_by_arrays([0, 1], [2, 1]).distance_to(GeometricPoint.new(1, 2))).to eq(1) end it 'to be distance to segment start' do expect(GeometricSegment.new_by_arrays([1, 1], [2, 1]).distance_to(GeometricPoint.new(0, 1))).to eq(1) end it 'to be distance to segment end' do expect(GeometricSegment.new_by_arrays([0, 1], [0, 2]).distance_to(GeometricPoint.new(0, 3))).to eq(1) end end describe '#intersection_point_with' do it 'to be regular case' do expect(GeometricSegment.new_by_arrays([0, 0], [2, 2]).intersection_point_with(GeometricSegment.new_by_arrays([0, 2], [2, 0]))).to eq(GeometricPoint.new(1, 1)) end it 'to be intersected at the endpoint' do expect(GeometricSegment.new_by_arrays([0, 0], [2, 2]).intersection_point_with(GeometricSegment.new_by_arrays([0, 2], [2, 2]))).to eq(GeometricPoint.new(2, 2)) end end describe '#intersects_with?' do it 'to be segments intersect' do expect(GeometricSegment.new_by_arrays([0, 0], [2, 0]).intersects_with?(GeometricSegment.new_by_arrays([1, 1], [1, -1]))).to eq(true) end it 'to be segments does not intersect but lay on intersecting lines' do expect(GeometricSegment.new_by_arrays([0, 0], [0, 2]).intersects_with?(GeometricSegment.new_by_arrays([1, 1], [3, 1]))).to eq(false) end it 'to be segments does not intersect but line contains end point' do expect(GeometricSegment.new_by_arrays([0, 0], [0, 2]).intersects_with?(GeometricSegment.new_by_arrays([1, 0], [3, 0]))).to eq(false) end it 'to be segment contains endpoint' do expect(GeometricSegment.new_by_arrays([0, 0], [2, 2]).intersects_with?(GeometricSegment.new_by_arrays([0, 0], [2, 0]))).to eq(true) end it 'to be segments parralel' do expect(GeometricSegment.new_by_arrays([0, 1], [2, 1]).intersects_with?(GeometricSegment.new_by_arrays([0, 0], [2, 0]))).to eq(false) end it 'to be segments parallel and have common endpoint' do expect(GeometricSegment.new_by_arrays([0, 0], [1, 0]).intersects_with?(GeometricSegment.new_by_arrays([1, 0], [2, 0]))).to eq(true) end it 'to be segments overlap' do expect(GeometricSegment.new_by_arrays([0, 0], [2, 0]).intersects_with?(GeometricSegment.new_by_arrays([1, 0], [3, 0]))).to eq(true) end it 'to be segments overlap on vertical line' do expect(GeometricSegment.new_by_arrays([0, 0], [0, 2]).intersects_with?(GeometricSegment.new_by_arrays([0, 1], [0, 3]))).to eq(true) end it 'to be segments lie on one line' do expect(GeometricSegment.new_by_arrays([0, 0], [1, 0]).intersects_with?(GeometricSegment.new_by_arrays([2, 0], [3, 0]))).to eq(false) end it 'to be segments lie on one vertical line' do expect(GeometricSegment.new_by_arrays([0, 0], [0, 1]).intersects_with?(GeometricSegment.new_by_arrays([0, 2], [0, 3]))).to eq(false) end end describe '#length' do it 'to be parallel to axis' do expect(GeometricSegment.new_by_arrays([1, 1], [1, 2]).length).to eq(1) expect(GeometricSegment.new_by_arrays([1, 1], [2, 1]).length).to eq(1) end it 'to be inclined' do expect(GeometricSegment.new_by_arrays([1, 1], [2, 2]).length).to eq(Math.sqrt(2)) end end describe '#lies_on_one_line_with?' do it 'to be parralel but on different lines' do expect(GeometricSegment.new_by_arrays([0, 1], [2, 1]).lies_on_one_line_with?(GeometricSegment.new_by_arrays([0, 0], [2, 0]))).to eq(false) end it 'to be not parralel' do expect(GeometricSegment.new_by_arrays([0, 0], [2, 0]).lies_on_one_line_with?(GeometricSegment.new_by_arrays([0, 1], [2, 2]))).to eq(false) end it 'to be segment that lies on one horizontal line' do expect(GeometricSegment.new_by_arrays([0, 0], [1, 0]).lies_on_one_line_with?(GeometricSegment.new_by_arrays([2, 0], [3, 0]))).to eq(true) end it 'to be segment that lies on one vertical line' do expect(GeometricSegment.new_by_arrays([0, 0], [0, 1]).lies_on_one_line_with?(GeometricSegment.new_by_arrays([0, 2], [0, 3]))).to eq(true) end end describe '#overlaps?' do it 'to be overlaped' do expect(GeometricSegment.new_by_arrays([0, 0], [2, 0]).overlaps?(GeometricSegment.new_by_arrays([1, 0], [3, 0]))).to eq(true) end it 'to be overlapes same segment' do expect(GeometricSegment.new_by_arrays([0, 0], [2, 0]).overlaps?(GeometricSegment.new_by_arrays([0, 0], [2, 0]))).to eq(true) end it 'to be overlap intersects on one point' do expect(GeometricSegment.new_by_arrays([0, 0], [1, 0]).overlaps?(GeometricSegment.new_by_arrays([1, 0], [1, 1]))).to eq(false) end it 'to be overlap lies on one horizontal line' do expect(GeometricSegment.new_by_arrays([0, 0], [2, 0]).overlaps?(GeometricSegment.new_by_arrays([4, 0], [6, 0]))).to eq(false) end it 'to be overlap lies on one vertical line' do expect(GeometricSegment.new_by_arrays([0, 0], [0, 1]).overlaps?(GeometricSegment.new_by_arrays([0, 2], [0, 3]))).to eq(false) end end describe '#parallel_to?' do it 'to be parallel' do expect(GeometricSegment.new_by_arrays([0, 0], [2, 1]).parallel_to?(GeometricSegment.new_by_arrays([1, 1], [5, 3]))).to eq(true) end it 'to be not parallel' do expect(GeometricSegment.new_by_arrays([0, 0], [2, 1]).parallel_to?(GeometricSegment.new_by_arrays([1, 1], [2, 2]))).to eq(false) end it 'to be lays on the same line' do expect(GeometricSegment.new_by_arrays([0, 0], [1, 1]).parallel_to?(GeometricSegment.new_by_arrays([2, 2], [3, 3]))).to eq(true) end end describe '#to_vector' do it 'to be parallel' do expect(GeometricSegment.new_by_arrays([1, 1], [3, 2]).to_vector).to eq(GeometricVector.new(2, 1)) end it 'to be not parallel' do expect(GeometricSegment.new_by_arrays([1, 1], [2, 2]).to_vector).to eq(GeometricVector.new(1, 1)) expect(GeometricSegment.new_by_arrays([2, 2], [1, 1]).to_vector).to eq(GeometricVector.new(-1, -1)) end end end