How can we Plot a line passing through two points?
Afficher commentaires plus anciens
I am given two points (x1,y1) and (x2,y2). How can I plot a line that will pass through these two points and extend till the x and y axis?
Réponse acceptée
Plus de réponses (1)
Mahesh
le 29 Mai 2014
1 vote
A = (x1,y1); B = (x2,y2);
plot(A,B)
4 commentaires
Mahesh
le 29 Mai 2014
Example: Want to draw the line b/w the points (5,10) and (15,30).
A = (5,15); B = (10,30); plot(A,B)
Emmanuel
le 29 Mai 2014
No, this is incorrect. With the syntax plot(A,B), A contains x-values and B contains y-values. Instead, Mahesh defined A and B as (x,y) coordinates.
A simple test will show that this is incorrect,
A = [1,2];
B = [ -3,4];
plot(A,B)
hold on
plot(A(1),A(2), 'r*')
plot(B(1),B(2), 'k*')
legend('line','A','B','Location','BestOutside')
axis padded
To connect coordinates A-B you must use,
plot([A(1),B(1)], [A(2),B(2)])
Peter Hansen
le 17 Août 2021
First of all, you need to understand that mathlab is made for matrixes and arrays, to easy big data calculation.
So all data is ussaly needed in matrix or arrays.
Therfor when ploting points, you dont plot single points like A and B but and array of x-cordinates of A and B and a array of y-cordinates of A and B.
maybe this will claryfy it for you :)
figure(2); clf(2); hold on; axis([0 10 0 10]); axis padded
% If Point A is in x=1 and y=2
% If Point B is in x=-3 and y=4
% If Point C is in x=-6 and y=-4
x_array = [1 -3 -6];
y_array = [2 4 -4];
plot(x_array(1),y_array(1),'*') % A
plot(x_array(2),y_array(2),'*') % B
plot(x_array(3),y_array(3),'*') % C
% OR to plot all points at once uncoment below line insted, and remve ",'B','C'" from legend
% plot(x_array,y_array,'*') % A, B and C
line(x_array,y_array) % line
legend('A','B','C','line','Location','BestOutside') hold off
Catégories
En savoir plus sur Annotations dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

