In: Computer Science
Using MATLAB or Octave, Write a script that prompts the user for the coordinates of three points A, B, and C, namely (xA, yA), (xB, yB), (xC, yC), forming a triangle, storing each in a variable (for a total of 6 variables). The script should then calculate the centroid G = (xG, yG) using xG = xA+xB+xC 3 and yG = yA+yB+yC 3 , storing each in a variable. Finally, the script should print all four points. Sample output: Enter x value for triangle point A: 1 Enter y value for triangle point A: 2 Enter x value for triangle point B: 10 Enter y value for triangle point B: 1 Enter x value for triangle point C: 5 Enter y value for triangle point C: 8 Point A of the triangle is ( 1.00, 2.00 ) Point B of the triangle is ( 10.00, 1.00 ) Point C of the triangle is ( 5.00, 8.00 ) The centroid of the triangle is ( 5.33, 3.67 ) Enter x value for triangle point A: -5.5 Enter y value for triangle point A: -6.75 Enter x value for triangle point B: 11.1 Enter y value for triangle point B: -3.4 Enter x value for triangle point C: 1.275 Enter y value for triangle point C: 4.567 Point A of the triangle is ( -5.50, -6.75 ) Point B of the triangle is ( 11.10, -3.40 ) Point C of the triangle is ( 1.27, 4.57 ) The centroid of the triangle is ( 2.29, -1.86 )
MATLAB:
clc;close all;clear all;
xA=input('Enter x value for triangle point A:');
yA=input('Enter y value for triangle point A:');
xB=input('Enter x value for triangle point B:');
yB=input('Enter y value for triangle point B:');
xC=input('Enter x value for triangle point C:');
yC=input('Enter y value for triangle point C:');
fprintf('Point A of the triangle is (%4.2f ,%4.2f)\n',xA,yA)
fprintf('Point B of the triangle is (%4.2f ,%4.2f)\n',xB,yB)
fprintf('Point C of the triangle is (%4.2f ,%4.2f)\n',xC,yC)
xG=(xA+xB+xC)/3;
yG=(yA+yB+yC)/3;
fprintf('The centroid of the triangle is (%4.2f
,%4.2f)\n',xG,yG)
Command window:ouptut
Enter x value for triangle point A:1
Enter y value for triangle point A:2
Enter x value for triangle point B:10
Enter y value for triangle point B:1
Enter x value for triangle point C:5
Enter y value for triangle point C:8
Point A of the triangle is (1.00 ,2.00)
Point B of the triangle is (10.00 ,1.00)
Point C of the triangle is (5.00 ,8.00)
The centroid of the triangle is (5.33 ,3.67)
>>
Enter x value for triangle point A:-5.5
Enter y value for triangle point A:-6.75
Enter x value for triangle point B:11.1
Enter y value for triangle point B:-3.4
Enter x value for triangle point C:1.27
Enter y value for triangle point C:4.57
Point A of the triangle is (-5.50 ,-6.75)
Point B of the triangle is (11.10 ,-3.40)
Point C of the triangle is (1.27 ,4.57)
The centroid of the triangle is (2.29 ,-1.86)
>>