Reputation: 97
In Matlab, if I would like to draw a line manually on the image with two points unfixed, I could use getline() function, and this function returns (x1,y1) (x2,y2) which are the end points' coordinates.
However, now I have a known fixed point, say (x0,y0). I want the users to draw the line passing this point. Is there any ways?
I know I could let users to just determine the other point, thus two points form a line. However, getline() function have the dotted line animation which could let users clearly see the whole line during the choosing process, but getpts() do not. Thus users may not draw the line precisely.
So is there any ways that could let users to draw a line with one fixed point.
Thank you!
Upvotes: 2
Views: 424
Reputation: 5190
If you want to keep the "animation" effect of getline
while drawin the line, you can use the imline
function.
It allows you the draw the line and adjust both the starting and ending point; once the line is OK, you just have to double-click
on it.
You can also add a flag to control if the line shall start from a pre-defined point (as in you r question) or not.
In the following code, imline
is used to add a line on a chart.
% Initial point coords
x0=3
y0=3
% Define the flag to control the starting point of the line
% plot_form_fixed_point=1 ==> the line will start from the pre-defined
% starting point, regardless, the adjustment
% of the initial point
% plot_form_fixed_point=0 ==> the pre-defined initial point is not
% considered
plot_form_fixed_point=1
% Plot some data (not strictly needed, just to populate the axes)
plot(randi([-3 10],5,1))
hold on
% Plot the first point / marker
plot(x0,y0,'o','markerfacecolor','r','markeredgecolor','r')
grid on
drawnow
% Draw and adjust the line
h = imline;
pos = wait(h)
% Check if the line has to start from the pre-defined starting point
if(plot_form_fixed_point)
plot([x0 pos(2,1)],[y0 pos(2,2)],'o-r','markerfacecolor','g','markeredgecolor','g')
else
plot(pos(:,1),pos(:,2),'o-r','markerfacecolor','g','markeredgecolor','g')
end
delete(h)
If you do not have the Image Processing toolbox
you can use getline
this way:
plot
instruction, the pre-defined point in place of the first point returned by getline
.If needed, using getline
you can plot a line made of more than one segment, by just left-click
(double-click
to end the process).
Also in this case, you can add the to control if the line shall start from a pre-defined point (as in you r question) or not.
% Initial point coords
x0=1
y0=2
% Plot the first point / marker
plot(x0,y0,'o','markerfacecolor','r','markeredgecolor','r')
grid on
drawnow
% Use "getline to draw the line, the user should use the previously plotted
% marker as a refrence stating point
[x1,y1]=getline()
hold on
% Define the flag to control the starting point of the line
% plot_form_fixed_point=1 ==> the line will start from the pre-defined
% starting point, regardless, the adjustment
% of the initial point
% plot_form_fixed_point=0 ==> the pre-defined initial point is not
% considered
plot_form_fixed_point=0
% Plot the line from the first point st by the user discrding the first
% point identified by usign "getline"
if(~plot_form_fixed_point)
plot(x1,y1,'o-r','markerfacecolor','g','markeredgecolor','g')
else
plot([x0 x1(2:end)'],[y0 y1(2:end)'],'o-r','markerfacecolor','g','markeredgecolor','g')
end
Hope this helps.
Upvotes: 1
Reputation: 10792
You can use ginput.
x1 = 2.5; %fixed x
y1 = 2.5; %fixed y
plot(x1,y1,'o');
ylim([0 5])
xlim([0 5])
hold on
[x2,y2] = ginput(1); %ask for the second point
plot([x1 x2],[y1 y2],'o',[x1 x2],[y1 y2]) %plot the result
EDIT
For your second question you can use impoint
if you have the image processing toolbox.
x1 = 2.5;
y1 = 2.5;
ax0 = plot(x1,y1,'o');
ylim([0 5])
xlim([0 5])
hold on
mypoint = impoint;
w = waitforbuttonpress;
while w <= 0
w = waitforbuttonpress;
end
mypoint = getPosition(mypoint);
x2 = mypoint(1);
y2 = mypoint(2);
x = [x1 x2];
y = [y1 y2];
ax1 = plot(x,y,'o');
ax2 = plot(x,y);
axis('equal');
delete(ax0);
Once you have dragged your point, just press anywhere on your keyboard for point validation.
Upvotes: 1