Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Attached Files Point_header.txt (0.321 KB) Point_implementation.txt (0.448 KB) T

ID: 3622161 • Letter: A

Question

Attached Files Point_header.txt (0.321 KB) Point_implementation.txt (0.448 KB) The text to create the header (Point.h) and implementation (Point.cpp) files for the class that represents a point in 2D space can be found in the attached documents. Using this class, build another class that represents a triangle in 2D space. Private attributes of the triangle should include the coordinates of the three vertices (each of which is a type Point), the length of the three sides, and the angles (in degrees) opposite each side.
The methods should include the following:
- functions to create default triangles (vertices at [0,0], [1,0] and [0,1]) and to allow initial coordinates of the vertices to be entered
- functions to change the vertices (mutator methods)
- functions to get the coordinates of the vertices (accessor methods)
- functions to get the lengths of the sides and the three angles (accessor methods)
- a function to get the area of the triangle (accessor method).
Remember that whenever the coordinates of a vertex are changed, the lengths of the sides and the values of the angles must be updated.
Point_header.txt is :.................................................................................................
class Point
{
// Declaration of attributes
private:
double xCoord, yCoord;
// Function prototypes
public:
double getX() const; // Returns x value
double getY() const; // Returns y value
void setX(double newX);
void setY(double newY);
// Distance between two points
double Point::distance(const Point rhs) const;
};
and Point_implementation.txt is :....................................................................................
#include "Point.h"
#include
using namespace std;

double Point::getX() const
{
return xCoord;
}

double Point::getY() const
{
return yCoord;
}

void Point::setX(double newX)
{
xCoord = newX;
}

void Point::setY(double newY)
{
yCoord = newY;
}

double Point::distance(const Point rhs) const
{
double t1, t2, d;
t1 = rhs.xCoord - xCoord; // (x2 - x1)
t2 = rhs.yCoord - yCoord; // (y2 - y1)
d = sqrt(pow(t1,2) + pow(t2,2));
return d;
}

Explanation / Answer

I can help you with most of it. I typed some code, I have not compiled it so there may be some errors but this should give you an idea of how to do it. I did not add the get angle member because it will take a while to complete that and I do not have the time. You have to divide the triangle into two triangles with a line to form right angles, then use trig functions (i.e. sin=opp/hyp, cos=adj/hyp and tan=opp/adj) to find the angles. I used mainly data by passing values by reference. Also make sure you use cmath or math.h so that the pow function works for the distance formula. I also used your previous class methods to calculate the triangle formulas. Here is the code: #include class Triangle { private: Point p1, p2, p3; public: Triangle(); void ChangeVertex(int, int, int, int, int, int); void getCoordinates(int &,int &,int &,int &,int &,int &);//this will pass the values back through the parameters void getLegths(double &, double &, double &); void getAngles(double &, double &, double &); }; Triangle::Triangle() { p1.setX(0); p1.setY(0); p2.setX(0); p2.setY(1); p3.setX(1); p3.setY(0); }; void Triangle::ChangeVertex(int x1, int y1, int x2, int y2, int x3, int y3) { p1.setX(x1); p1.setY(y1); p2.setX(x2); p2.setY(y2); p3.setX(x3); p3.setY(y3); }; void Triangle::getCoordinates(int &v1x, int &v1y, int &v2x, int &v2y, int &v3x, int &v3y) { v1x = p1.getX(); V1y = p1.getY(); v2x = p2.getX(); V2y = p2.getY(); v3x = p3.getX(); V3y = p3.getY(); }; void Triangle::getLegths(double & L1, double & L2, double & L3) { L1 = pow(pow((p2.getX() - p1.getX()), 2) + pow((p2.getY() - p1.getY()), 2), 1/2); L2 = pow(pow((p3.getX() - p1.getX()), 2) + pow((p3.getY() - p1.getY()), 2), 1/2); L3 = pow(pow((p3.getX() - p2.getX()), 2) + pow((p3.getY() - p2.getY()), 2), 1/2); };