Array Question...
-
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
-
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
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
-
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
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?