My own line function, ALMOST works

This is a discussion on My own line function, ALMOST works within the C++ Programming forums, part of the General Programming Boards category; void draw(float x1, float x2, float y1, float y2) //Numbers are passed in from Main() {{ system("CLS"); float slope = ...

  1. #1
    Shadow12345
    Guest

    My own line function, ALMOST works

    void draw(float x1, float x2, float y1, float y2) //Numbers are passed in from Main()
    {{
    system("CLS");
    float slope = (y1 - y2) / (x1 - x2); //Finds the slope
    cout << "The slope is " << slope;


    gotoxy(x1, y2); //This part doesn't work if you use gotoxy(x1, y1)
    cout << ".";
    gotoxy(x2, y1); //This part doesn't work if you use gotoxy(x2, y2)
    cout << ".";
    float newx = x1;
    float newy = y1;

    while(newx != x2) { //This is the most important part of the function
    gotoxy(newx, newy); //While the newx (which is x1) is not equal to x2 (the other end of the line segment)
    cout << "."; //add the slope to the newx and newy values and draw the new dot
    newx = newx + slope;
    newy = newy + slope;
    }

    }}

    This function does not work and I am not sure why, if someone could look at it and see if there are any errors I would be very happy. I am not sure what is wrong.

    When I enter (1,2), (5,6) for the points it draws them in the correct place, but the line seems to be going the wrong way. It seems when I enter any other points it just keeps on drawing dots for all eternity.
    Last edited by Shadow12345; 05-18-2002 at 04:13 PM.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    In you code, I changed these
    Code:
    gotoxy(x1, y1);
    cout << ".";
    gotoxy(x2, y2);
    And call the function with
    >draw(1, 5, 2, 6);
    And it gave me this:
    Code:
    The slope is 1
    .
     .
      .
       .
    That's starting at 1 in, 2 down..... connecting to 5 in, 6 down. Is that what you're after?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Shadow12345
    Guest
    Yes that does look like what I have in mind

    Thanks for replying, I wasn't sure if anyone would even both with my code.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >Thanks for replying
    No problem! Does your version do what you want now?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  2. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  3. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 08:10 AM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. Replies: 3
    Last Post: 03-04-2005, 01:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21