コード例 #1
0
// Draw each path onto the passed cairo context
void PathSimplificationApp::drawPath(cairo::Context &ctx, SmoothPath *smoothPath, int mode)
{
    // initialize the line settings
    float thickness = 1.0;
    ctx.setLineWidth( 1.0f );
    ctx.setLineCap(cairo::LINE_CAP_ROUND);
    ctx.setLineJoin(cairo::LINE_JOIN_ROUND);

    
    if(smoothPath->inProgress){
        // draw lines point to point
        vector<Vec2f> pathPoints = smoothPath->getPathPoints();
        for (int i=1; i<pathPoints.size(); i++) {
            drawLine(ctx, pathPoints[i-1], pathPoints[i], 1.0, Color(1.0, 0.0, 0.0));
            drawCircle(ctx, pathPoints[i], 2, Color(1.0, 0.0, 0.0));
        }
    }else{
        // draw smooth lines
        Path2d path = smoothPath->getCurrentPath();
        drawCurves(ctx, path, thickness, Color(255.0, 0.0, 0.0));
        
        int pointIndex = 0;
        // draw circles at the bezier points
        for (int i=0; i<path.getNumSegments(); i++) {
            Vec2f c1 = path.getPoint(pointIndex+1);
            Vec2f c2 = path.getPoint(pointIndex+2);
            Vec2f pt1 = path.getPoint(pointIndex);
            Vec2f pt2 = path.getPoint(pointIndex+3);
            drawCircle(ctx, c1, 1, Color(1.0f, 1.0f, 0.0f), true);
            drawCircle(ctx, c2, 1, Color(0.0f, 1.0f, 1.0f));
            drawLine(ctx, c1, pt1, 1, Color(1.0, 1.0, 0.0 ));
            drawLine(ctx, c2, pt2, 1, Color(0.0, 1.0, 1.0 ));
            pointIndex += 3;
        }
    }   
}
コード例 #2
0
// Draw each path onto the passed cairo context
void ContinuousPathApp::drawPath(cairo::Context &ctx, SmoothPath *smoothPath, int mode)
{
    // update the drawing points only if the line hasn't been completed
    
    Path2d path = smoothPath->getCurrentPath();
    vector<Vec2f> pathPoints = smoothPath->getPathPoints();
    vector<int>   endPoints = smoothPath->getEndPoints();
    
    if(path.getNumPoints() == 0) return;
    
    
    // initialize the line settings
    float thickness = 1.0;
    ctx.setLineWidth( 1.0f );
    ctx.setLineCap(cairo::LINE_CAP_ROUND);
    ctx.setLineJoin(cairo::LINE_JOIN_ROUND);
    
    Vec2f pt = path.getPoint(0);
    
    
    // draw bezier line
    if (mode != 5) {
        // draw the line based on bezier points
        drawCurves(ctx, path, thickness, Color(255.0, 0.0, 0.0));
    }
    
    
    // draw circles at each of the original path points
    if(mode != 1 && mode != 5){
        for (int i=0; i<pathPoints.size(); i++) {
            Vec2f pt = pathPoints[i];        
            drawCircle(ctx, pt, 1, Color( 1.0f, 1.0f, 1.0f ));
        }
    }
    
    
    int i;
    int pointIndex = 0;
    // draw circles at the bezier points
    for (i=0; i<path.getNumSegments(); i++) {
        
        int segType = path.getSegmentType(i);
        
        // change jumpIndex depending on the type of segment
        switch(segType){
            case Path2d::CUBICTO:
            {
                Vec2f c1 = path.getPoint(pointIndex+1);
                Vec2f c2 = path.getPoint(pointIndex+2);
                Vec2f pt1 = path.getPoint(pointIndex);
                Vec2f pt2 = path.getPoint(pointIndex+3);
                
                
                if (mode == 2 || mode == 3) {
                    if(mode == 2){
                        for(int j=0; j<endPoints.size(); j++){
                            if(endPoints[j] == pointIndex){
                                drawCircle(ctx, pt2, 8, Color(0.0f, 1.0f, 1.0f), true);
                            }
                        }
                    }
                    if(mode == 3){
                        // draw the control points and tangent lines
                        drawCircle(ctx, c1, 2, Color(1.0f, 1.0f, 0.0f), true);
                        drawCircle(ctx, c2, 2, Color(0.0f, 1.0f, 1.0f));
                        drawLine(ctx, c1, pt1, 1, Color(1.0, 1.0, 0.0 ));
                        drawLine(ctx, c2, pt2, 1, Color(0.0, 1.0, 1.0 ));
                    }
                    drawCircle(ctx, pt2, 2, Color(1.0f, 0.0f, 1.0f));
                    drawCircle(ctx, pt1, 2, Color(1.0f, 0.0f, 0.0f), true);
                }
                
                if (mode == 4) {
                    
                    drawLine(ctx, pt1, pt2, 1, Color(1.0, 1.0, 1.0 ));
                    drawCircle(ctx, pt1, 5, Color(1.0, 0.0, 0.0), true);
                    drawCircle(ctx, pt2, 3, Color(1.0, 0.0, 1.0));
                }
                
                pointIndex += 3;
                break;
            }
                
            case Path2d::MOVETO:
                // don't do anything with this point
                pointIndex += 0;
                break;
                
            default:
                pointIndex += 1;
                break;
        }
    }
    
    
    if (mode== 5) {
        // draw a line between the last bezier point and the current point
        for (i=1; i<pathPoints.size(); i++) {
            drawLine(ctx, pathPoints[i-1], pathPoints[i], 1.0, Color(1.0, 0.0, 0.0));
        }
    }
}