MATLAB QUESTION The area of a triangle is given by area squ
MATLAB QUESTION
The area of a triangle is given by: area = squareroot s times (s-a) times (s-b) times (s-c) where a, b and c are the lengths of the sides of the triangle, and s is equal to half the sum of lengths of the three sides of the triangle. Write a function compute Area that computes the area of the triangle given the coordinates of the three points that define the triangle. function a = compute Area(p1, p2, p3) Inputs: p_1 - coordinates of the first point as a vector with coordinates [x1 y1] p2 - coordinates of the second point as a vector with coordinates [x2 y2] p3 - coordinates of the second point as a vector with coordinates [x3 y3]Solution
Here\'s the code
#include <iostream>
#include <cmath>
#include <climits>
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
double computeArea(vector<int>, vector<int>, vector<int>);
double computeDistances(vector<int>, vector<int>);
int main(){
vector<int> p1,p2,p3;
int temp,temp2;
printf(\"Enter first point coordinates:\");
cin>>temp>>temp2;
p1.push_back(temp);
p1.push_back(temp2);
printf(\"Enter second point coordinates:\");
cin>>temp>>temp2;
p2.push_back(temp);
p2.push_back(temp2);
printf(\"Enter third point coordinates:\");
cin>>temp>>temp2;
p3.push_back(temp);
p3.push_back(temp2);
double res=computeArea(p1,p2,p3);
printf(\"The area of the triangle is %f\ \",res);
return 0;
}
double computeArea(vector<int> p1, vector<int> p2, vector<int> p3) {
double a = computeDistances(p1, p2);
double b = computeDistances(p2, p3);
double c = computeDistances(p3, p1);
double s = (a+b+c)/2;
double area = sqrt(s*(s-a)*(s-b)*(s-c));
return area;
}
double computeDistances(vector<int> x, vector<int> y) {
return sqrt(pow(y[1]-x[1],2)+pow(y[0]-x[0],2));
}
Here\'s the sample output
lokesh1729@lokesh1729-Latitude-E5570:/tmp$ ./computeDistance.out Enter first point coordinates:0
8
Enter second point coordinates:8
0
Enter third point coordinates:8
8
The area of the triangle is 32.000000
Also, here\'s the ideone of it: http://ideone.com/iieD5R

