这个问题与这个问题非常相似,但在这里我有参数方程,例如:
x = sin(t)
y = cos(t)
如何在特定点(该点由用户选择)的参数曲线上绘制切线?
任何想法,将不胜感激。
这个答案从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