Write a program that will represent an axis-aligned right triangle in the x-y pl
ID: 3532993 • Letter: W
Question
Write a program that will represent an axis-aligned right triangle in the x-y plane as a Class. A right triangle has a right angle (90-degree angle) and two sides adjacent to the right angle, called legs. See http://en.wikipedia.org/wiki/Right_triangle for a complete definition. In addition, an axis-aligned right triangle has one leg parallel with the x-axis and the other leg parallel with the y-axis. The location of the triangle is the vertex that connects the two legs, called the bottom left vertex. This vertex is located at the right angle and represented with the Class Point. The length of the triangle is the length of the leg parallel with the x-axis. The height of the triangle is the length of the leg parallel with the y-axis. The procedure main() (see the code template triangles_template.cpp) has been written for you as well as the member attributes of the Class Triangle. You will write code for the member functions of the Class Triangle as indicated by the comments /* INSERT CODE HERE */.
1. The function prototypes are provided for you and must NOT be changed in any way.
2. The get and set member functions of the class Point are already written for you.
3. The procedure main() is provided for you and must NOT be changed in any way.
4. Understand the class definitions for Point and Triangle.
5. Understand the algorithm in the procedure main().
6. Do NOT add more functions to the solution.
7. Each function should have a comment explaining what it does.
8. Each function parameter should have a comment explaining the parameter.
9. Write code by replacing every place /* INSERT CODE HERE */ appears with your solution.
10. Implement the get and set member functions for the class Triangle. Use the appropriate class attributes of the class Triangle.
a. The location of the bottom left vertex is stored in the member attribute blPoint.
b. The top left vertex can be computed from blPoint and the height.
c. The bottom right vertex can be computed from blPoint and the length.
11. You may assume that the user will enter positive values for the length and height.
12. Implement the member function hypotenuse() of the class Triangle that computes the hypotenuse of a triangle object. Use the Pythagorean Theorem to compute the length of the side of the triangle opposite to the right angle. Use the appropriate class attribute(s) of the class Triangle.
13. Implement the member function perimeter() of the class Triangle that computes the perimeter of the triangle object. Sum all three sides of the triangle. Use the appropriate class attribute(s) of the class Triangle and the member function hypotenuse().
14. Implement the member function scaleLength() of the class Triangle that computes the new value of the length as the current length weighted by the scale factor in the x direction. For example, if the current length is 2 and the scale factor is 3, the new length is 6. If the current length is 2 and the scale factor is 0.5, the new length is 1. You may assume that the scale factor will be positive. Update the length class attribute of the class Triangle.
15. Implement the member function scaleHeight() of the class Triangle that computes the new value of the height as the current height weighted by the scale factor in the y direction. You may assume that the scale factor will be positive. Update the height class attribute of the class Triangle.
16. Implement the member function display() of the class Triangle that displays all information about the triangle. Use the appropriate class attributes and member functions of the class Triangle. Important note, do not write redundant code.
17. Implement the function read_triangle() that prompts and reads the location of the bottom left vertex, length and height. These values are assigned into the class Triangle object passed into the function using set member functions.
18. TEST YOUR CODE THOROUGHLY. For example, pay close attention to the format of the output including empty lines. Your program must not have a run-time error.
19. Be sure to add the header comments
Explanation / Answer
#include <iostream>
#include <cmath>
using namespace std;
class Point
{
private:
double px;
double py;
public:
void setX(const double x);
void setY(const double y);
double getX() const;
double getY() const;
};
class Triangle
{
private:
Point blPoint;
double length, height;
public:
// member functions
void setBottomLeftX(const double x);
void setBottomLeftY(const double y);
void setLength(const double inLength);
void setHeight(const double inHeight);
Point getBottomLeft() const;
Point getBottomRight() const;
Point getTopLeft() const;
double getLength() const;
double getHeight() const;
double perimeter() const;
double hypotenuse() const;
void scaleLength(const double sx);
void scaleHeight(const double sy);
void display() const;
};
// FUNCTION PROTOTYPES GO HERE:
double read_triangle(Triangle & tri);
int main()
{
// Define local variables
Triangle tri;
double sx, sy;
//Prompt the user for triangle information and fill Class Triangle object, tri,
//with this information
read_triangle(tri);
// Display triangle information
tri.display();
// Prompt and read scale factors to change length and height
cout << "Enter scale factor in x direction: ";
cin >> sx;
cout << "Enter scale factor in y direction: ";
cin >> sy;
// Apply scale factors
tri.scaleLength(sx);
tri.scaleHeight(sy);
// Display triangle information
tri.display();
return 0;
}
// FUNCTION DEFINITIONS GO HERE:
// CLASS MEMBER FUNCTION DEFINITINOS GO HERE:
void Point::setX(const double x)
{
px = x;
}
void Point::setY(const double y)
{
py = y;
}
double Point::getX() const
{
return (px);
}
double Point::getY() const
{
return (py);
}
void Triangle::setBottomLeftX(const double x)
{
/* INSERT YOUR CODE */
blPoint.setX(x);
}
void Triangle::setBottomLeftY(const double y)
{
/* INSERT YOUR CODE */
blPoint.setY(y);
}
void Triangle::setLength(const double inLength)
{
/* INSERT YOUR CODE */
length=inLength;
}
void Triangle::setHeight(const double inHeight)
{
/* INSERT YOUR CODE */
height=inHeight;
}
Point Triangle::getBottomLeft() const
{
/* INSERT YOUR CODE */
return (blPoint);
}
Point Triangle::getBottomRight() const
{
/* INSERT YOUR CODE */
Point getBottomRight;
double mx = (blPoint.getX()+ length);
getBottomRight.setX(mx);
return(getBottomRight);
}
Point Triangle::getTopLeft() const
{
/* INSERT YOUR CODE */
Point getTopLeft;
double my = (blPoint.getY()+ height);
getTopLeft.setY(my);
return (getTopLeft);
}
double Triangle::getLength() const
{
/* INSERT YOUR CODE */
return (length);
}
double Triangle::getHeight() const
{
/* INSERT YOUR CODE */
return (height);
}
double Triangle::hypotenuse() const
{
/* INSERT YOUR CODE */
//hypotenuse = (sqrt((height * height)+(length * length)));
return (sqrt((height * height)+(length * length)));
}
double Triangle::perimeter() const
{
/* INSERT YOUR CODE */
//perimeter = ((sqrt((height * height)+(length * length)))+ height + length);
return ((sqrt((height * height)+(length * length)))+ height + length);
}
void Triangle::scaleLength(const double scalefact)
{
/* INSERT YOUR CODE */
double newblpx
length = scalefact * length;
}
void Triangle::scaleHeight(const double scalefact)
{
/* INSERT YOUR CODE */
height = scalefact * height;
}
void Triangle::display() const
{
/* INSERT YOUR CODE */
cout <<"---------------------------------------" << endl;
cout << "Lower Left Vertex (" << blPoint.getX() << ", " << blPoint.getY() << ')' <<endl;
cout << "Top Left Vertex (" << blPoint.getX() << ", " << getTopLeft.getY() << ')' << endl;
cout << "Bottom Right Vertex (" << getBottomRight().getX() << ", " << blPoint.getY() << ')' << endl;
cout << "Dimensions (" << getBottomRight().getX()- blPoint.getX() << ", " << getTopLeft().getY() - blPoint.getY() << ')' << endl;
cout << "Hypotenuse = " << hypotenuse() << endl;
cout << "Perimeter = " << perimeter() << endl;
cout <<"---------------------------------------" << endl;
}
double read_triangle(Triangle & tri)
{
/* INSERT YOUR CODE */
double x, y, inLength, inHeight;
cout << "Enter bottom left x coordinate: ";
cin >> x;
tri.setBottomLeftX(x);
cout << "Enter bottom left y coordinate: ";
cin >> y ;
tri.setBottomLeftY(y);
cout << "Enter length: ";
cin >> inLength;
tri.setLength(inLength);
cout << "Enter Height: ";
cin >> inHeight;
tri.setHeight(inHeight);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.