Drawing Polyline problem

Hi everybody,

I´ve just started to play around with OF and I´ve written some code that should draw a line-class derived from ofPolyline so that it looks as the line would be drawn. My code even works but when I add Vertices with a mouseklick it actually should go on to draw the line, but instead it first draws a line to some random point before it finally displays the final line. I think the problem is that the size of the Polyline is actualised before the actual element is inserted and so it draws a line to an uninitialized point. Can anybody help me?

here is the code:

Header:

  
  
#pragma once  
  
#include "ofMain.h"  
  
class slowPolyline : public ofPolyline{  
  
    public:  
  
        int lineSegmentCounter;         //Number of drawn segments on the current line  
        int maxLineSegmentCounter;      //total number until finish of the current line  
        int fullLineCounter;            //Number of already finished lines  
        ofVec2f direction;              //direction of the currently drawn line  
        ofVec2f drawnDist;              //already drawn distance of the currently drawn line  
        ofVec2f offset;  
        float segmentsize;  
        time_t maxtime;  
  
             slowPolyline();  
        void slowRedraw();  
        void slowDraw();  
        void update();  
  
    private:  
  
        time_t lasttime;  
};  
  
class testApp : public ofBaseApp{  
  
	public:  
  
        slowPolyline testline;  
  
		void setup();  
		void update();  
		void draw();  
  
		void keyPressed  (int key);  
		void keyReleased(int key);  
		void mouseMoved(int x, int y );  
		void mouseDragged(int x, int y, int button);  
		void mousePressed(int x, int y, int button);  
		void mouseReleased(int x, int y, int button);  
		void windowResized(int w, int h);  
		void dragEvent(ofDragInfo dragInfo);  
		void gotMessage(ofMessage msg);  
  
};  
  

CPP file:

  
  
#include "testApp.h"  
  
//--------------------------------------------------------------  
void testApp::setup(){  
    ofSetFrameRate(30);  
  
    testline.addVertex(-100,100);  
    testline.addVertex(100,100);  
    testline.addVertex(150,150);  
    testline.addVertex(300,150);  
    testline.addVertex(350,200);  
    testline.addVertex(450,200);  
    testline.offset.set(300,0);  
}  
  
//--------------------------------------------------------------  
void testApp::update(){  
    testline.update();  
}  
//--------------------------------------------------------------  
void testApp::draw(){  
    testline.slowDraw();  
    //cout<<"fullLineCounter: "<<testline.fullLineCounter<<" lineSegmentCounter: "<<testline.lineSegmentCounter<<endl;  
    //cout<<"mouse x: "<<mouseX<<" mouse Y"<<mouseY<<endl;}  
}  
//--------------------------------------------------------------  
void testApp::keyPressed(int key){  
  
}  
  
//--------------------------------------------------------------  
void testApp::keyReleased(int key){  
  
}  
  
//--------------------------------------------------------------  
void testApp::mouseMoved(int x, int y ){  
  
}  
  
//--------------------------------------------------------------  
void testApp::mouseDragged(int x, int y, int button){  
  
}  
  
//--------------------------------------------------------------  
void testApp::mousePressed(int x, int y, int button){  
  
    if(button == 2)  
    {  
        testline.addVertex(mouseX-testline.offset.x,mouseY-testline.offset.y);  
    }  
    if(button == 1)  
    {  
  
    system("cls");  
    for(int i=0;i<=testline.size();i++)  
    {  
        cout<<"Vertex ["<<i<<"]: "<<testline[i]<<endl;  
    }  
    cout<<".size() = "<<testline.size();  
    }  
    if(button == 0)  
    {  
        testline.slowRedraw();  
    }  
}  
  
//--------------------------------------------------------------  
void testApp::mouseReleased(int x, int y, int button){  
  
}  
  
//--------------------------------------------------------------  
void testApp::windowResized(int w, int h){  
  
}  
  
//--------------------------------------------------------------  
void testApp::gotMessage(ofMessage msg){  
  
}  
  
//--------------------------------------------------------------  
void testApp::dragEvent(ofDragInfo dragInfo){  
  
}  
  
//--------------------------------------------------------------  
slowPolyline::slowPolyline(){  
    fullLineCounter = 0;  
    lineSegmentCounter = 0;  
    drawnDist.set(0,0);  
    direction.set(0,0);  
    offset.set(0,0);  
    segmentsize = 5;  
    maxtime = 10;  
    lasttime = ofGetSystemTime();  
}  
  
