-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathptlabel_along.m
123 lines (100 loc) · 3.07 KB
/
ptlabel_along.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
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
function [hl, ht] = ptlabel_along( xc, yc, tc, t, dst, LineSpec )
%PTLABEL_ALONG Label points along a curve with nice offset
% [HL, HT] = PTLABEL_ALONG( XC, YC, TC, T ) plots and labels points at
% parameter T along a parametric curve (XC, YC, TC). The text labels are
% nicely offset from the curve.
%
% [HL, HT] = PTLABEL_ALONG( XC, YC, TC, T, DST ) as above, but with the
% label offset distance fraction specified in DST (0.02). The offset
% distance is specified as a fraction of the X-Axis length. A Positive
% DST will place the labels on the left side of the curve, negative DST
% to the right.
%
% [HL, HT] = PTLABEL_ALONG( XC, YC, TC, T, DST, LINESPEC ) as above, but
% will plot the desired parametric points with line specifier string
% LINESPEC ('k.').
%
% The line or point plot handle is returned in HL. The text label handle
% is returned in HT.
%
% If the data or plot aspect ratios change significantly after the call
% to PTLABEL_ALONG, the text labels may not look properly offset.
%
% Example:
% th = linspace( 0, 360, 181 );
% thmark = 0:15:359;
% x = cos( th * pi / 180 );
% y = sin( th * pi / 180 );
% plot( x, y );
% ptlabel_along( x, y, th, thmark );
%
% See also TEXT, PLOT, DASPECT, PBASPECT.
% Rob McDonald
% 15 June 2021 v. 1.0 -- Original version.
% Store hold setting
holdsetting = ishold;
xlog = strcmp( get( gca, 'XScale' ), 'log' );
ylog = strcmp( get( gca, 'YScale' ), 'log' );
% Transform data if in log-space
if ( xlog )
xc = log( xc );
end
if ( ylog )
yc = log( yc );
end
% Default offset distance
if ( nargin < 5 )
dst = 0.02;
end
% Default black dots
if ( nargin < 6 )
LineSpec = 'k.';
end
% Grab axis limits
ax = axis;
dxax = ax(2)-ax(1);
% Scale offset by plot width
off = dst * dxax;
% Grab aspect ratios to correct scaling
% Data aspect ratio
da = daspect( );
ard = da( 2 ) / da( 1 );
% Plot aspect ratio (paper space)
pa = pbaspect( );
arp = pa( 2 ) / pa( 1 );
% Compute overall aspect ratio including
ar = ard / arp;
% Done handling input options.
tscale = max( tc ) - min( tc );
dt = tscale / 100;
% Find marker points along the parameterized curve
xt = interp1( tc, xc, t );
yt = interp1( tc, yc, t );
% Find vectors in local direction of curve
dxdt = ( interp1( tc, xc, t + dt ) - xt ) ./ dt;
dydt = ( interp1( tc, yc, t + dt ) - yt ) ./ dt ./ ar;
% Normalize direction vector
drdt = sqrt( dxdt.^2 + dydt.^2 );
dxdt = dxdt ./ drdt;
dydt = dydt ./ drdt;
% Offset text orthogonal to curve
xtt = xt - off * dydt;
ytt = yt + off * dxdt * ar;
% Transform back out of log-space
if ( xlog )
xt = exp( xt );
xtt = exp( xtt );
end
if ( ylog )
yt = exp( yt );
ytt = exp( ytt );
end
hold on
% Plot points and text
hl = plot( xt, yt, LineSpec );
ht = text( xtt, ytt, num2str( t' ), 'HorizontalAlignment', 'center', 'VerticalAlignment', 'middle' );
% Restore hold setting
if ( ~holdsetting )
hold off;
end