3 Question 3 . For two points u and v, the distance d between them is given as f
ID: 3918252 • Letter: 3
Question
3 Question 3 . For two points u and v, the distance d between them is given as follows. Write a function distance to calculate the distance between two PointXY objects. double distance(const PointXY u, conat PointXY kv): . Write a function "area.perimeter" to calculate the area and perimeter given an input vector of three points. The three points form a triangle. . Your function should enable the program below to compile and run correctly // #include relevant headers using namespace std; // class declaration and functions int main) int n 3; vector v (n); // set x,y coordinates double area, perineter: area_periseter (v, area, perineter); area periseter(v, area, perineter); // function call // call twice why not coutExplanation / Answer
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
class PointXY // class with public x and y coordinates of a Point
{
public:
int x,y;
};
double distance(const PointXY &u,const PointXY &v) // reference arguments
{
return sqrt((u.x - v.x)* (u.x - v.x)+ (u.y - v.y)*(u.y - v.y));
}
void area_perimeter(vector<PointXY> &v,double &area,double &perimeter)
{
double a = distance(v[0],v[1]);
double b = distance(v[1],v[2]);
double c = distance(v[2],v[0]);
perimeter = a+b+c;
double s = perimeter/2.0;
area = sqrt(s*(s-a)*(s-b)*(s-c));
}
int main() {
int n = 3;
vector<PointXY> v(n);
// set x,y coordinates;
v[0].x = 3.5;
v[0].y = 0.4;
v[1].x = 7.2;
v[1].y = 0.6;
v[2].x = 4.1;
v[2].y = 5.6;
double area,perimeter;
area_perimeter(v,area,perimeter);
cout<<"area "<<area<<endl;
cout<<"perimeter "<<perimeter<<endl;
return 0;
}
Output:
area 10
perimeter 14.93
Do ask if any doubt. Please upvote.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.