-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcurvytextgenerator.m
50 lines (36 loc) · 1.39 KB
/
curvytextgenerator.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
function curvytextgenerator(xy, str, varargin)
if size(xy,1) > 2
xy = xy';
end
n = size(xy,2);
m = length(str);
% Calculate the spline
XY = spline(1:n, xy, linspace(1,n,m+1));
% Calculate the distances between adjacent points on the curve
dXY = sqrt(sum((XY(:,2:end) - XY(:,1:end-1)).^2));
% Compute the cumulative sum of distances
cumulativeDist = [0 cumsum(dXY)];
% Calculate the spacing for each letter
letterSpacing = cumulativeDist(end) / m;
letterIndex = 1;
for i = 1:m
letterIndex = min(letterIndex, m - 1);
while cumulativeDist(letterIndex) < (i - 1) * letterSpacing && letterIndex < m
letterIndex = letterIndex + 1;
end
letterPos = interp1(cumulativeDist, XY', (i - 0.5) * letterSpacing);
letterPos = letterPos';
dXY_temp = XY(:, letterIndex + 1) - XY(:, letterIndex);
theta = atan2(dXY_temp(2), dXY_temp(1)) / (2 * pi) * 360;
text(letterPos(1), letterPos(2), str(i), 'rotation', theta, ...
'horizontalalignment', 'center', 'verticalalignment', 'middle', varargin{:});
end
minX = min(xy(1,:));
minY = min(xy(2,:));
maxX = max(xy(1,:));
maxY = max(xy(2,:));
xlim([minX, maxX]);
ylim([minY, maxY]);
axis equal;
axis off;
end