-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_line_intersect.m
77 lines (68 loc) · 1.62 KB
/
test_line_intersect.m
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
% test line segment intersection
% runtests('test_line_intersect')
ABSTOL = 1e-10;
assert_tol = @(x, y) assert(all(abs(x-y)<ABSTOL));
%% Test 1: Lines crossing at 45 deg angle
xa = [0, 1];
ya = [0, 1];
xb = [0, 1];
yb = [1, 0];
[xi, yi] = line_intersect(xa, ya, xb, yb);
assert_tol(xi, 0.5);
assert_tol(yi, 0.5);
%% Test 2: One vertical line
xa = [0, 1];
ya = [0.5, 0.5];
xb = [0.5, 0.5];
yb = [1, 0];
[xi, yi] = line_intersect(xa, ya, xb, yb);
assert_tol(xi, 0.5);
assert_tol(yi, 0.5);
%% Test 3: Parallel lines
xa = [0, 1];
ya = [0, 1];
xb = xa+0.1;
yb = ya+0.1;
[xi, yi] = line_intersect(xa, ya, xb, yb);
assert(isnan(xi));
assert(isnan(yi));
%% Test 4: Identical lines
xa = [0, 1];
ya = [0, 1];
xb = xa;
yb = ya;
[xi, yi] = line_intersect(xa, ya, xb, yb);
assert(isnan(xi));
assert(isnan(yi));
%% Test 4.1: Colinear segments
xa = [0, 1];
ya = [0, 1];
xb = [0.5, 1];
yb = [0.5, 1];
[xi, yi] = line_intersect(xa, ya, xb, yb);
assert(isnan(xi));
assert(isnan(yi));
%% Test 5: Multiple lines for set B
xa = [0, 1];
ya = [0, 1];
xb = [0.0, 0.0, 0.5, 0.0; ...
1.0, 1.0, 0.5, 1.0];
yb = [1.0, 0.5, 0.0, 1.0; ...
0.0, 0.5, 1.0, 2.0];
[xi, yi] = line_intersect(xa, ya, xb, yb);
assert_tol(xi(1:3), [0.5, 0.5, 0.5]);
assert_tol(yi(1:3), [0.5, 0.5, 0.5]);
assert(isnan(xi(4)));
assert(isnan(yi(4)));
%% Test 6: Multiple lines for set A
xb = [0, 1];
yb = [0, 1];
xa = [0.0, 0.0, 0.5, 0.0; ...
1.0, 1.0, 0.5, 1.0];
ya = [1.0, 0.5, 0.0, 1.0; ...
0.0, 0.5, 1.0, 2.0];
[xi, yi] = line_intersect(xa, ya, xb, yb);
assert_tol(xi(1:3), [0.5; 0.5; 0.5]);
assert_tol(yi(1:3), [0.5; 0.5; 0.5]);
assert(isnan(xi(4)));
assert(isnan(yi(4)));