Line drawing function fails strangely...

Hello. I followed a tutorial that explained how to draw a line in C++, my version of the result code is this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#include <string

void printxy(unsigned int x, unsigned int y, std::string str = "*") {
	std::cout << "\x1b[" << y << ";" << x << "H" << str << std::flush;
}

void DrawLine(float x1, float y1, float x2, float y2) {
	float xDiff = x2 - x1;
	float yDiff = y2 - y1;

	if (xDiff == 0.0f && yDiff == 0.0f) {
		printxy(x1, y1);
		return;
	}

	if (std::abs(xDiff) > std::abs(yDiff)) {
		float xMin = std::min(x1, x2), xMax = std::max(x1, x2);

		float slope = yDiff / xDiff;
		for (float x = xMin; x <= xMax; x += 1.0f) {
			float y = y1 + ((x - x1) * slope);

			printxy(x, y);
		}
	}
	else {
		float yMin = std::min(y1, y2), yMax = std::max(y1, y2);

		float slope = xDiff / yDiff;
		for (float y = yMin; y <= yMax; y += 1.0f) {
			float x = x1 + ((y - y1) * slope);

			printxy(x, y);
		}
	}
}

int main(int argc, char **argv) {
	DrawLine(0, 0, 10, 2); // std::abs(xDiff) > std::abs(yDiff)

    return 0;
}


Output:

*********
         *


This is not supposed to happen, is it? I would think that the line would look more like the following:

***
   ***
       **


Is the problem my code, my idea of the output, or just that I am using a console for what it isn't meant for? Please request clarification if necessary.
Last edited on
its probably ok. normally slope is y/x not x/y but meh. that is just a coordinate system.
you asked for a line from 0,0 to 10,2 so it goes way over (10) and up just a little (2) ...
if you want a 45 degree like you think it should be try 0,0 and say 10,10
I'm not familiar with the control code output, but if we just look at the coordinates,
0, 0
1, 0
2, 0
3, 0
4, 0
5, 1
6, 1
7, 1
8, 1
9, 1
10, 2

I see three y-levels, not just two.

But if we connect this to the previous thread, it seems what you are really looking for is for the numbers to round to integers, as opposed to truncating into integers.

If you want this behavior, I would actually change it so your functions that print/render the point take in floating-point arguments, and then round the result to an integer.
e.g. (without the control sequences):
1
2
3
void printxy(float x, float y, std::string str = "*") {
	std::cout << std::round(x) << ", " << std::round(y) << '\n';
}

0, 0
1, 0
2, 0
3, 1
4, 1
5, 1
6, 1
7, 1
8, 2
9, 2
10, 2
Last edited on
I did not know that rounding was different from truncating. Thank you all.
I'm not sure why, but std::round cannot be resolved. (iostream and cmath included.)
What IDE or compiler version are you using? C++11 is a decade old at this point. I suggest an upgrade.

But if you can't upgrade, and you're using GCC, add -std=c++11 or -std=c++0x to your flags.
I am using Eclipse IDE and MinGW compiler I'm pretty sure. I added -std=c++17, I generally use C++17. But it still didn't work.
What version is your MinGW? (type g++ --version). What is the exact error message? Also, try std::roundf instead of std::round. It shouldn't really matter.. but worth a shot.
Last edited on
G++ 9.2.0. Ah, it seems that after a brief project refresh it was resolved. Thank you! :D
Topic archived. No new replies allowed.