-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
/
Copy pathFindIntersections.test.js
159 lines (131 loc) · 5.82 KB
/
FindIntersections.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import findIntersections, {
createPoint,
createEvent,
comparePoints,
compareSegmentsY,
calculateIntersectionPoint,
handlePotentialIntersection
} from '../FindIntersections'
describe('Geometry Functions', () => {
describe('createPoint', () => {
it('should create a point with given coordinates', () => {
const point = createPoint(1, 2)
expect(point).toEqual({ x: 1, y: 2 })
})
})
describe('createEvent', () => {
it('should create an event with the given parameters', () => {
const point = createPoint(1, 2)
const event = createEvent(point, ['left'], [])
expect(event).toEqual({
point,
types: ['left'],
segments: []
})
})
})
describe('comparePoints', () => {
it('should return negative if first point is less than second', () => {
const a = createPoint(1, 2)
const b = createPoint(2, 3)
expect(comparePoints(a, b)).toBeLessThan(0)
})
it('should return positive if first point is greater than second', () => {
const a = createPoint(2, 3)
const b = createPoint(1, 2)
expect(comparePoints(a, b)).toBeGreaterThan(0)
})
it('should return zero if points are equal', () => {
const a = createPoint(1, 2)
const b = createPoint(1, 2)
expect(comparePoints(a, b)).toBe(0)
})
})
describe('compareSegmentsY', () => {
it('should compare segments based on their start y-coordinates', () => {
const seg1 = { start: { x: 0, y: 1 }, end: { x: 1, y: 1 } }
const seg2 = { start: { x: 0, y: 2 }, end: { x: 1, y: 2 } }
expect(compareSegmentsY(seg1, seg2)).toBe(-1)
})
it('should return zero if segments have the same start y-coordinate', () => {
const seg1 = { start: { x: 0, y: 1 }, end: { x: 1, y: 1 } }
const seg2 = { start: { x: -1, y: 1 }, end: { x: -2, y: 1 } }
expect(compareSegmentsY(seg1, seg2)).toBe(0)
})
it('should return positive if first segment is greater than second', () => {
const seg1 = { start: { x: 0, y: 3 }, end: { x: 1, y: 3 } }
const seg2 = { start: { x: -1, y: 2 }, end: { x: -2, y: 2 } }
expect(compareSegmentsY(seg1, seg2)).toBe(1)
})
})
describe('calculateIntersectionPoint', () => {
it('should return null for parallel segments', () => {
const seg1 = { start: createPoint(0, 0), end: createPoint(5, 5) }
const seg2 = { start: createPoint(0, 1), end: createPoint(5, 6) }
expect(calculateIntersectionPoint(seg1, seg2)).toBeNull()
})
it('should return intersection point for intersecting segments', () => {
const seg1 = { start: createPoint(0, 0), end: createPoint(5, 5) }
const seg2 = { start: createPoint(0, 5), end: createPoint(5, 0) }
const intersection = calculateIntersectionPoint(seg1, seg2)
expect(intersection).toEqual(createPoint(2.5, 2.5))
})
it('should handle vertical and horizontal lines correctly', () => {
const seg1 = { start: createPoint(0, -1), end: createPoint(0, 1) } // vertical line
const seg2 = { start: createPoint(-1, 0), end: createPoint(1, 0) } // horizontal line
const intersection = calculateIntersectionPoint(seg1, seg2)
expect(intersection.x).toBeCloseTo(0) // Check for close proximity to zero
expect(intersection.y).toBeCloseTo(0) // Check for close proximity to zero
})
it('should handle coincident segments correctly', () => {
const seg1 = { start: createPoint(0, -1), end: createPoint(0, -3) }
const seg2 = { start: createPoint(0, -3), end: createPoint(0, -4) }
expect(calculateIntersectionPoint(seg1, seg2)).toBeNull() // should not intersect as they are collinear but not overlapping
})
it('should handle edge cases for intersection calculations', () => {
const seg1 = { start: createPoint(-10, -10), end: createPoint(-10, -10) }
const seg2 = { start: createPoint(-10, -10), end: createPoint(-10, -10) }
const intersection = calculateIntersectionPoint(seg1, seg2)
expect(intersection).toBeNull() // Expect null since they are coincident lines.
})
})
describe('handlePotentialIntersection', () => {
it('should push intersection event and intersection data when segments intersect', () => {
const events = []
const intersections = []
const seg1 = { start: createPoint(0, 0), end: createPoint(5, 5) }
const seg2 = { start: createPoint(0, 5), end: createPoint(5, -5) }
handlePotentialIntersection(seg1, seg2, events, intersections)
expect(events.length).toBeGreaterThan(0)
expect(intersections.length).toBeGreaterThan(0)
})
it('should not push anything when segments do not intersect', () => {
const events = []
const intersections = []
const seg1 = { start: createPoint(0, -10), end: createPoint(-10, -20) }
const seg2 = { start: createPoint(-20, -30), end: createPoint(-30, -40) }
handlePotentialIntersection(seg1, seg2, events, intersections)
expect(events.length).toBe(0)
expect(intersections.length).toBe(0)
})
})
describe('findIntersections', () => {
it('should find intersections among segments correctly', () => {
const segments = [
{ start: createPoint(0, 0), end: createPoint(5, 5) },
{ start: createPoint(0, 5), end: createPoint(5, 0) },
{ start: createPoint(6, 6), end: createPoint(7, 7) }
]
const result = findIntersections(segments)
expect(result.length).toBeGreaterThanOrEqual(1) // There should be at least one intersection
})
it('should return an empty array when no intersections exist', () => {
const segments = [
{ start: createPoint(10, 10), end: createPoint(20, 20) },
{ start: createPoint(30, 30), end: createPoint(40, 40) }
]
const result = findIntersections(segments)
expect(result).toEqual([]) // No intersections should be found
})
})
})