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.

106 ビュー (過去 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
Venkat Siddarth Reddy 2024 年 4 月 18 日
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 Center および File ExchangeMATLAB についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by