1. The Euclidean distance between any two points (x1, y1) and (x2, y2) is given
ID: 3759712 • Letter: 1
Question
1. The Euclidean distance between any two points (x1, y1) and (x2, y2) is given by:
d = ((x1 x2) ^2 + (y1 y2)^ 2)
The area of a triangle can be calculated based on the lengths of its sides as follows:
(s × (s a) × (s b) × (s c))
where a, b, and c are the lengths of the sides and s is equal to half the sum of the lengths of the three sides of the triangle, i.e. s = (a + b + c)/2.
(a) Write a MATLAB function side_length that calculates and returns the length of the side formed by any two points x1, y1 and x2, y2 (i.e. the distance between them).
(b) Write a MATLAB function triangle_area that takes three coordinates of three points x1, y1, x2, y2, x3, y3 that determine a triangle (i.e. the x and y components of each of the three points in order) and returns the area of a triangle and is calculated by calling your side_length function from part (a).
(c) Write a MATLAB script that prompts a user to enter the coordinates of three points that determine a triangle (i.e. the x and y components of each point entered as [x1, y1, x2, y2, x3, y3]), calculates the area of the triangle and prints it to the screen. You may use your triangle_area function from part (b). For example, running your script and providing it with some example input should yield: Enter triangle coordinates [x1, y1, x2, y2, x3, y3] : [1, 2, 1, 6, 4, 2] The area of the triangle is : 6 Hint : The MATLAB function to prompt for user input is input
Explanation / Answer
function len = side_length(x1,y1,x2,y2)
len = sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2))
end
function area = triangle_area(x1,y1,x2,y2,x3,y3)
a = side_length(x1,y1,x2,y2)
b = side_length(x2,y2,x3,y3)
c = side_length(x1,y1,x3,y3)
s = (a+b+c)/2
area = sqrt(s*(s-a)+s*(s-b)+s*(s-c))
end
x1 = input('Enter x1');
y1 = input('Enter y1');
x2 = input('Enter x2');
y2 = input('Enter y2');
x3 = input('Enter x3');
y3 = input('Enter y3');
triangle_area(x1,y1,x2,y2,x3,y3);
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.