我正在尝试实现一个程序,当用户单击屏幕时,将在该程序中从点到点绘制线条。我有一个折线类,它是自由形式类的子类,它是曲线类的子类。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();
};
};