Code for Given three points (x1, y1), (x2, y2) and (x3, y3), write a program to check if all the three points fall on one straight line.

88 次查看(过去 30 天)
Given three points (x1, y1), (x2, y2) and (x3, y3), write a program to check if all the three points fall on one straight line.

采纳的回答

Venkat Siddarth Reddy
Hi Ravi,
To achieve this you can use the concept of slopes i.e. if the slope between (x1, y1) and (x2,y2), and between (x2,y2) and (x3,y3) are equal then points lie on a straight line.
Following is the implementation of above logic in MATLAB
function isCollinear = checkCollinearity(x1, y1, x2, y2, x3, y3)
% Calculate the "slope" conditions using cross multiplication in case
% to avoid division.
% (y2 - y1) * (x3 - x2) == (y3 - y2) * (x2 - x1)
% If the above condition is true, then the points are collinear.
isCollinear = (y2 - y1) * (x3 - x2) == (y3 - y2) * (x2 - x1);
end
Call the above functions with the co-ordinates as arguments
% Define the points
x1 = 1; y1 = 1;
x2 = 2; y2 = 2;
x3 = 3; y3 = 3;
% Check if the points are collinear
if checkCollinearity(x1, y1, x2, y2, x3, y3)
disp('The points fall on one straight line.');
else
disp('The points do not fall on one straight line.');
end
The points fall on one straight line.
Hope it helps!

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by