CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 7 FirstFirst 12345 ... LastLast
Results 16 to 30 of 98
  1. #16
    Join Date
    Sep 2002
    Location
    DC Metro Area, USA
    Posts
    1,509
    Several things:

    [CODE]
    void CEditorDoc::SetSquareColor(int i, int j, COLORREF color)
    {
    ASSERT( i >= 0 && i <= GetGridY() && j >= 0 && j <= GetGridX() );
    m_clrGrid.At(i, j) = color;
    SetModifiedFlag(TRUE);
    CRect pRect( 20, -10, 30, -20 );
    UpdateAllViews( NULL, 1, ( CObject* )&pRect);
    }
    [\CODE]

    Will not work as the pRect vzariable is local will probably go out of scope if UpdateAllViews is posting messages...

    In your square drawing code (besides thte advice that Mathew gave you) you're continually recalculating the x & y's for the rect. You should move the y calculation to the outer loop as it will be the same for each j:

    [CODE]
    //
    //Draw the squares
    //
    CRect rect;

    rect.top = -10;
    rect.bottom = -20;

    for(int i = 0 ; i < 500 ; i ++)
    {
    rect.left = 10;
    rect.right = 20;

    for(int j = 0 ; j < 750 ; j ++)
    {
    COLORREF color = pDoc->GetSquareColor(i, j);
    pDC->FillSolidRect(rect, color);

    rect.left += 10;
    rect.right += 10;
    }

    rect.top -= 10;
    rect.bottom -= 20;
    }
    [\CODE]
    Note: I did not compile this code!!!

    This should help with some of the speed problems. Another optimization would be to get the clip region and only draw the rects that are contained inside of the clip regions bounding rect.
    bytz
    --This signature left intentionally blank--

  2. #17
    Join Date
    Jul 2003
    Location
    Singapore
    Posts
    1,822
    Originally posted by bytz
    Another optimization would be to get the clip region and only draw the rects that are contained inside of the clip regions bounding rect.
    this is what am trying to do since day one....
    but i have no clue whatsoever....
    i posted many questions but no one could help..
    can u help me ad tell ho wcan i use the GetClipRect in my scenario please??????
    tks a lot...really appreciate your help....
    R. Thomas
    "Be anxious for nothing, but in everything by prayer and supplication, with thanksgiving, let your requests be made know to God; and the peace of God, which surpasses all understanding, will guard your hearts and minds through Christ Jesus."Philippians 4:6-7
    "Rejoice always, pray without ceasing, in everything give thanks; for this is the will of God in Christ Jesus for you."1Thess. 5:16-18

  3. #18
    Join Date
    Jul 2003
    Location
    Singapore
    Posts
    1,822
    Originally posted by bytz
    In your square drawing code (besides thte advice that Mathew gave you) you're continually recalculating the x & y's for the rect. You should move the y calculation to the outer loop as it will be the same for each j:

    [CODE]
    //
    //Draw the squares
    //
    CRect rect;

    rect.top = -10;
    rect.bottom = -20;

    for(int i = 0 ; i < 500 ; i ++)
    {
    rect.left = 10;
    rect.right = 20;

    for(int j = 0 ; j < 750 ; j ++)
    {
    COLORREF color = pDoc->GetSquareColor(i, j);
    pDC->FillSolidRect(rect, color);

    rect.left += 10;
    rect.right += 10;
    }

    rect.top -= 10;
    rect.bottom -= 20;
    }
    [\CODE]
    Note: I did not compile this code!!!
    i didnt understand the above..i duno how to take it off my codes....
    if i use the above codes..where should i fill the rect??
    this is what i have done till now...
    Code:
    //
    	//Draw the squares
    	//
    	int nGridX = pDoc->GetGridX();
    	int nGridY = pDoc->GetGridY();
    	COLORREF color;
    	int x1;
    	int y1;
    	int x2;
    	int y2;
    	for(int i = 0 ; i < nGridY ; i ++) {
    		for(int j = 0 ; j < nGridX ; j ++) {
    			color = pDoc->GetSquareColor(i, j);
    			CBrush brush(color);
    			x1 = (j * 10) + 10;
    			y1 = (i * -10) - 10;
    			x2 = x1 + 10;
    			y2 = y1 - 10;
    			CRect rect(x1, y1, x2, y2);
    			pDC->FillRect(rect, &brush);
    		}
    	}
    but i still face the same probs......
    pls help.....
    aslo can u pls tell mw how can i use the clip rect method...
    tks a lot..
    really appreciate your help...
    R. Thomas
    "Be anxious for nothing, but in everything by prayer and supplication, with thanksgiving, let your requests be made know to God; and the peace of God, which surpasses all understanding, will guard your hearts and minds through Christ Jesus."Philippians 4:6-7
    "Rejoice always, pray without ceasing, in everything give thanks; for this is the will of God in Christ Jesus for you."1Thess. 5:16-18

  4. #19
    Join Date
    Jul 2003
    Location
    Singapore
    Posts
    1,822
    Originally posted by Mathew Joy
    Hi Joseph,
    If so why are you iterating the loop to the no of pixels times??? You should be iterating only the no of squares...since the no. of squares is the no of pixels/10 you need to iterate only that many times.
    hi. i am iteraing the number of pixels cause i have to draw the four corners of rectangle....if am wrong..pls tell me how can i do it the way you are suggesting....
    above you can see the code that i have changed according to what you said..
    if you want pls take a look at my proj that is attached below...tks a lot....
    really appreciate your help...
    Attached Files Attached Files
    R. Thomas
    "Be anxious for nothing, but in everything by prayer and supplication, with thanksgiving, let your requests be made know to God; and the peace of God, which surpasses all understanding, will guard your hearts and minds through Christ Jesus."Philippians 4:6-7
    "Rejoice always, pray without ceasing, in everything give thanks; for this is the will of God in Christ Jesus for you."1Thess. 5:16-18

  5. #20
    Join Date
    Feb 2003
    Location
    Bangalore, India
    Posts
    1,354
    Originally posted by Joseph_R_Thomas
    hi. i am iteraing the number of pixels cause i have to draw the four corners of rectangle....if am wrong..pls tell me how can i do it the way you are suggesting....
    above you can see the code that i have changed according to what you said..
    if you want pls take a look at my proj that is attached below...tks a lot....
    really appreciate your help...
    In your project you are supposed to display(create) a bitmap of the height h (say) and width w, as specified in the dialoge box that appears when the project starts. Next you should tell which method u've chosen. Either way (using brushes or floodfill), you are trying to fill squares right? And if you not are choosing from many colors both are almost same in terms of using processor power (to the eye). But I guess floodfill is better than using brushes.
    The outer loop indictate the rows and the inner the cols. (or y and x) Note here I'm talking about the loop that follows
    Code:
    //draw the squares
    . Now why are you iterating the outerloop the y times (or rather nGridY) and the inner x times? You are not doing something with each pixel are you? so it should be the no of square times. Since the size of the square is 10 pixels you should iterate nGridY/10 times and nGridX/10 times.

    And let me tell you...if you want to optimize that loop the first thing you should do is to take the
    Code:
    CBrush brush(color);
    out of the loop. And also make sure that you are not drawing anything outside the client window(and not much even if you are). As others suggested in this thread you should clip the region to your client window.

    Guess this helps
    Even if our suggestions didn't help, please post the answer once you find it. We took the effort to help you, please return it to others.

    * While posting code sections please use CODE tags
    * Please check the codeguru FAQ and do a little search to see if your question have been answered before.
    * Like a post, Rate The Post
    * I blog: Network programming, Bible

    I do all things thru CHRIST who strengthens me

  6. #21
    Join Date
    Feb 2003
    Location
    Bangalore, India
    Posts
    1,354
    And when you are dealing with points in graphics, test to see if they are correct. (You can use SetPixel() for instance)
    Even if our suggestions didn't help, please post the answer once you find it. We took the effort to help you, please return it to others.

    * While posting code sections please use CODE tags
    * Please check the codeguru FAQ and do a little search to see if your question have been answered before.
    * Like a post, Rate The Post
    * I blog: Network programming, Bible

    I do all things thru CHRIST who strengthens me

  7. #22
    Join Date
    Jul 2003
    Location
    Singapore
    Posts
    1,822
    Originally posted by Mathew Joy
    But I guess floodfill is better than using brushes.
    can u pl tell me how can i replace my code with floodfill please????

    Originally posted by Mathew Joy

    And let me tell you...if you want to optimize that loop the first thing you should do is to take the
    Code:
    CBrush brush(color);
    out of the loop
    i canot take it out...cause if i take it out i cannot draw the brush...
    if i put this outside the code
    CBrush brush
    then inside the loop it doesnt allow me to do this
    brush(color)
    so i am using it inside...any way ai can do the above so tat i can move
    CBrush brush
    to out of the loop???
    this is what i have done till now with your kind help..
    Code:
    	//
    	//Draw the squares
    	//
    	int nGridX = (pDoc->GetGridX()/10);
    	int nGridY = (pDoc->GetGridY()/10);
    	COLORREF color;
    	int x1;
    	int y1;
    	int x2;
    	int y2;
    	for(int i = 0 ; i < nGridY ; i ++) {
    		for(int j = 0 ; j < nGridX ; j ++) {
    			color = pDoc->GetSquareColor(i, j);
    			CBrush brush(color);
    			x1 = (j * 10) + 10;
    			y1 = (i * -10) - 10;
    			x2 = x1 + 10;
    			y2 = y1 - 10;
    			CRect rect(x1, y1, x2, y2);
    			pDC->FillRect(rect, &brush);
    		}
    	}

    Originally posted by Mathew Joy
    As others suggested in this thread you should clip the region to your client window.
    i have no idea how to applu the getclip to my proj...i asked many questions byut noone could help...can u pls help me on GetClipRect??
    tks a lot...really appreciate your time and help..
    R. Thomas
    "Be anxious for nothing, but in everything by prayer and supplication, with thanksgiving, let your requests be made know to God; and the peace of God, which surpasses all understanding, will guard your hearts and minds through Christ Jesus."Philippians 4:6-7
    "Rejoice always, pray without ceasing, in everything give thanks; for this is the will of God in Christ Jesus for you."1Thess. 5:16-18

  8. #23
    Join Date
    Feb 2003
    Location
    Bangalore, India
    Posts
    1,354
    First about the question of taking the brush out of the loop, it is not difficult...provided the no of colors that you use is relatively small. Say in your case you have 3 colors...so create 3 brushes before hand. If the color is red, use the corresponding brush...similarly with green and blue.

    Now about the question of flood fill, take a look at MSDN...if you don't have better download or get it from someone. Look at the func ExtFloodFill(). You can fill it 2 ways either by specifying the boundary of the squares, if it is constant, or by specifying the color of the surface to be filled. You should note that the x,y that you specify must be a point inside the square. Since the grid must be present before flood filling you must draw grid before.

    About the clip region I recomend you to use MFC since you are using that...I may not be able to help because I use APIs rather the classes.

    Now I want to tell something about your question...your actual need and your question are way different...that's why those suggestions weren't of help to you. Since all are not in a position to download your code and check it, make sure that your question reflect your actual need. I still don't have a first hand idea of what you are trying to accomplish(you didn't answer to many of my questions regaring your project). So do all who answered in this thread
    Even if our suggestions didn't help, please post the answer once you find it. We took the effort to help you, please return it to others.

    * While posting code sections please use CODE tags
    * Please check the codeguru FAQ and do a little search to see if your question have been answered before.
    * Like a post, Rate The Post
    * I blog: Network programming, Bible

    I do all things thru CHRIST who strengthens me

  9. #24
    Join Date
    Jul 2003
    Location
    Singapore
    Posts
    1,822
    ok...tks ...i will answer your qeustions here...
    i am trying to make a graphic editor...
    the grids are supposed to be the enlarged squares..
    and the top view is supposed to be the preview window....
    as the squares are filled in the lower half of the app the upper half will be filled correspodingly..
    what i am thinking is
    1 square in the lower half = 1 pixel in the upper half of the app..

    total nomber of colors to be used are 3
    total number of squares to be used are 6400x4800...
    above is what the app is all about...
    its a very simple app...
    i have 2 views in it...the bottom view(with the squares)is caled the EditorView
    the top view(the preview area) is called the PeekView..
    now i have having a lot of probs in drawings...
    cant seem to get it working...
    tin the attached version o f the proj...i tried to activate the PeekView...and now the whole things doesnt work!!i am getting link errors...
    i hope you can pls help me..tks a lot for your time...really appreciate it..
    R. Thomas
    "Be anxious for nothing, but in everything by prayer and supplication, with thanksgiving, let your requests be made know to God; and the peace of God, which surpasses all understanding, will guard your hearts and minds through Christ Jesus."Philippians 4:6-7
    "Rejoice always, pray without ceasing, in everything give thanks; for this is the will of God in Christ Jesus for you."1Thess. 5:16-18

  10. #25
    Join Date
    Jul 2003
    Location
    Singapore
    Posts
    1,822
    regarding the ExtFloodFill...
    it says i need to to specify x and y...
    but what i have is a rect.....
    how can i put the rect in the ExtFloodFill..????
    is there a way???
    tks a lot..
    R. Thomas
    "Be anxious for nothing, but in everything by prayer and supplication, with thanksgiving, let your requests be made know to God; and the peace of God, which surpasses all understanding, will guard your hearts and minds through Christ Jesus."Philippians 4:6-7
    "Rejoice always, pray without ceasing, in everything give thanks; for this is the will of God in Christ Jesus for you."1Thess. 5:16-18

  11. #26
    Join Date
    Jul 2003
    Location
    Singapore
    Posts
    1,822
    Code:
    //
    	//Draw the squares
    	//
    	int nGridX = (pDoc->GetGridX()/10);
    	int nGridY = (pDoc->GetGridY()/10);
    	COLORREF color;
    	int x1;
    	int y1;
    	int x2;
    	int y2;
    	for(int i = 0 ; i < nGridY ; i ++) {
    		for(int j = 0 ; j < nGridX ; j ++) {
    			color = pDoc->GetSquareColor(i, j);
    			CBrush brush(color);
    			x1 = (j * 10) + 10;
    			y1 = (i * -10) - 10;
    			x2 = x1 + 10;
    			y2 = y1 - 10;
    			CRect rect(x1, y1, x2, y2);
    			pDC->FillRect(rect, &brush);
    		}
    	}
    regarding the above code...as you see the point where i get the color is inside the loop!!!
    so if thats the case, how can i declare the brush outside and chage its color fro within the loop???
    R. Thomas
    "Be anxious for nothing, but in everything by prayer and supplication, with thanksgiving, let your requests be made know to God; and the peace of God, which surpasses all understanding, will guard your hearts and minds through Christ Jesus."Philippians 4:6-7
    "Rejoice always, pray without ceasing, in everything give thanks; for this is the will of God in Christ Jesus for you."1Thess. 5:16-18

  12. #27
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150
    Try the following piece of code:
    Code:
    	//
    	//Draw the squares
    	//
    	int nGridX = pDoc->GetGridX();
    	int nGridY = pDoc->GetGridY();
    	COLORREF color;
    	int x1;
    	int y1;
    	y1 = -10;
    	for(int i = 0 ; i < nGridY ; ++i) {
    		y1 -= 10;
    		x1 = 0;
    		for(int j = 0 ; j < nGridX; ++j) {
    			color = pDoc->GetSquareColor(i, j);
    			x1 += 10;
    			dc->FillSolidRect(x1,y1,10,10,color);
    		}
    	}
    
    	//
    	// then draw the grid lines surrounding them
    	//
    	CRect clipBoxRect;
    	dc->GetClipBox(&clipBoxRect);
    	for(int x = 10 ; x <= (pDoc->GetGridX()+1)*10 ; x += 10) {
    		dc->MoveTo(x, -10);
    		dc->LineTo(x, -(pDoc->GetGridY()+1)*10);
    	}
    	
    	for(int y = -10 ; y >= -(pDoc->GetGridY()+1)*10; y -=10) {
    		dc->MoveTo(10, y);
    		dc->LineTo((pDoc->GetGridX()+1)*10, y);
    	}
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  13. #28
    Join Date
    Sep 2002
    Location
    14° 39'19.65"N / 121° 1'44.34"E
    Posts
    9,815
    OK, let's try to put an end to this drama. A few notes:
    • First of all, don't even think about using FloodFill. FloodFill is a function to fill an arbitrary shaped region. It uses a recursive algorithm which scans the DC line by line, and is highly inefficient compared to FillRect(). So stay with FillRect() for filling rectangles.
    • To get your updating and drawing to work, first remove the CMemDC stuff, as it obscures what is really happening. Without the offscreen drawing, you will see much better what is going on. Furthermore, you will note that you don't need offscreen drawing in order to remove flicker for your application.
    • Next, remove your implementation of OnEraseBkgnd (or call the base class implementation). You will definitely need to erase the background.
    • Now to your updating problem: Note that UpdateRect() expects the rect in device coordinates, you are passing logical coordinates. So create a CClientDC, set the mapping mode to MM_LOENGLISH and use LPToDP to convert the rect to device coords (the same way you did in OnLButtonDown).
    • Next, fix the bugs in CEditorDoc::SetSquareColor(). You are always returning the same rect, build it from x1, y1, x2, y2 instead. Note that you also have i and j swapped.
    • If you compile and run your app now, you will note that it works fine (albeit slow): Any field you click will be repainted without any flicker. However, it takes some time, as you are creating and deleting the brush and calling FillRect() for every field. Now let's go about optimizing:
    • Take into account the clipping region when drawing the rectangles. The easiest way to do it is to call pDC->IsRectVisible(). So inside your loop, for every rectangle you get, test whether it is actually visible (meaning: it is inside the clipping region) and create the brush and call FillRect() only if this is the case. You will now note that your app runs fine as before, only much faster. (To speed things further up, you should also draw the grid lines only if they fall into the clipping region).
    • Although this will probably already fit your needs, a more intelligent (read: efficient) way of optimizing the drawing will be to get the bounding rectangle of the visible region and determine which fields fall into that region. Then you could set the start and end values of your two nested loops to those values.
    • Just a last hint: The code to convert from a given square(i,j) to its actual coordinates (and back) is replicated in several places of your code. You should probably wrap that into two methods (e.g. GetSquareBounds / GetSquareFromPoint). This will be especially helpful if you want to change the square size and offset from 10 to something else in the future.

  14. #29
    Join Date
    Feb 2003
    Location
    Bangalore, India
    Posts
    1,354
    Originally posted by gstercken
    FloodFill is a function to fill an arbitrary shaped region. It uses a recursive algorithm which scans the DC line by line, and is highly inefficient compared to FillRect(). So stay with FillRect() for filling rectangles.
    You are right if FloodFill uses recursive algs...but I doubt since if filling a large area, the stack could easily run out. There are non-recursive floodfill alg available but writing a recursive floodfill is easy. I would appreciate if you could post or point to any material that confirms it.
    Even if our suggestions didn't help, please post the answer once you find it. We took the effort to help you, please return it to others.

    * While posting code sections please use CODE tags
    * Please check the codeguru FAQ and do a little search to see if your question have been answered before.
    * Like a post, Rate The Post
    * I blog: Network programming, Bible

    I do all things thru CHRIST who strengthens me

  15. #30
    Join Date
    Sep 2002
    Location
    14° 39'19.65"N / 121° 1'44.34"E
    Posts
    9,815
    Originally posted by Mathew Joy
    You are right if FloodFill uses recursive algs...but I doubt since if filling a large area, the stack could easily run out. There are non-recursive floodfill alg available but writing a recursive floodfill is easy. I would appreciate if you could post or point to any material that confirms it.
    The point is not whether the actual implementation of the Win32 ExtFloodFill() is recursive or not - any recursive algortihm can be rewritten without recursion. The point is that FloodFill (recursive or not) is extremely slow when compared to FillRect, so replacing FillRect by FloodFill is counter-productive as an optimizing measure.

Page 2 of 7 FirstFirst 12345 ... LastLast

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