c++ programming For two points u and v, the distance d between them is given as
ID: 3918718 • Letter: C
Question
c++ programming
For two points u and v, the distance d between them is given as follows. d = q (ux ? vx) 2 + (uy ? vy) 2 . • Write a function distance to calculate the distance between two PointXY objects. double distance(const PointXY &u, const PointXY &v); • 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, perimeter; area_perimeter(v, area, perimeter); // function call area_perimeter(v, area, perimeter); // call twice why not cout << "area = " << area << endl; cout << "perimeter = " << perimeter << endl; return 0; } • Write an appropriate signature for the “area perimeter” function. 1. Declare variables a, b, c and compute the edge lengths of the triangle a = distance(v[0],v[1]), similarly b for (v[1],v[2]) and c for (v[2],v[0]). 2. The perimeter of the triangle is the sum of the edge lengths a + b + c. 3. Define the semi-perimeter as s = perimeter/2.0. 4. The area of the triangle is given by the Heron’s formula. area = p s(s ? a)(s ? b)(s ? c) .
Explanation / Answer
#include #include #include using namespace std; class PointXY { private: int x, y; public: int getX() const { return x; } int getY() const { return y; } void setX(int x) { this->x = x; } void setY(int y) { this->y = y; } }; double distance(const PointXY& u, const PointXY& v) { return sqrt((u.getX() - v.getX()) * (u.getX() - v.getX()) + (u.getY() - v.getY()) * (u.getY() - v.getY())); } void area_perimeter(vector& 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 v(n); v[0].setX(3.5); v[0].setY(0.4); v[1].setX(7.2); v[1].setY(0.6); v[2].setX(4.1); v[2].setY(5.6); double area, perimeter; area_perimeter(v, area, perimeter); coutRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.