# Thread: My own line function, ALMOST works

1. ## 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.

2. 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?

3. 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. >Thanks for replying
No problem! Does your version do what you want now?

Popular pages Recent additions