How do I call something in to a for loop?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • slenish
    Contributor
    • Feb 2010
    • 283

    How do I call something in to a for loop?

    Hi im new to C++ and im trying to call a function in to a for loop.


    Code:
    [B]void DrawLine[/B](int x1, int y1, int x2, int y2, unsigned int color)
    {
    
    int dx, dy;                         // dy / dx is the slope
        int x, y;                           // loop and point variables
    
        // calculate changes in y and x between the points
        dy = y2 - y1;
        dx = x2 - x1;
    
        if (Abs(dy) > Abs(dx)) {
            // since there is a greater change in y than x we must
            // loop in y, calculate x and draw
            for (y=y1; y != y2; y += Sign(dy)) {
                x = x1 + (y - y1) * dx / dy;
                PGraphics->AddPoint(x, y, color);
            }
        }
        else {
            // since there is a greater (or equal) change in x than y we must
            // loop in x, calculate y and draw
            for (x=x1; x != x2; x += Sign(dx)) {
                y = y1 + (x - x1) * dy / dx;
                PGraphics->AddPoint(x, y, color);
            }
        }
    
        // draw the last pixel
        PGraphics->AddPoint(x2, y2, color);
    
        // draw the points
        PGraphics->Draw();
    }
    
    
    void DrawStuff() {
        COLORREF green = RGB(0, 255, 0);     // green color to draw with
        COLORREF purple = RGB(255, 0, 255);  // purple color to draw with
    
        char str[32];                       // string to store user input
        int h, k;                           // parabola vertex
        double a;                           // parabola constant - might be a decimal
        int x, y;                           // loop and point variables
        int xPrev, yPrev;                   // previous point for drawng line segments
        int ymin, ymax;                     // limits for y loop
        RECT rect;                          // rectangle for the output window
    
        // get the user input from the edit boxes and 
        // convert string input to integer
        GetDlgItemText(HDialog, IDC_EDIT_VERTEXX, str, 32);
        h = atoi(str);
        GetDlgItemText(HDialog, IDC_EDIT_VERTEXY, str, 32);
        k = atoi(str);
        GetDlgItemText(HDialog, IDC_EDIT_CONSTA, str, 32);
        a = atof(str);                              // use atof to allow user to enter a decimal
    
        // get the rect for this window
        GetClientRect(HOutput, &rect);
    
        // use the rectangle info to set up y loop limits
        ymin = -(rect.bottom - rect.top) / 2;
        ymax =  (rect.bottom - rect.top) / 2;
    
        // clear the scene and add an axis
        PGraphics->ClearScene(RGB(0, 0, 0));
        PGraphics->AddAxis(RGB(150, 150, 150), 10);
    
        yPrev = ymin;
        xPrev = (int)( a * (yPrev-k) * (yPrev-k) ) + h;
    
        // loop in y, calculate x and draw
       [B] for (y = ymin; y <= ymax; y++) {
            x = (int)( a * (y-k) * (y-k) ) + h;
            PGraphics->AddPoint(x, y, green); [/B]
        }
    
        // draw the points
        PGraphics->Draw();
    }
    What im trying to do is take the DrawLine function (that I put in bold) and have it referenced right before the for loop in the DrawStuff (that i put in bold) This way the DrawStuff will know to use DrawLine to draw more points in a line.

    Thanks in advance!
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    The only wat that DrawStuff can know to use DrawLine is for you to a) call Drawline inside DrawStuff or b) have a function pointer argument in DrawStuff that contains the adress of the correct DrawLine function.


    Code:
    void DrawStuff()
    {
         DrawLine(etc...);
         for (etc...
         {
             ///or  DrawLine(etc...);
         }
    }

    Comment

    • whodgson
      Contributor
      • Jan 2007
      • 542

      #3
      Do you mean to spell ymin like ymi.n

      Comment

      • slenish
        Contributor
        • Feb 2010
        • 283

        #4
        Hi weaknessforcats ,
        Appreciate you helping me out with this I was wondering if you could give me a little more detail if your example or possibly give me a couple websites with some examples I could look at.

        Thanks for the help!


        Whodgson,
        its supposed to be ymin. :) It refers to the min value of y

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          Originally posted by slenish
          Hi weaknessforcats ,
          Appreciate you helping me out with this I was wondering if you could give me a little more detail if your example or possibly give me a couple websites with some examples I could look at.
          It may be you are no clear on how a function call works.

          Your program excutes statements in sequence. A B C D. If you have steps that are used in several places, you code A XXX B XXX C XXX D XXX. Those XXX instrcutions are repeated as many times a needed.

          A function call is a way of coding XXX only once. These instructions are put together using a name an argument list and a pair of braces:

          Code:
          void MyFunction()
          {
                XXX
          
          }
          The code now looks like:

          A MyFunction() B MyFunction() C MyFunction() D MyFunction().

          The last thing is when you use a variable as an argument to a function, the compiler makes a copy of it. The copy is the argument variable of the function being called. This is done to protect the original variable.

          You can find all sorts of information about this from just about any book on C.

          Comment

          Working...