I have a very weird question for my assignment and I was wondering how to figure it out exactly.
The question asks:
-
Create a base class Shape which will form the basis of your shapes. The Shape class will contain functions to calculate area and circumference of the shape, plus provide the coordinates (Points) of a rectangle that encloses the shape (a bounding box). These will be overloaded by the derived classes as necessary. Create a display() function that will display the name of the class plus all stored information about the class (including area, circumference, and bounding box).
-
Build the hierarchy by creating the Shape classes Circle, Square, and Triangle. For these derived classes, create default constructors and constructors whose arguments can initialize the shapes appropriately using the correct number of Point objects (i.e., Circle requires a Point center and a radius; Square requires four Point vertices, while Triangle requires three Point vertices).
-
In
main(), create one instance each of the following: a Circle with a radius of 23, a Square with sides 25, and a Triangle with sides 10, 20, 30. Define all of them so that the origin (0,0) is somewhere within each object. Display the information from each object.
So I need to figure out the points that will create a triangle with sides 10, 20, 30.
Input:
Triangle t(Point(0,0), Point(0,20), Point(0,30));
Here is my code for Triangle:
class Triangle : public Shape
{
Point s1, s2, s3;
public:
Triangle() {}
Triangle(const Point &p1, const Point &p2, const Point &p3) : s1(p1), s2(p2), s3(p3) {}
void bbox()
{
std::cout << "Triangle::bounding " << s1 << s2 << s3;
}
void circumference()
{
Point side1 = (s1 - s2);
Point side2 = (s2 - s3);
Point side3 = (s3 - s1);
std::cout << "Triangle::perimeter " << side1.dist() + side2.dist() + side3.dist();
}
void area()
{
Point side1 = (s1 - s2);
Point side2 = (s2 - s3);
Point side3 = (s3 - s1);
double half = (side1.dist() + side2.dist() + side3.dist())/2;
double answer = sqrt(half * (half - side1.dist()) * (half - side2.dist()) * (half - side3.dist()));
std::cout << "Triangle::area " << answer;
}
};
This is the output:
Triangle::bounding (0,0)(0,20)(0,30)
Triangle::perimeter 60
Triangle::area 0
What is the best method to create a bounding box around the Triangle with sides 10,20,30 or any triangle for that matter.
There is no triangle with sides 10,20,30 with a non-zero area, so what you state is correct:
But what you call bounding in that list is the corners of the triangle, not the bounding box.
If this shape should actually be called a triangle is a matter of definition, but since that’s built in to the question I would not think too much about that. Either your teacher is trying to make you confused or he/she did not think it through.
The bounding box ‘around (well, touching) any polygon is the rectangle with corners at
so, in your case