Write a MATLAB program that asks the user to input the coordinates of the vertic
ID: 3817530 • Letter: W
Question
Write a MATLAB program that asks the user to input the coordinates of the vertices of atriangle as an array [x1 y1; x2 y2; x3 y3]. In addition, ask the user to input the coordinates ofanother point [x0 y0]. Then decide and output whether the given point [x0 y0] lies (1)completely inside the triangle, or (2) completely outside the triangle (3) or exactly on one ofthe edges.
Hint: Recall that the equation of a line through 2 points (x1,y1) and (x2, y2) is:
l1(x,y) = (y – y1) * (x2 – x1) – ( y2 – y1) * (x – x1)
(1) If (x0,y0) is on the line then l1(x0,y0) = 0.(2) If (x0,y0) and (x3,y3) are on the same side of the line then
sign (l1(x0,y0)) = sign(l1(x3,y3)).
(3) If (x0,y0) and (x3,y3) are on the opposite side of the line then
sign (l1(x0,y0)) = – 1 * sign(l1(x3,y3)).
Similarly you can decide about the position of (x0,y0) with respect to line (x2,y2) (x3,y3)and line (x3,y3) (x1,y1).
Explanation / Answer
please refer below code
1) create file point_triangle.m (or any name) and paste below code
close all
clc
clear all
prompt = 'Enter co-ordinates of vertices in array form[x1 y1;x2 y2;x3 y3] ';
Triangle = input(prompt);
prompt = ' Please enter point [x0 y0] ';
point = input(prompt);
x0 = point(1,1);
y0 = point(1,2);
x1 = Triangle(1,1);
y1 = Triangle(1,2);
x2 = Triangle(2,1);
y2 = Triangle(2,2);
x3 = Triangle(3,1);
y3 = Triangle(3,2);
%equation of line theough 2 points (x1,y1) and (x2,y2)
l1_x0_y0 = (y0 - y1) * (x2 - x1) - ( y2 - y1) * (x0 - x1);
l1_x3_y3 = (y3 - y1) * (x2 - x1) - ( y2 - y1) * (x3 - x1);
if l1_x0_y0==0
fprintf('Point lies on the edge (x1,y1)---(x2,y2) ');
elseif l1_x0_y0 < 0 && l1_x3_y3 > 0 || l1_x0_y0 > 0 && l1_x3_y3 < 0
fprintf('Point lies outside of triange');
else
end
%equation of line theough 2 points (x1,y1) and (x3,y3)
l2_x0_y0 = (y0 - y1) * (x3 - x1) - ( y3 - y1) * (x0 - x1);
l2_x2_y2 = (y2 - y1) * (x3 - x1) - ( y3 - y1) * (x2 - x1);
if l2_x0_y0==0
fprintf('Point lies on the edge (x1,y1)---(x3,y3) ');
elseif l2_x0_y0 < 0 && l2_x2_y2 > 0 || l2_x0_y0 > 0 && l2_x2_y2 < 0
fprintf('Point lies outside of triange');
else
end
%equation of line theough 2 points (x2,y2) and (x3,y3)
l3_x0_y0 = (y0 - y2) * (x3 - x2) - ( y3 - y2) * (x0 - x2);
l3_x1_y1 = (y1 - y2) * (x3 - x2) - ( y3 - y2) * (x1 - x2);
if l3_x0_y0==0
fprintf('Point lies on the edge (x1,y1)---(x3,y3) ');
elseif l3_x0_y0 < 0 && l3_x1_y1 > 0 || l3_x0_y0 > 0 && l3_x1_y1 < 0
fprintf('Point lies outside of triange');
else
end
% If point lies on same side of any vertex then there is two conditions
% 1) It lies inside triange
%2) It lies outside of triangle
% If point P lies inside of triange ABC then
% Area of ABC = Area of PBC + Area of PAC + Area of PAB
A = area_of_triangle(x1,y1,x2,y2,x3,y3);
A1 = area_of_triangle(x0,y0,x2,y2,x3,y3);
A2 = area_of_triangle(x1,y1,x0,y0,x3,y3);
A3 = area_of_triangle(x1,y1,x2,y2,x0,y0);
if A == A1 + A2 + A3
fprintf('Point lies inside triangle ');
else
fprintf('Point lies outside triangle ');
end
2) create file area_of_triangle.m(don't change name here) and paste below code
function A = area_of_triangle(x1,y1,x2,y2,x3,y3)
A = abs((x1*(y2-y3) + x2*(y3-y1)+ x3*(y1-y2))/2.0);
Let us check whether the point P(10, 15) lies inside the triangle formed by A(0, 0), B(20, 0) and C(10, 30)
Please refer below output for reference
Enter co-ordinates of vertices in array form[x1 y1;x2 y2;x3 y3]
[0 0; 20 0; 10 30]
Please enter point [x0 y0]
[10 15]
Point lies inside triangle
>>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.