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

Write a program that will represent an axis-aligned right triangle in the x-y pl

ID: 3767292 • 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 */. Run triangles_solution.exe to see an example of the program.

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

template:

#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:
void 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 */
}

void Triangle::setBottomLeftY(const double y)
{
   /* INSERT YOUR CODE */
}

void Triangle::setLength(const double inLength)
{
   /* INSERT YOUR CODE */
}

void Triangle::setHeight(const double inHeight)
{
   /* INSERT YOUR CODE */
}

Point Triangle::getBottomLeft() const
{
   /* INSERT YOUR CODE */
}

Point Triangle::getBottomRight() const
{
   /* INSERT YOUR CODE */
}

Point Triangle::getTopLeft() const
{
   /* INSERT YOUR CODE */
}

double Triangle::getLength() const
{
   /* INSERT YOUR CODE */
}

double Triangle::getHeight() const
{
   /* INSERT YOUR CODE */
}

double Triangle::hypotenuse() const
{
   /* INSERT YOUR CODE */
}
  
double Triangle::perimeter() const
{
   /* INSERT YOUR CODE */
}
  
void Triangle::scaleLength(const double scalefact)
{
   /* INSERT YOUR CODE */
}

void Triangle::scaleHeight(const double scalefact)
{
   /* INSERT YOUR CODE */
}

void Triangle::display() const
{
   /* INSERT YOUR CODE */
}

void read_triangle(Triangle & tri)
{
   /* INSERT YOUR CODE */
}

Explanation / Answer

Here is the code for you. If you need any further refinements, just get back to me.

// Here is the template//
/*
File: triangles.cpp
Created by: ??
Creation Date: ??
Synopsis: ??
*/
#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:
void 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 p;
p.setX(blPoint.getX() + length);
p.setY(blPoint.getY());
return p;
}
Point Triangle::getTopLeft() const
{
/* INSERT YOUR CODE */
Point p;
p.setX(blPoint.getX());
p.setY(blPoint.getY()+height);
return p;
}
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 */
return sqrt(length*length + height*height);
}
  
double Triangle::perimeter() const
{
/* INSERT YOUR CODE */
return length + height + this->hypotenuse();
}
  
void Triangle::scaleLength(const double scalefact)
{
/* INSERT YOUR CODE */
length = length * scalefact;
}
void Triangle::scaleHeight(const double scalefact)
{
/* INSERT YOUR CODE */
height = height * scalefact;
}
void Triangle::display() const
{
/* INSERT YOUR CODE */
Point p = this->getBottomLeft();
cout<<"Lower Left Vertex ("<<p.getX()<<", "<<p.getY()<<")"<<endl;
p = this->getTopLeft();
cout<<"Top Left Vertex ("<<p.getX()<<", "<<p.getY()<<")"<<endl;
p = this->getBottomRight();
cout<<"Top Left Vertex ("<<p.getX()<<", "<<p.getY()<<")"<<endl;
cout<<"Dimensions ("<<getLength()<<", "<<getHeight()<<")"<<endl;
cout<<"Hypotenuse = "<<hypotenuse()<<endl;
cout<<"Perimeter = "<<perimeter()<<endl;
}
void read_triangle(Triangle & tri)
{
/* INSERT YOUR CODE */
double x, y, length, height;
cout<<"Enter bottom left x coordinate: ";
cin>>x;
cout<<"Enter bottom left y coordinate: ";
cin>>y;
cout<<"Enter length: ";
cin>>length;
cout<<"Enter height: ";
cin>>height;
tri.setBottomLeftX(x);
tri.setBottomLeftY(y);
tri.setLength(length);
tri.setHeight(height);
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote