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. Ratio result placing a square not working - when resizing window

Ratio result placing a square not working - when resizing window

Scheduled Pinned Locked Moved C / C++ / MFC
5 Posts 4 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.
  • S Offline
    S Offline
    simon alec smith
    wrote on last edited by
    #1

    Hi, I am very confused to why this isn't working. My starting window width size is 756 I got this result by declaring: CRect Rect; GetClientRect(&Rect); Rect.Width(); I divide the 756 / 2 to plae the triangle in the centre of the window. I have set up a message handler for the left and right arrow key press, which moves the triagle by 10. if the user resizes the the window, the window should display in proportion, by the statement. widthRatio = Rect.Width() / 756; then * the x posistion by the widthRatio I have tried declaring the variables 'float, double, and int I can not seem to find a reason why this shouldn't work the posistion remains the same if I expand, and snaps to the left hand of the screen if the size has gone smaller does anybody know whats going on cheers Simon

    D I M 3 Replies Last reply
    0
    • S simon alec smith

      Hi, I am very confused to why this isn't working. My starting window width size is 756 I got this result by declaring: CRect Rect; GetClientRect(&Rect); Rect.Width(); I divide the 756 / 2 to plae the triangle in the centre of the window. I have set up a message handler for the left and right arrow key press, which moves the triagle by 10. if the user resizes the the window, the window should display in proportion, by the statement. widthRatio = Rect.Width() / 756; then * the x posistion by the widthRatio I have tried declaring the variables 'float, double, and int I can not seem to find a reason why this shouldn't work the posistion remains the same if I expand, and snaps to the left hand of the screen if the size has gone smaller does anybody know whats going on cheers Simon

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      simon alec smith wrote:

      I can not seem to find a reason why this shouldn't work

      What doesn't work? What value does widthRatio contain? Since both the numerator and the denominator are integers, it will likely contain 0. Try dividing my 756.0 instead.

      "Love people and use things, not love things and use people." - Unknown

      "The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch

      S 1 Reply Last reply
      0
      • S simon alec smith

        Hi, I am very confused to why this isn't working. My starting window width size is 756 I got this result by declaring: CRect Rect; GetClientRect(&Rect); Rect.Width(); I divide the 756 / 2 to plae the triangle in the centre of the window. I have set up a message handler for the left and right arrow key press, which moves the triagle by 10. if the user resizes the the window, the window should display in proportion, by the statement. widthRatio = Rect.Width() / 756; then * the x posistion by the widthRatio I have tried declaring the variables 'float, double, and int I can not seem to find a reason why this shouldn't work the posistion remains the same if I expand, and snaps to the left hand of the screen if the size has gone smaller does anybody know whats going on cheers Simon

        I Offline
        I Offline
        Iain Clarke Warrior Programmer
        wrote on last edited by
        #3

        Modified for the 2nd time... Looks like I missed the point with your question - dividing an integer with another integer, especially a larger one is going to give you BAD results. - Iain. Where are you doing these calculations?

        class CMyView : public CView
        {
        ...
        int m_nHorzOffset;
        ...
        };

        void CMyView::OnDraw(CDC* pDC)
        {
        CRect rc;
        GetClientRect (&rc);
        // Make a point in the middle
        CPoint ptCentre = rc.CenterPoint ();

        ptCentre.x = rc.Width () / 4; // make it 1/4 the way across if we like...

        // draw a vertical centre line
        pDC->MoveTo (ptCentre.x, rc.top);
        pDC->LineTo (ptCentre.x, rc.bottom);

        // draw a triangle, offset by m_nHorzOffset
        pDC->MoveTo (ptCentre.x + m_nHorzOffset - 100, ptCentre.y + 50);
        pDC->LineTo (ptCentre.x + m_nHorzOffset + 100, ptCentre.y + 50);
        pDC->LineTo (ptCentre.x + m_nHorzOffset, ptCentre.y - 100);
        pDC->LineTo (ptCentre.x + m_nHorzOffset - 100, ptCentre.y + 50);
        }
        ...

        void CMyView::OnLeftABit ()
        {
        m_nHorzOffset -= 10;
        Invalidate ();
        }

        I'm assuming you have the left arrow mapped via an accelerator to (eg) IDC_LEFTABIT, and have a command handler called OnLeftABit in your view class. Hopefully that should work. Iain. Modified: Added centre line, some comments, and x=width/4 as an example.

        modified on Thursday, September 18, 2008 12:07 PM

        1 Reply Last reply
        0
        • D David Crow

          simon alec smith wrote:

          I can not seem to find a reason why this shouldn't work

          What doesn't work? What value does widthRatio contain? Since both the numerator and the denominator are integers, it will likely contain 0. Try dividing my 756.0 instead.

          "Love people and use things, not love things and use people." - Unknown

          "The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch

          S Offline
          S Offline
          simon alec smith
          wrote on last edited by
          #4

          Thank you very much, I have tried 756.0 and it works it would of taken me a hundred years to work that one out cheers

          1 Reply Last reply
          0
          • S simon alec smith

            Hi, I am very confused to why this isn't working. My starting window width size is 756 I got this result by declaring: CRect Rect; GetClientRect(&Rect); Rect.Width(); I divide the 756 / 2 to plae the triangle in the centre of the window. I have set up a message handler for the left and right arrow key press, which moves the triagle by 10. if the user resizes the the window, the window should display in proportion, by the statement. widthRatio = Rect.Width() / 756; then * the x posistion by the widthRatio I have tried declaring the variables 'float, double, and int I can not seem to find a reason why this shouldn't work the posistion remains the same if I expand, and snaps to the left hand of the screen if the size has gone smaller does anybody know whats going on cheers Simon

            M Offline
            M Offline
            Mark Salsbery
            wrote on last edited by
            #5

            In addition to DavidCrow's reply...

            simon alec smith wrote:

            widthRatio = Rect.Width() / 756; then * the x posistion by the widthRatio

            When working with integers like this, you can often rearrange the calculation to avoid getting the 0. Do the multiply first, then the divide. This way you can avoid floating point types which can give better performance in certain situations. Of course, if you need the ratio variable in other places, then you'll need to make sure you do a floating point calculation as DavidCrow showed. Mark

            Mark Salsbery Microsoft MVP - Visual C++ :java:

            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