//--------------------------------------------------------------  
void slowPolyline::slowRedraw()  
{  
    fullLineCounter = 0;  
    lineSegmentCounter = 0;  
    drawnDist.set(0,0);  
    lasttime = ofGetSystemTime();  
}  
  
//--------------------------------------------------------------  
void slowPolyline::slowDraw()  
{  
    for(int i=0;i<fullLineCounter;i++)  
    {  
        ofLine((*this)[i]+offset,(*this)[i+1]+offset);  
    }  
    if(fullLineCounter<this->size())  
    {  
        ofLine((*this)[fullLineCounter]+offset,(*this)[fullLineCounter]+drawnDist+offset);  
    }  
}  
  
//--------------------------------------------------------------  
void slowPolyline::update()  
{  
    if(fullLineCounter < (*this).size()-1)  
    {  
        if(fullLineCounter == 0 && (*this).size() >= 2)  
        {  
            ofVec2f temp = ((*this)[1]-(*this)[0]);  
            maxLineSegmentCounter = temp.length()/segmentsize;  
            direction.set((*this)[1]-(*this)[0]);  
            direction.scale(segmentsize);  
        }  
        if((ofGetSystemTime() - lasttime) > maxtime)  
        {  
            lasttime = ofGetSystemTime();  
            if(lineSegmentCounter < maxLineSegmentCounter)  
            {  
                lineSegmentCounter++;  
                drawnDist += direction;  
            }  
            else  
            {  
                fullLineCounter++;  
                lineSegmentCounter = 0;  
                ofVec2f temp = ((*this)[fullLineCounter+1]-(*this)[fullLineCounter]);  
                maxLineSegmentCounter = temp.length()/segmentsize;  
                drawnDist.set(0,0);  
                direction.set(temp);  
                direction.scale(segmentsize);  
            }  
        }  
    }  
}  
  

I think part looks a bit strange:

  
  
  
 for(int i=0;i<fullLineCounter;i++)    
    {    
        ofLine((*this)[i]+offset,(*this)[i+1]+offset);    
    }    
    if(fullLineCounter<this->size())    
    {    
        ofLine((*this)[fullLineCounter]+offset,(*this)[fullLineCounter]+drawnDist+offset);    
    }    
  

How do you know that i+1 in ofLine((*this)[i]+offset,(*this)[i+1]+offset); isn’t past the end of the vector?

Hey… finally I got it working :smiley:

her is the code:

  
void slowPolyline::slowDraw()  
{  
    for(int i=0;i<fullLineCounter;i++)  
    {  
        ofLine((*this)[i]+offset,(*this)[i+1]+offset);  
    }  
    if(fullLineCounter<this->size())  
    {  
        ofLine((*this)[fullLineCounter]+offset,(*this)[fullLineCounter]+drawnDist+offset);  
    }  
}  
  
//--------------------------------------------------------------  
void slowPolyline::update()  
{  
    if(fullLineCounter < (*this).size()-1)  
    {  
        if(fullLineCounter == 0 && (*this).size() >= 2)  
        {  
            ofVec2f temp = ((*this)[1]-(*this)[0]);  
            maxLineSegmentCounter = temp.length()/segmentsize;  
            direction.set((*this)[1]-(*this)[0]);  
            direction.scale(segmentsize);  
        }  
        if((ofGetSystemTime() - lasttime) > maxtime)  
        {  
            lasttime = ofGetSystemTime();  
            if(lineSegmentCounter < maxLineSegmentCounter)  
            {  
                lineSegmentCounter++;  
                drawnDist += direction;  
            }  
            else if(fullLineCounter < (*this).size()-2)  
            {  
                fullLineCounter++;  
                lineSegmentCounter = 0;  
                ofVec2f temp = ((*this)[fullLineCounter+1]-(*this)[fullLineCounter]);  
                maxLineSegmentCounter = temp.length()/segmentsize;  
                drawnDist.set(0,0);  
                direction.set(temp);  
                direction.scale(segmentsize);  
            }  
        }  
    }  
}  

the only thing that was missing was an additional

  
else if(fullLineCounter < (*this).size()-2)