Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Array Question...

Array Question...

Scheduled Pinned Locked Moved C / C++ / MFC
databasegraphicsdata-structuresquestion
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    CDuddley
    wrote on last edited by
    #1

    I'm trying to write an undo feature for a drawing program. I have an array of CPoint's called ShapePts[100]. I made another array of CPoint's Undo[100]. When the program deletes a point it finds the closest point to the mouse. So, I was thinking just store the point in another array before it gets deleted. /* This is the MainFrame.h declaration */ CPoint Undo[100]; int nUndo;

    void CMainFrame::OnLButtonDown (UINT nFlags, CPoint MousePt) { int ShortDist; if (nFlags & MK_SHIFT) { // Shift key down. So delete closest point. if (nPts > 0) // - only if have one { // Step: Find point closest to MousePt Pos = ClosestPoint (MousePt,ShapePts,nPts, ShortDist); <-- returns int Undo[nUndo] = ShapePts[nPts]; // nPts and nUndo were initialized to // zero in MainFrame contructor // Step: Shift points down one array component index for (int i = Pos + 1; i < nPts; i++) { ShapePts[i-1] = ShapePts[i]; } nPts--; // adjust count } // end if } else { if (nPts >= 99) // array full return; // do nothing else { ShapePts[nPts] = MousePt; nPts++; CString s; s.Format("Last pt:(%d, %d)",MousePt.x, MousePt.y); myStatusBar.SetText (s,1,SBT_NOBORDERS); } } Invalidate(); }

    void CMainFrame::OnRButtonDown (UINT nFlags, CPoint MousePt) { CPoint ScreenPt; int ShortestDist, i; // Step 1: Find position of closest pt in array Pos = ClosestPoint (MousePt, ShapePts, nPts, ShortestDist); if(nFlags & MK_SHIFT) { ShapePts[nPts] = Undo[nUndo]; nPts++; for(i = 0; i < nUndo; i++) { Undo[i-1] = Undo[i]; } nUndo--; } else { SetCapture(); // Step 2: Move the cursor to that position ScreenPt = ShapePts[Pos]; ClientToScreen (&ScreenPt); SetCursorPos (ScreenPt.x, ScreenPt.y); } }

    Where am I going wrong. If you need more info please let me know. -CDudd

    C T 2 Replies Last reply
    0
    • C CDuddley

      I'm trying to write an undo feature for a drawing program. I have an array of CPoint's called ShapePts[100]. I made another array of CPoint's Undo[100]. When the program deletes a point it finds the closest point to the mouse. So, I was thinking just store the point in another array before it gets deleted. /* This is the MainFrame.h declaration */ CPoint Undo[100]; int nUndo;

      void CMainFrame::OnLButtonDown (UINT nFlags, CPoint MousePt) { int ShortDist; if (nFlags & MK_SHIFT) { // Shift key down. So delete closest point. if (nPts > 0) // - only if have one { // Step: Find point closest to MousePt Pos = ClosestPoint (MousePt,ShapePts,nPts, ShortDist); <-- returns int Undo[nUndo] = ShapePts[nPts]; // nPts and nUndo were initialized to // zero in MainFrame contructor // Step: Shift points down one array component index for (int i = Pos + 1; i < nPts; i++) { ShapePts[i-1] = ShapePts[i]; } nPts--; // adjust count } // end if } else { if (nPts >= 99) // array full return; // do nothing else { ShapePts[nPts] = MousePt; nPts++; CString s; s.Format("Last pt:(%d, %d)",MousePt.x, MousePt.y); myStatusBar.SetText (s,1,SBT_NOBORDERS); } } Invalidate(); }

      void CMainFrame::OnRButtonDown (UINT nFlags, CPoint MousePt) { CPoint ScreenPt; int ShortestDist, i; // Step 1: Find position of closest pt in array Pos = ClosestPoint (MousePt, ShapePts, nPts, ShortestDist); if(nFlags & MK_SHIFT) { ShapePts[nPts] = Undo[nUndo]; nPts++; for(i = 0; i < nUndo; i++) { Undo[i-1] = Undo[i]; } nUndo--; } else { SetCapture(); // Step 2: Move the cursor to that position ScreenPt = ShapePts[Pos]; ClientToScreen (&ScreenPt); SetCursorPos (ScreenPt.x, ScreenPt.y); } }

      Where am I going wrong. If you need more info please let me know. -CDudd

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      I don't get why your undo is a collection of points, but why not use a deque, then you can upll undos from one end, and drop them from the other as you push them in when you reach the size you want. Christian The tragedy of cyberspace - that so much can travel so far, and yet mean so little. "I'm thinking of getting married for companionship and so I have someone to cook and clean." - Martin Marvinski, 6/3/2002

      1 Reply Last reply
      0
      • C CDuddley

        I'm trying to write an undo feature for a drawing program. I have an array of CPoint's called ShapePts[100]. I made another array of CPoint's Undo[100]. When the program deletes a point it finds the closest point to the mouse. So, I was thinking just store the point in another array before it gets deleted. /* This is the MainFrame.h declaration */ CPoint Undo[100]; int nUndo;

        void CMainFrame::OnLButtonDown (UINT nFlags, CPoint MousePt) { int ShortDist; if (nFlags & MK_SHIFT) { // Shift key down. So delete closest point. if (nPts > 0) // - only if have one { // Step: Find point closest to MousePt Pos = ClosestPoint (MousePt,ShapePts,nPts, ShortDist); <-- returns int Undo[nUndo] = ShapePts[nPts]; // nPts and nUndo were initialized to // zero in MainFrame contructor // Step: Shift points down one array component index for (int i = Pos + 1; i < nPts; i++) { ShapePts[i-1] = ShapePts[i]; } nPts--; // adjust count } // end if } else { if (nPts >= 99) // array full return; // do nothing else { ShapePts[nPts] = MousePt; nPts++; CString s; s.Format("Last pt:(%d, %d)",MousePt.x, MousePt.y); myStatusBar.SetText (s,1,SBT_NOBORDERS); } } Invalidate(); }

        void CMainFrame::OnRButtonDown (UINT nFlags, CPoint MousePt) { CPoint ScreenPt; int ShortestDist, i; // Step 1: Find position of closest pt in array Pos = ClosestPoint (MousePt, ShapePts, nPts, ShortestDist); if(nFlags & MK_SHIFT) { ShapePts[nPts] = Undo[nUndo]; nPts++; for(i = 0; i < nUndo; i++) { Undo[i-1] = Undo[i]; } nUndo--; } else { SetCapture(); // Step 2: Move the cursor to that position ScreenPt = ShapePts[Pos]; ClientToScreen (&ScreenPt); SetCursorPos (ScreenPt.x, ScreenPt.y); } }

        Where am I going wrong. If you need more info please let me know. -CDudd

        T Offline
        T Offline
        Tim Smith
        wrote on last edited by
        #3

        There isn't a point at Undo [nUndo]. All the points exist between 0 and nUndo - 1. You can correct this problem by decrementing nUndo prior to the Undo [nUndo] line. Also, since you are getting the top element off the undo stack, there is no need for that block of code shifting the undo stack down. After you get it working, look into std::deqeu. It would really simplify the code. Tim Smith I know what you're thinking punk, you're thinking did he spell check this document? Well, to tell you the truth I kinda forgot myself in all this excitement. But being this here's CodeProject, the most powerful forums in the world and would blow your head clean off, you've got to ask yourself one question, Do I feel lucky? Well do ya punk?

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups