我正在尝试实现一个程序,当用户点击屏幕时,将从一个点到另一个点绘制线条。我有一个Polyline类,它是Freeform类的子类,它是Curve类的子类。 Curve超类的draw方法通常调用getPoint,它将获得该特定点的曲线的加权点。但是,对于Polyline的draw方法,我试图覆盖曲线类以获得用户点击的点(正如您所看到的,Polyline类的draw方法从不调用getPoint)。但是,当我调试代码时,我发现当我尝试绘制折线时仍然会调用getPoint。有什么建议吗?
class Curve {
public:
virtual float2 getPoint(float t)=0;
void draw(){
glBegin(GL_LINE_STRIP);
for (float i = 0; i < 1; i+=.01) {
float2 point = getPoint(i);
float x = point.x;
float y = point.y;
glVertex2d(x, y);
}
glEnd();
};
};
class Freeform : public Curve
{
protected:
std::vector<float2> controlPoints;
public:
virtual float2 getPoint(float t)=0;
virtual void addControlPoint(float2 p)
{
controlPoints.push_back(p);
}
void drawControlPoints(){
glBegin(GL_POINTS);
for (float i = 0; i < controlPoints.size(); i++) {
float2 point = controlPoints.at(i);
float x = point.x;
float y = point.y;
glVertex2d(x, y);
}
glEnd();// draw points at control points
}
};
class Polyline : public Freeform {
public:
float2 getPoint(float t) {
return float2(0.0, 0.0);
}
//we add a control point (in this case, control point is where mouse is clicked)
void addControlPoint(float2 p)
{
controlPoints.push_back(p);
}
//trying to override Curve draw method
void draw(){
glBegin(GL_LINE_STRIP);
for (float i = 0; i < controlPoints.size(); i++) {
float2 point = controlPoints.at(i);
float x = point.x;
float y = point.y;
glVertex2d(x, y);
}
glEnd();
};
};