linsolve() in a while loop with ginput()
Show older comments
Hi,
I've just almost finished my homework. To summarize, we are to draw a line through two user-defined points via ginput(). The user then clicks another point and we are to show the point of projection onto a line and its reflected point across the line. The user can click as many points to be projected and reflected until he/she decides to terminate ginput() with the return key.
I decided to implement this with systems of equation then using the linsolve() function. It works perfectly in a single iteration. In a while-loop, the calculations and plots are still correct but upon hitting the return key, it will spit out an error 'Dimensions must agree' involving linsolve(). I want to be able to hit the return key and print out to the command line "Program Ended". This error prevents me from doing so. How can I avoid this?
Here is my while loop:
while 1
[p,q]=ginput(1);
plot(p,q,'bo');
originalPoint = [p,q]';
%Calculate Projection
A = [x(2)-x(1) y(2)-y(1)
y(1)-y(2) x(2)-x(1)];
B= -[-p*(x(2)-x(1))-q*(y(2)-y(1))
-y(1)*(x(2)-x(1))+(x(1)*(y(2)-y(1)))];
X=linsolve(A,B);
plot(X(1),X(2),'bp');
%Calculate Reflection Point
reflectPoint = [2*X(1),2*X(2)]'-[originalPoint(1),originalPoint(2)]';
plot(reflectPoint(1),reflectPoint(2),'bh');
key = get(gcf, 'CurrentKey');
if(key == 'return')
fprintf('Program Terminated\n');
break
end
end
1 Comment
KSSV
on 20 Feb 2017
When we execute the code it shows error, x, y are undefined.
Answers (0)
Categories
Find more on Data Exploration in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!