我的C++代码有什么问题?
What's wrong with my C++ code?
我正在尝试创建一个输出到控制台的等边三角形,但我的代码似乎在点 0,0 处输出三角形。
我该如何解决这个问题?
这是我所拥有的:
头文件:
#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();
};
和 CPP 文件
#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;
}
我不确定它有什么问题。我已经尝试了许多不同的方法来修复它,但我没有运气这样做。有人可以帮忙吗?
sin
、cos
等使用弧度,而不是度。
此外,您将质心设置为硬编码0,0
。
正如另一个答案所指出的,sin
和cos
以弧度而不是度数来接受他们的论点。
此外,您的构造函数似乎很奇怪。请记住,我没有代码背后的所有细节(Vertex
,Shape
,...),但是假设vertices
需要包含三角形的三个顶点,并且Vertex
有一个构造函数采用三个坐标,我会尝试这样的事情:
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));
}
相关文章:
- 警告处理为错误这里有什么问题
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 问题:什么是QAbstractItemView::NoEditTriggers的反面
- 当我尝试添加 2 个大字符串时,我无法弄清楚出了什么问题
- 违反const正确性:我应该现实地期待什么问题
- 这个带有模板<类 Vector 的C++代码片段有什么问题>
- 我的逻辑反转字符串中的元音有什么问题?
- 需要以下代码的帮助,下面的代码有什么问题
- 常量公共成员有什么问题?
- c++无值sort()的问题是什么?
- 以下代码中的函数模板有什么问题?
- 这个返回元素位置的基于循环的函数有什么问题?
- creat_list2功能有什么问题?
- 基本的 c++ 问题:如果我在函数中创建某些内容并返回它会发生什么?
- 我遇到了黑客排名中的问题"TWO STRINGS"的三个测试用例的分段错误。原因是什么?
- 什么是钻石问题?是一系列问题还是特定问题?
- 格式说明符C++有什么问题
- 我应该在 main 函数中写什么来测试我的问题?
- 任何人都可以告诉我我的 C++ 代码出了什么问题?
- 方法问题 - 什么会改变值,什么不会改变?什么是无效的?