【发布时间】:2011-07-15 22:37:59
【问题描述】:
嘿,我有一个名为 Partition 的抽象类,它是一个函子,它是我的 ConcavePolygon 的成员> 类。 Partition Functor 依赖于很多 ConcavePolygon 的数据,例如 TPPLPoints 和 SFMLPoints。
我发现即使我已经在它里面定义了类 取决于,我无法轻易获得 Concaves 的数据。 我该怎么做 这个?
我也想使用 Body 类中的一些功能,并希望 通过 ConcavePolygon 做到这一点,因为它是它的后代。 (需要 AddShape() 函数);
如果有帮助,这里是代码:
class ConcavePolygon : public Body{
protected:
std::list<Vector2f> SFMLPoints;
std::vector <TPPLPoint> TPPLPoints; //TODO: figure out how to make a temp version without Memory Exception
public:
//////////////////// Partitioning/Triangulating Classes /////////////////////////////////////////////////////////////
class Partition{
protected:
virtual void RunAlgorithm(){};
public:
Partition(Vector2f* Points, long numbPoints){ //TODO turn this into a base class for triangulate or Convexulate
//rev up all the needed data structs
std::list<TPPLPoly> PartitionOutput;
std::list <TPPLPoly> ::iterator I;
//Backup the points, and convert them to tppl
for(int I=0; I<numbPoints; I++){
TPPLPoints.push_back(TPPLPoint(Points[I].x, Points[I].y));
SFMLPoints.push_back(Points[I]);}
TPPLPoly Poly(&TPPLPoints[0], numbPoints, false);
//clear everything to be filled with the Partition Algorithm
this->Clear();
// Run the Partitioning Algorithm
RunAlgorithm();
// Convert results to SFML points, shapes, and add to the body
for( I= PartitionOutput.begin(); I!= PartitionOutput.end();I++){
sf::Shape TempShape;
for(int i=0; i< I->GetNumPoints(); i++)
TempShape.AddPoint( I->GetPoint(i).x, I->GetPoint(i).y);
this->AddShape(TempShape);
}
};
};
class Convexulate: public Partition{
bool RunAlgorithm(TPPLPoly& Poly, std::list<TPPLPoly>& PartitionOutput){
TPPLPartition Partition;
Partition.ConvexPartition_OPT(&Poly, &PartitionOutput);
};
};
class Triangulate: public Partition{
bool RunAlgorithm(TPPLPoly& Poly, std::list<TPPLPoly>& PartitionOutput){
TPPLPartition Partition;
Partition.Triangulate_OPT(&Poly, &PartitionOutput);
};
};
//////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////// Constructors /////////////////////////////////////////////////////
ConcavePolygon(Vector2f* Points, long numbPoints){
Convexulate(Points, numbPoints);
};
};// ConcavePolygon Class
【问题讨论】:
-
提示:不要过度使用 cmets。
//////////的大行字根本不会提高阅读效果,反而会降低舒适度。此外,代码告诉如何,如果你做得对,它会告诉你什么,而 cmets 应该告诉你为什么。所以说//// Constructors ////是完全没有必要的,代码已经说了。Run the Partitioning Algorithm也是如此。不过//clear everything to be filled with the Partition Algorithm没问题,因为它解释了为什么会发生某些事情。
标签: c++ class polymorphism membership functor