CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Jun 1999
    Posts
    505

    Prob with FloodFill

    Hi,
    FloodFill is not working. I am using Visual C 6.0 in windows 7 environment. Its not filling a triangle.

    My code is:

    Code:
       
    void CFloodFillExView::OnDraw(CDC* pDC)
    {
    	CFloodFillExDoc* pDoc = GetDocument();
    	ASSERT_VALID(pDoc);
    	// TODO: add draw code for native data here
    	CPoint Pt[7];
    	Pt[0] = CPoint(300, 400);
    	Pt[1] = CPoint(200, 300);
    	Pt[2] = CPoint(600, 400);
    	pDC->Polygon(Pt, 3);
    	pDC->SetPixel(400, 390, RGB(0,0,0));
    	CBrush brSYell(RGB(0,0,0));
        CBrush* pOldBrush=NULL;
        pOldBrush=pDC->SelectObject(&brSYell);
    //select the new pen
    //Now Draw a triangle
       
        pDC->FloodFill(400, 350, RGB(0,0,0));
        pDC->SelectObject(pOldBrush);
    
    }
    Somebody plz guide me. Its an urgent problem.
    Zulfi.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    Re: Prob with FloodFill

    Define "not working"
    Victor Nijegorodov

  3. #3
    Join Date
    Jan 2012
    Location
    India
    Posts
    193

    Re: Prob with FloodFill

    Hello .. I am also learning VC++ ..

    Just tried ur prog out of curiocity ..

    Remove your pDC->SetPixel or color the pixel in different color than (0,0,0) ..

    Code works ..

  4. #4
    Join Date
    Jun 1999
    Posts
    505

    Re: Prob with FloodFill

    Hi,
    Thanks for your attention. Sorry i dont know what should i say with this problem. I have already stated that its not filling the triangle. Let me know what should i state??
    I have tried this again and its now working.
    This pixel command just helps me to find out whether i am inside the triangle or not.

    The pixel coordinates were correct but the coordinates of floodfill might not be inside the triangle. I changed it and its now working.
    However if i use RGB(0,0,0) with pixel it does not work. Somebody plz guide me whats the problem??
    CPoint Pt[7];
    Pt[0] = CPoint(300, 400);
    Pt[1] = CPoint(200, 300);
    Pt[2] = CPoint(600, 400);
    pDC->Polygon(Pt, 3);
    pDC->SetPixel(400, 390, RGB(10,10,10));
    CBrush brSYell(RGB(0,0,0));
    CBrush* pOldBrush=NULL;
    pOldBrush=pDC->SelectObject(&brSYell);
    //select the new pen
    //Now Draw a triangle

    pDC->FloodFill(400, 390, RGB(0,0,0));
    pDC->SelectObject(pOldBrush);

    Plz guide me about the issue related to setpixel which i have stated above in this post??
    Zulfi.

  5. #5
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: Prob with FloodFill

    You seem to fail to understand how FLoodFill works.

    you're setting a pixel
    you're flood filling at that very same pixel location thus recoloring the pixel.
    it'll stop at the pixel because the one pixel in whatever color does not neighbour any other pixels of the same color.


    Floodfill is not the ideal method to draw a filled triangle though...

    The "correct" way to do it, would be to create and select the background as a brush (SetObject the brush), set the polygon fill mode (SetPolyFillMode), and then use the Polygon() you're already using now to immediately draw a filled triangle.
    (don't forget to restore the polyfillmode and the brush into the dc)

  6. #6
    Join Date
    Jun 1999
    Posts
    505

    Re: Prob with FloodFill

    Hi,
    Thanks for providing me extra knowledge but its not working (not filling the triangle, no syntax error)

    CBrush brBlack(RGB(255,255,255));
    CBrush* pOldBrush2=NULL;
    pOldBrush2=pDC->SelectObject(&brBlack);
    pDC->SetPolyFillMode(2);
    Pt[0] = CPoint(300, 100);
    Pt[1] = CPoint(400, 10);
    Pt[2] = CPoint(600, 300);
    pDC->Polygon(Pt, 3);
    pDC->SelectObject(pOldBrush2);
    Actually, I dont know:
    select the background as a brush (SetObject the brush)
    & I dont know:
    don't forget to restore the polyfillmode
    Kindly guide me.

    Zulfi.

  7. #7
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: Prob with FloodFill

    Code:
    CBrush brBlack(RGB(255,255,255));
    This is actually a white brush; hard to see over the white background

    You should restore the mode just like you did the brush:
    Code:
    int iOldMode = pDC->SetPolyFillMode(WINDING); 
    // do your drawing
    pDC->SetPolyFillMode(iOldMode);
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  8. #8
    Join Date
    Jun 1999
    Posts
    505

    Re: Prob with FloodFill

    Hi,
    Thanks. It worked. In my view the advantage is that we can fill any color in the polygon. We dont have to bother about the boundary colors. But i case of Floodfill we have to fill the same color which is the boundary color? Plz tell me if i am right??

    CBrush brBlack(RGB(255,10,10));
    CBrush* pOldBrush2=NULL;
    pOldBrush2=pDC->SelectObject(&brBlack);
    int iOldMode = pDC->SetPolyFillMode(WINDING);
    // do your drawing
    pDC->SetPolyFillMode(2);
    Pt[0] = CPoint(300, 100);
    Pt[1] = CPoint(400, 10);
    Pt[2] = CPoint(600, 300);
    pDC->Polygon(Pt, 3);
    pDC->SelectObject(pOldBrush2);
    pDC->SetPolyFillMode(iOldMode);
    Zulfi.

  9. #9
    Join Date
    Jun 1999
    Posts
    505

    Re: Prob with FloodFill

    Hi,

    CBrush brBlack(RGB(0,255,0));
    CBrush* pOldBrush2=NULL;
    pOldBrush2=pDC->SelectObject(&brBlack);
    //int iOldMode = pDC->SetPolyFillMode(WINDING);
    // do your drawing
    pDC->SetPolyFillMode(2);
    Pt[0] = CPoint(300, 100);
    Pt[1] = CPoint(400, 10);
    Pt[2] = CPoint(600, 300);
    //pDC->SetPixel(350,75, RGB(255, 10, 10));
    pDC->Polygon(Pt, 3);
    pDC->SelectObject(pOldBrush2);
    //pDC->SetPolyFillMode(iOldMode);

    In the above code, I have even commented SetPolyFillMode but its still doing filling. Kindly guide me.

    Zulfi.

  10. #10
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: Prob with FloodFill

    But i case of Floodfill we have to fill the same color which is the boundary color? Plz tell me if i am right??
    Not quite...

    THe idea behind a floodfill is that you start at a pixel.
    The floodfill "remembers" that pixelcolor
    it recolors the pixel in the supplied floodcolor
    it then checks the pixel above, left, right, below the current pixel.
    if the color of the pixel above (left, right, below) is the same as the remembered pixel, the floodfill "recurses" into the pixel above (left, right, below)

    so basically if you floodfill green into a blue pixel.
    then that blue pixel and all it's 'connected' pixels which are also blue will be "flooded" with green.

    FLoodfill has it's uses, but it's not ideal for everything. For a triangle, the internal fill is considerably faster (mainly because there is no need to maintain a floodlist or check pixel colors).

    In the above code, I have even commented SetPolyFillMode but its still doing filling. Kindly guide me
    There is ALWAYS a fillmode, either winding or alternating, so it is Always brush-filled. via either of these 2 methods.

    If you want to draw an "empty" polygon (only the outline), you'll need to select a hollow (BS_HOLLOW) (or NULL) brush into the DC. With MFC, you easily get one of those with
    Code:
    CBrush* pbr = CBrush::FromHandle((HBRUSH)GetStockObject(HOLLOW_BRUSH));

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured