Results 1 to 3 of 3

Thread: How to paint cross filling?

  1. #1
    Join Date
    Jul 2013
    Posts
    72
    Qt products
    Qt5
    Platforms
    Unix/X11
    Thanks
    1

    Default How to paint cross filling?

    Fill curve with different brushes, while two curves' cross. Like the picture
    选区_001.png


    My code like this.

    void FillAreaCurve::fillCurve(QPainter *painter, const QwtScaleMap &xMap, const QwtScaleMap &yMap, const QRectF &canvasRect, QPolygonF &polygon) const
    {
    .........
    // calculate crossing points
    for ( int i = 0; i < corssingNum; i++ ) {
    ......
    // according to different terms, get the clipRect and assign different brush to painter.
    painter->save();
    painter->setClipRect( clipRect, Qt::IntersectClip );
    closePolyline( painter, xMap, yMap, polygon );
    polygon = QwtClipper::clipPolygonF( canvasRect, polygon, true );
    painter->setPen( Qt::NoPen );
    painter->setBrush( brush );
    QwtPainter::drawPolygon( painter, polygon );
    painter->restore();
    }
    }

    Apparently, how much time it would consume is determined by the crossing points' number, which is out of my control.
    Is there another way to fill my curves quicker ?

  2. #2
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,325
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows
    Thanked 879 Times in 827 Posts

    Default Re: How to paint cross filling?

    If you don't want to implement the filling algo yourself ( setting pixels on a QImage ) using clip rects is not a bad idea.

    Assuming you already have created a polygon ( clipped to the canvas rectangle ) with the outline of both points in paint device coordinates and your list of crossing points is also clipped, so that all points outside the canvas rectangle are not included:
    Then all that needs to be done in your loop is:

    Qt Code:
    1. double y1 = canvasRect.y();
    2.  
    3. for ( int i = 0; i < crossingNum; i++ )
    4. {
    5. const double y2 = yMap.transform( crossingPoint[i].y() );
    6.  
    7. painter->setClipRect( canvasRect.x(), y1, canvasRect.width(), y2 - y1 );
    8. painter->setBrush( ... );
    9. painter->drawPolygon( ... );
    10.  
    11. y1 = y2;
    12. }
    13.  
    14. painter->setClipRect( canvasRect.x(), y1, canvasRect.width(), canvasRect.bottom() - y1 );
    15. painter->setBrush( ... );
    16. painter->drawPolygon( ... );
    To copy to clipboard, switch view to plain text mode 

    Qt is known for rendering before clipping, so it's worth to try to clip the polygon with QwtClipper against the current clip rectangle, before calling QPainter::drawPolygon. If this results in a significant better performance you could consider to implement a faster clipping algo instead of Sutherland-Hodgman using the fact, that your points are ordered in vertical direction.

    Uwe

  3. #3
    Join Date
    Jul 2013
    Posts
    72
    Qt products
    Qt5
    Platforms
    Unix/X11
    Thanks
    1

    Default Re: How to paint cross filling?

    Thanks you. I'll try some other clipping algo.

Similar Threads

  1. Replies: 7
    Last Post: 14th August 2013, 16:02
  2. Replies: 1
    Last Post: 11th November 2010, 00:31
  3. Filling a QGraphicsItem
    By c_srikanth1984 in forum Qt Programming
    Replies: 15
    Last Post: 6th July 2009, 14:34
  4. Filling an image
    By Caius Aérobus in forum Qt Programming
    Replies: 6
    Last Post: 6th July 2008, 21:13
  5. filling scrollview
    By illuzioner in forum Qt Programming
    Replies: 1
    Last Post: 17th February 2006, 22:00

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.