From: Guillermo Rodriguez Garcia Date: Wed, 9 Feb 2000 20:22:57 +0000 (+0000) Subject: Added automatic connection of drawn points X-Git-Url: https://git.saurik.com/wxWidgets.git/commitdiff_plain/7989fb3707d6b6cbc278d33aaff0b5ac2e8a996c?ds=inline Added automatic connection of drawn points git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@5932 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- diff --git a/demos/life/life.cpp b/demos/life/life.cpp index b65451f8b5..891eacd0f0 100644 --- a/demos/life/life.cpp +++ b/demos/life/life.cpp @@ -625,24 +625,79 @@ void LifeCanvas::OnMouse(wxMouseEvent& event) if (!event.LeftIsDown()) { m_status = MOUSE_NOACTION; + return; } - else - { - bool alive = m_life->IsAlive(i, j); - // if just pressed, update status - if (m_status == MOUSE_NOACTION) - m_status = (alive? MOUSE_ERASING : MOUSE_DRAWING); - - // toggle cell and refresh if needed - if (((m_status == MOUSE_ERASING) && alive) || - ((m_status == MOUSE_DRAWING) && !alive)) + // button just pressed? + if (m_status == MOUSE_NOACTION) + { + // yes, update status and toggle this cell + m_status = (m_life->IsAlive(i, j)? MOUSE_ERASING : MOUSE_DRAWING); + + m_mi = i; + m_mj = j; + m_life->SetCell(i, j, m_status == MOUSE_DRAWING); + DrawCell(i, j, m_status == MOUSE_DRAWING); + } + else if ((m_mi != i) || (m_mj != j)) + { + // draw a line of cells using Bresenham's algorithm + wxInt32 d, ii, jj, di, ai, si, dj, aj, sj; + di = i - m_mi; + ai = abs(di) << 1; + si = (di < 0)? -1 : 1; + dj = j - m_mj; + aj = abs(dj) << 1; + sj = (dj < 0)? -1 : 1; + + ii = m_mi; + jj = m_mj; + + if (ai > aj) + { + // iterate over i + d = aj - (ai >> 1); + + while (ii != i) + { + m_life->SetCell(ii, jj, m_status == MOUSE_DRAWING); + DrawCell(ii, jj, m_status == MOUSE_DRAWING); + if (d >= 0) + { + jj += sj; + d -= ai; + } + ii += si; + d += aj; + } + } + else { - m_life->SetCell(i, j, !alive); - DrawCell(i, j, !alive); - GET_FRAME()->UpdateInfoText(); + // iterate over j + d = ai - (aj >> 1); + + while (jj != j) + { + m_life->SetCell(ii, jj, m_status == MOUSE_DRAWING); + DrawCell(ii, jj, m_status == MOUSE_DRAWING); + if (d >= 0) + { + ii += si; + d -= aj; + } + jj += sj; + d += ai; + } } + + // last cell + m_life->SetCell(ii, jj, m_status == MOUSE_DRAWING); + DrawCell(ii, jj, m_status == MOUSE_DRAWING); + m_mi = ii; + m_mj = jj; } + + GET_FRAME()->UpdateInfoText(); } void LifeCanvas::OnSize(wxSizeEvent& event) diff --git a/demos/life/life.h b/demos/life/life.h index 4f87eee981..74bc31138f 100644 --- a/demos/life/life.h +++ b/demos/life/life.h @@ -91,6 +91,7 @@ private: wxInt32 m_viewportH; // number of visible cells (h) int m_thumbX; // horiz. scrollbar thumb position int m_thumbY; // vert. scrollbar thumb position + wxInt32 m_mi, m_mj; // last mouse position }; // --------------------------------------------------------------------------