写了一个简单的,看看是否你要的效果:(重新修改了一下)
h = figure; axis([0 10 0 10]); hold on
button = 1; n = 0; xy_org = [];
while button == 1 % 输入数据点,以右键作结
[xi,yi,button] = ginput(1); plot(xi, yi, 'r+');
n = n + 1; xy_org(:,n) = [xi;yi];
end
pause;% 输入完毕后按任意键继续
button = 1; m = 0; s = zeros(1,2); pre_m = 0;
while button == 1 % 选择数据点,以右键作结
while button == 1 && m < 2
[xi,yi,button] = ginput(1);
[d, ind] = min(sum((repmat([xi;yi],1,n) - xy_org).^2)); % 求各数据点与当前鼠标点的最小距离
if d <= 0.1
if pre_m ~= 0 % 之前画过直线,要刷新一下屏幕
plot(xy_org(1,s(:)), xy_org(2,s(:)), 'wo');
plot(xy_org(1,s(:)), xy_org(2,s(:)), 'w');
plot(xy_org(1,s(:)), xy_org(2,s(:)), 'r+');
pre_m = 0;
end
plot(xy_org(1,ind), xy_org(2,ind), 'bo');
m = m + 1; s(m) = ind;
end
end
if m == 1
plot(xy_org(1,s(m)), xy_org(2,s(m)), 'wo');
plot(xy_org(1,s(m)), xy_org(2,s(m)), 'r+');
elseif m == 2
plot(xy_org(1,s(:)),xy_org(2,s(:)), 'g');
end
pre_m = m; m = 0;
end
hold off;
[ 本帖最后由 eight 于 2006-9-22 14:29 编辑 ] |