|
| 1 | +import PlaneSweep from '../PlaneSweep' |
| 2 | + |
| 3 | +describe('PlaneSweep', () => { |
| 4 | + describe('Constructor', () => { |
| 5 | + test('creates an instance with valid segments', () => { |
| 6 | + const segments = [ |
| 7 | + { start: { x: 1, y: 1 }, end: { x: 4, y: 4 } }, |
| 8 | + { start: { x: 1, y: 4 }, end: { x: 4, y: 1 } } |
| 9 | + ] |
| 10 | + const intersection = new PlaneSweep(segments) |
| 11 | + expect(intersection).toBeInstanceOf(PlaneSweep) |
| 12 | + }) |
| 13 | + |
| 14 | + test('throws an error if segments array is invalid', () => { |
| 15 | + expect(() => new PlaneSweep([])).toThrow( |
| 16 | + 'segments must be a non-empty array of objects with both start and end properties.' |
| 17 | + ) |
| 18 | + expect(() => new PlaneSweep([{ start: { x: 0, y: 0 } }])).toThrow( |
| 19 | + 'segments must be a non-empty array of objects with both start and end properties.' |
| 20 | + ) |
| 21 | + }) |
| 22 | + }) |
| 23 | + |
| 24 | + describe('Intersection Detection', () => { |
| 25 | + test('detects intersections correctly', () => { |
| 26 | + const segments = [ |
| 27 | + { start: { x: 1, y: 1 }, end: { x: 4, y: 4 } }, |
| 28 | + { start: { x: 1, y: 4 }, end: { x: 4, y: 1 } }, |
| 29 | + { start: { x: 5, y: 5 }, end: { x: 6, y: 6 } } |
| 30 | + ] |
| 31 | + const intersection = new PlaneSweep(segments) |
| 32 | + const result = intersection.findIntersections() |
| 33 | + |
| 34 | + // Check if there is one intersection found |
| 35 | + expect(result).toHaveLength(1) // Ensure there's one intersection |
| 36 | + |
| 37 | + const intersectingPair = result[0] |
| 38 | + |
| 39 | + // Check that both segments in the intersection are part of the original segments |
| 40 | + const segment1 = intersectingPair.segment1 |
| 41 | + const segment2 = intersectingPair.segment2 |
| 42 | + |
| 43 | + const isSegment1Valid = |
| 44 | + (segment1.start.x === segments[0].start.x && |
| 45 | + segment1.start.y === segments[0].start.y && |
| 46 | + segment1.end.x === segments[0].end.x && |
| 47 | + segment1.end.y === segments[0].end.y) || |
| 48 | + (segment1.start.x === segments[1].start.x && |
| 49 | + segment1.start.y === segments[1].start.y && |
| 50 | + segment1.end.x === segments[1].end.x && |
| 51 | + segment1.end.y === segments[1].end.y) |
| 52 | + |
| 53 | + const isSegment2Valid = |
| 54 | + (segment2.start.x === segments[0].start.x && |
| 55 | + segment2.start.y === segments[0].start.y && |
| 56 | + segment2.end.x === segments[0].end.x && |
| 57 | + segment2.end.y === segments[0].end.y) || |
| 58 | + (segment2.start.x === segments[1].start.x && |
| 59 | + segment2.start.y === segments[1].start.y && |
| 60 | + segment2.end.x === segments[1].end.x && |
| 61 | + segment2.end.y === segments[1].end.y) |
| 62 | + |
| 63 | + expect(isSegment1Valid).toBe(true) |
| 64 | + expect(isSegment2Valid).toBe(true) |
| 65 | + }) |
| 66 | + |
| 67 | + test('returns an empty array if there are no intersections', () => { |
| 68 | + const segments = [ |
| 69 | + { start: { x: 1, y: 1 }, end: { x: 2, y: 2 } }, |
| 70 | + { start: { x: 3, y: 3 }, end: { x: 4, y: 4 } } |
| 71 | + ] |
| 72 | + const intersection = new PlaneSweep(segments) |
| 73 | + const result = intersection.findIntersections() |
| 74 | + expect(result).toEqual([]) |
| 75 | + }) |
| 76 | + |
| 77 | + test('handles vertical and horizontal lines', () => { |
| 78 | + const segments = [ |
| 79 | + { start: { x: 2, y: 0 }, end: { x: 2, y: 3 } }, // Vertical line |
| 80 | + { start: { x: 0, y: 2 }, end: { x: 3, y: 2 } } // Horizontal line |
| 81 | + ] |
| 82 | + const intersection = new PlaneSweep(segments) |
| 83 | + const result = intersection.findIntersections() |
| 84 | + |
| 85 | + // Check if intersection contains the correct segments regardless of order |
| 86 | + expect(result).toHaveLength(1) // Ensure there's one intersection |
| 87 | + |
| 88 | + const intersectingPair = result[0] |
| 89 | + |
| 90 | + // Check that both segments in the intersection are part of the original segments |
| 91 | + const isSegment1Valid = |
| 92 | + (intersectingPair.segment1.start.x === segments[0].start.x && |
| 93 | + intersectingPair.segment1.start.y === segments[0].start.y && |
| 94 | + intersectingPair.segment1.end.x === segments[0].end.x && |
| 95 | + intersectingPair.segment1.end.y === segments[0].end.y) || |
| 96 | + (intersectingPair.segment1.start.x === segments[1].start.x && |
| 97 | + intersectingPair.segment1.start.y === segments[1].start.y && |
| 98 | + intersectingPair.segment1.end.x === segments[1].end.x && |
| 99 | + intersectingPair.segment1.end.y === segments[1].end.y) |
| 100 | + |
| 101 | + const isSegment2Valid = |
| 102 | + (intersectingPair.segment2.start.x === segments[0].start.x && |
| 103 | + intersectingPair.segment2.start.y === segments[0].start.y && |
| 104 | + intersectingPair.segment2.end.x === segments[0].end.x && |
| 105 | + intersectingPair.segment2.end.y === segments[0].end.y) || |
| 106 | + (intersectingPair.segment2.start.x === segments[1].start.x && |
| 107 | + intersectingPair.segment2.start.y === segments[1].start.y && |
| 108 | + intersectingPair.segment2.end.x === segments[1].end.x && |
| 109 | + intersectingPair.segment2.end.y === segments[1].end.y) |
| 110 | + |
| 111 | + expect(isSegment1Valid).toBe(true) |
| 112 | + expect(isSegment2Valid).toBe(true) |
| 113 | + }) |
| 114 | + }) |
| 115 | +}) |
0 commit comments