Reputation: 11
I'm trying to create an equilateral triangle that outputs to the console but my code seems to output the triangle at point 0,0.
How can I fix this?
this is what I have:
Header file:
#include "shape.h"
#include "vertex.h"
#include <list>
// An equilateral triangle
class Triangle : public Shape
{
// the radius provides the length of a side
// and enables a vertex to be plotted from
// which the other two vertices can be derived
// via rotation
int radius;
public:
// constructor
Triangle(Vertex point, int radius = 10);
// calculates and returns a triangle's area
int area();
// calculates and returns a triangle's perimeter
int perimeter();
};
and the cpp file
#include "triangle.h"
#include "vertex.h"
#include <list>
Triangle::Triangle(Vertex point, int radius) : Shape(point)
{
this->radius = radius;
this->centroid = Vertex(0,0);
vertices.push_back(Vertex(centroid.getY() + radius));
vertices.push_back(Vertex(centroid.getY() + (radius*2)));
vertices.push_back(Vertex(centroid.getX() * cos(120) - centroid.getY() * sin(120),centroid.getY() * cos(120) + centroid.getX() * sin(120)));
vertices.push_back(Vertex(centroid.getX() * cos(240) - centroid.getY() * sin(240),centroid.getY() * cos(240) + centroid.getX() * sin(240)));
this->centroid = point;
}
// returns the area of an equilateral triangle
int Triangle::area()
{
return radius*radius*(sqrt(3)/4);
}
// returns the perimeter of an equilateral triangle
int Triangle::perimeter()
{
return radius *3;
}
I'm not sure what's wrong with it. I have tried many different ways to fix it but I have had no luck in doing so. can somebody please help?
Upvotes: 0
Views: 218
Reputation: 5118
As noted in the other answer, sin
and cos
take their arguments in radians, not degrees.
Furthermore, your constructor seems strange. Bear in mind that I don't have all the details behind your code (Vertex
, Shape
, ...), but assuming that vertices
needs to contain the three vertices of your triangle and that Vertex
has a constructor taking the three coordinates, I'd try something like this:
Triangle::Triangle(Vertex point, int radius) : Shape(point)
{
this->radius = radius;
// in any case, you want the traingle to be centered around the point given as input
this->centroid = point;
// we can just avoid calling trigonometric functions at runtime
// also, the values for these particular angles are well-known, so we don't need to call any actual trigonometric functions
static const float COS_60 = 0.5f;
static const float COS_30 = 0.5f * sqrt(3.f);
// compute the length of the side of the triangle based on its radius
// for instance, using any of the six right triangles between the center, a vertice and the projection of the center against one of the sides
const float side = radius * 2.f * COS_30;
// more basic geometry
const float bottomHeight = point.getY() - COS_60 * radius;
// first vertice is right above the center
this->vertices.push_back(Vertex(point.getX(), point.getY() + radius));
// second vertice is at the bottom height, and its X position is offset by half the side
this->vertices.push_back(Vertex(point.getX() + COS_60 * side, bottomHeight));
// same, but in the other direction
this->vertices.push_back(Vertex(point.getX() - COS_60 * side, bottomHeight));
}
Upvotes: 0
Reputation: 36896
sin
, cos
, etc use radians, not degrees.
Also you set your centroid at a hard-coded 0,0
.
Upvotes: 4