-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo_nd.m
70 lines (47 loc) · 1.37 KB
/
demo_nd.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
%% N-D Demo
% Test cases for computing distance between point and line in N-D
%% 2D Case
% Two points
p1 = [0; 0]; % [x y]
p2 = [1; 2];
% Generate line equation
[a, d] = line_nd(p1, p2);
% Third point
p3 = [-1 4];
% How close is third point to line?
[dist, phat] = dist_nd(p3, a, d);
fprintf('**N-D demo, 2D case**\n');
fprintf('p1: [x1, y1] = [%f, %f]\n', p1(1), p1(2));
fprintf('p2: [x2, y2] = [%f, %f]\n', p2(1), p2(2));
fprintf('p3: [x3, y3] = [%f, %f]\n', p3(1), p3(2));
fprintf('Distance from line to p3: %f\n', dist);
fprintf('Closest point on line to p3: [%f, %f]\n', phat(1), phat(2));
%% 3D Case
% Two points
p1 = [0; 0; 0];
p2 = [1; 0; 0];
% Generate line equation
[a, d] = line_nd(p1, p2);
% Third point
p3 = [1; 1; 0];
% How close is third point to line?
[dist, phat] = dist_nd(p3, a, d);
fprintf('**N-D demo, 3D case**\n');
fprintf('Distance from line to p3: %f\n', dist);
fprintf('Closest point on line to p3: [%f, %f, %f]\n', ...
phat(1), phat(2), phat(3));
%% 4D Case
n = 4;
% Two points
p1 = randn(n, 1);
p2 = randn(n, 1);
% Generate line equation
[a, d] = line_nd(p1, p2);
% Third point
p3 = randn(n, 1);
% How close is third point to line?
[dist, phat] = dist_nd(p3, a, d);
fprintf('**N-D demo, 4D case**\n');
fprintf('Distance from line to p3: %f\n', dist);
fprintf('Closest point: [%f, %f, %f, %f]\n', ...
phat(1), phat(2), phat(3), phat(4));