-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsegment-distance.test.ts
156 lines (132 loc) · 4.88 KB
/
segment-distance.test.ts
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
import { describe, expect, test } from "bun:test"
import {
segmentToBoundsMinDistance,
segmentToBoxMinDistance,
segmentToCircleMinDistance,
segmentToSegmentMinDistance,
} from "../src/segment-distance"
describe("segmentToSegmentMinDistance", () => {
test("intersecting segments return distance 0", () => {
const a = { x: 0, y: 0 }
const b = { x: 10, y: 10 }
const u = { x: 0, y: 10 }
const v = { x: 10, y: 0 }
expect(segmentToSegmentMinDistance(a, b, u, v)).toBe(0)
})
test("parallel segments return correct distance", () => {
const a = { x: 0, y: 0 }
const b = { x: 10, y: 0 }
const u = { x: 0, y: 5 }
const v = { x: 10, y: 5 }
expect(segmentToSegmentMinDistance(a, b, u, v)).toBe(5)
})
test("perpendicular segments return correct distance", () => {
const a = { x: 0, y: 0 }
const b = { x: 0, y: 10 }
const u = { x: 5, y: 5 }
const v = { x: 15, y: 5 }
expect(segmentToSegmentMinDistance(a, b, u, v)).toBe(5)
})
test("handles degenerate case (point to segment)", () => {
const a = { x: 0, y: 0 }
const b = { x: 0, y: 0 } // Zero-length segment (a point)
const u = { x: 3, y: 0 }
const v = { x: 10, y: 0 }
expect(segmentToSegmentMinDistance(a, b, u, v)).toBe(3)
})
test("closest points are segment endpoints", () => {
const a = { x: 0, y: 0 }
const b = { x: 1, y: 0 }
const u = { x: 3, y: 1 }
const v = { x: 4, y: 2 }
// Distance between point (1,0) and (3,1)
const expectedDistance = Math.sqrt(5)
expect(segmentToSegmentMinDistance(a, b, u, v)).toBeCloseTo(
expectedDistance,
)
})
})
describe("segmentToBoundsMinDistance", () => {
test("segment intersects bounds returns distance 0", () => {
const a = { x: 0, y: 5 }
const b = { x: 10, y: 5 }
const bounds = { minX: 5, minY: 0, maxX: 15, maxY: 10 }
expect(segmentToBoundsMinDistance(a, b, bounds)).toBe(0)
})
test("segment inside bounds returns distance 0", () => {
const a = { x: 6, y: 5 }
const b = { x: 10, y: 5 }
const bounds = { minX: 5, minY: 0, maxX: 15, maxY: 10 }
expect(segmentToBoundsMinDistance(a, b, bounds)).toBe(0)
})
test("segment outside bounds returns correct distance", () => {
const a = { x: 0, y: 15 }
const b = { x: 10, y: 15 }
const bounds = { minX: 5, minY: 0, maxX: 15, maxY: 10 }
expect(segmentToBoundsMinDistance(a, b, bounds)).toBe(5)
})
test("segment with one point inside bounds returns distance 0", () => {
const a = { x: 6, y: 5 }
const b = { x: 20, y: 5 }
const bounds = { minX: 5, minY: 0, maxX: 15, maxY: 10 }
expect(segmentToBoundsMinDistance(a, b, bounds)).toBe(0)
})
})
describe("segmentToBoxMinDistance", () => {
test("segment intersects box returns distance 0", () => {
const a = { x: 0, y: 5 }
const b = { x: 10, y: 5 }
const box = { center: { x: 10, y: 5 }, width: 10, height: 10 }
expect(segmentToBoxMinDistance(a, b, box)).toBe(0)
})
test("segment inside box returns distance 0", () => {
const a = { x: 6, y: 5 }
const b = { x: 10, y: 5 }
const box = { center: { x: 10, y: 5 }, width: 10, height: 10 }
expect(segmentToBoxMinDistance(a, b, box)).toBe(0)
})
test("segment outside box returns correct distance", () => {
const a = { x: 0, y: 15 }
const b = { x: 10, y: 15 }
const box = { center: { x: 10, y: 5 }, width: 10, height: 10 }
expect(segmentToBoxMinDistance(a, b, box)).toBe(5)
})
test("segment with one point inside box returns distance 0", () => {
const a = { x: 6, y: 5 }
const b = { x: 20, y: 5 }
const box = { center: { x: 10, y: 5 }, width: 10, height: 10 }
expect(segmentToBoxMinDistance(a, b, box)).toBe(0)
})
})
describe("segmentToCircleMinDistance", () => {
test("segment intersects circle returns distance 0", () => {
const a = { x: 0, y: 0 }
const b = { x: 10, y: 0 }
const circle = { x: 5, y: 0, radius: 2 }
expect(segmentToCircleMinDistance(a, b, circle)).toBe(0)
})
test("segment outside circle returns correct distance", () => {
const a = { x: 0, y: 5 }
const b = { x: 10, y: 5 }
const circle = { x: 5, y: 0, radius: 2 }
expect(segmentToCircleMinDistance(a, b, circle)).toBe(3)
})
test("segment endpoint inside circle returns distance 0", () => {
const a = { x: 4, y: 0 }
const b = { x: 10, y: 0 }
const circle = { x: 5, y: 0, radius: 2 }
expect(segmentToCircleMinDistance(a, b, circle)).toBe(0)
})
test("handles degenerate case (point to circle)", () => {
const a = { x: 10, y: 0 }
const b = { x: 10, y: 0 } // Zero-length segment (a point)
const circle = { x: 5, y: 0, radius: 2 }
expect(segmentToCircleMinDistance(a, b, circle)).toBe(3)
})
test("closest point is on the segment", () => {
const a = { x: 0, y: 5 }
const b = { x: 10, y: 5 }
const circle = { x: 5, y: 10, radius: 2 }
expect(segmentToCircleMinDistance(a, b, circle)).toBe(3)
})
})