Write a program that will represent an axis-aligned right triangle in the x-y pl
ID: 3539443 • 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.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
#include <iostream>
#include <cmath>
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:
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)
{
blPoint.setX(x);
}
void Triangle::setBottomLeftY(const double y)
{
blPoint.setY(y);
}
void Triangle::setLength(const double inLength)
{
length = inLength;
}
void Triangle::setHeight(const double inHeight)
{
height = inHeight;
}
Point Triangle::getBottomLeft() const
{
return blPoint;
}
Point Triangle::getBottomRight() const
{
Point brPoint;
brPoint.setX( blPoint.getX() + length);
brPoint.setY( blPoint.getY());
return brPoint;
}
Point Triangle::getTopLeft() const
{
Point tlPoint;
tlPoint.setX( blPoint.getX());
tlPoint.setY( blPoint.getY() + height);
return tlPoint;
}
double Triangle::getLength() const
{
return length;
}
double Triangle::getHeight() const
{
return height;
}
double Triangle::hypotenuse() const
{
return sqrt(length*length + height*height);
}
double Triangle::perimeter() const
{
return (length + height + hypotenuse() );
}
void Triangle::scaleLength(const double scalefact)
{
length = length*scalefact;
}
void Triangle::scaleHeight(const double scalefact)
{
height = height*scalefact;
}
void Triangle::display() const
{
Point brPoint = getBottomRight();
Point tlPoint = getTopLeft();
cout<<"Bottom Left Point : (" << blPoint.getX()<<" , "<<blPoint.getY()<<")"<<endl;
cout<<"Bottom Right Point : (" << brPoint.getX()<<" , "<<brPoint.getY()<<")"<<endl;
cout<<"Top Left Point : (" << tlPoint.getX()<<" , "<<tlPoint.getY()<<")"<<endl;
cout<<"Length = "<<getLength()<<endl;
cout<<"Height = "<<getHeight()<<endl;
cout<<"Hypotenuse = "<<hypotenuse()<<endl;
cout<<"Perimeter = "<<perimeter()<<endl;
}
void read_triangle(Triangle & tri)
{
double x,y,l,h;
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>>l;
tri.setLength(l);
cout<<"Enter Height : ";
cin>>h;
tri.setHeight(h);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.