下面的代码代表一个多边形。我希望能够将一个点(用于x,y,z的gvector3对象)推到向量“顶点”上。我知道vector具有push_back方法,但是由于我已将顶点声明为私有,因此我认为我无法调用vertices.push_back(gvector3 xyz)。有人对我如何实现此建议有任何建议吗?
#ifndef POLYGON_H
#define POLYGON_H
#include <iostream>
#include <gvector3.h>
#include <vector>
class Polygon {
private:
std::vector <gVector3> vertices;
std::vector <gVector3> color;
public:
///----------------------------------------------------------------
/// Constructors
///----------------------------------------------------------------
/// Default constructor
Polygon();
///----------------------------------------------------------------
/// Getters and Setters
///----------------------------------------------------------------
/// Return vertex / reference to vertex at index
gVector3 operator[](unsigned int index) const;
gVector3 &operator[](unsigned int index);
///----------------------------------------------------------------
/// Draw Polygon
///----------------------------------------------------------------
void drawPolygon(const Polygon &p);
};
std::ostream &operator<<(std::ostream &o, const Polygon p);
#endif // POLYGON_H
最佳答案
给您的多边形添加顶点的方法?
void Polygon::addVertex(const gVector3 &vec) {
vertices.push_back(vec);
}
关于c++ - 使用STD Vector类的面类,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21666039/