The 3 vertices of the triangle are to be entered as x and y coordinates which sh
ID: 3655622 • Letter: T
Question
The 3 vertices of the triangle are to be entered as x and y coordinates which should be of type double. It would be convenient to store all 6 vertices in separate variables.
After reading the vertices your program should print the distances from the first point to the second, from the second to the third and from the third to the first. It would probably be convenient to store these distances in 3 double variables. These distances are the lengths of the sides of the triangle. The formula for distance is:
distance = sqrt ( (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2) )
Assuming the 3 distances are a, b and c, you can compute the area of the triangle using this formula:
area = 0.25 * sqrt ( (a+b+c) * (-a+b+c) * (a-b+c) * (a+b-c) )
Your program needs to implement 2 user-defined functions. One function needs to compute the distance between 2 vertices. The second function needs to compute the area of a triangle.
Please do not copy&paste from forums, I could have done that myself too, you know!
Thanks.
Explanation / Answer
#include#includeusing namespace std; //function to calculate distance double distance(double x1,double x2,double y1,double y2) { return sqrt((x1-x2)*(x1-x2) +(y1-y2)*(y1-y2)); } //function to calculate area double area( double a, double b, double c){ return ((double)1/4)*sqrt((a+b+c)*(a-b+c)*(a+b-c)); } int main(){ double distance(double x1,double x2,double y1,double y2); double area( double a, double b, double c); double x1,x2,x3,y1,y2,y3,a,b,c,areaOfTriangle; cout x1 >> y1 >> x2>> y2>>x3 >>y3 ; a= distance(x1,x2,y1,y2); coutRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.