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;
}
|