答案 0 :(得分:2)
这个答案撕掉了Rody Oldenhuis的优秀代码(你链接到的代码)。它仅修改绘图程序,以保持选定的点和切线连接到圆。正如评论家指出的那样,其余部分只是几何:
function test
hh = figure(1); clf, hold on
grid on
x = [0:0.01:2*pi]';
f = @(x) sin(x);
g = @(x) cos(x);
fprime = @(x,y) -x./y;
circle = [f(x) g(x)];
plot(g(x), f(x), 'r')
axis tight
D = [];
L = [];
set(hh, ...
'WindowButtonMotionFcn', @mouseMove,...
'WindowButtonDownFcn', @mouseClick);
function mouseMove(varargin)
coords = get(gca, 'currentpoint');
xC = coords(1,1);
yC = coords(1,2);
% find nearest point on the circle
[minr2 imin]=min(sum((circle - ones(size(circle,1),1)*coords(1,1:2)).^2,2));
if ishandle(D)
delete(D);
end
D = plot(circle(imin,1), circle(imin,2), 'ko');
end
function mouseClick(obj, varargin)
switch get(obj, 'selectiontype')
% actions for left mouse button
case 'normal'
coords = get(gca, 'currentpoint');
xC = coords(1,1);
yC = coords(1,2);
% find nearest point on the circle
[minr2 imin]=min(sum((circle - ones(size(circle,1),1)*coords(1,1:2)).^2,2));
xC=circle(imin,1);
yC=circle(imin,2);
m = fprime(xC,yC);
b = yC-m*xC;
if ishandle(L)
delete(L);
end
L = line(xC+[-pi/2;pi/2], m*(xC+[-pi/2;pi/2])+b);
case 'alt'
% actions for right mouse button
case 'extend'
% actions for middle mouse button
case 'open'
% actions for double click
otherwise
% actions for some other X-mouse-whatever button
end
end
end