-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdraw.h
113 lines (68 loc) · 1.79 KB
/
draw.h
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include <iostream>
#include <windows.h>
#include <cmath>
#include <conio.h>
#include <chrono>
#include <algorithm>
#define _USE_MATH_DEFINES
using namespace std;
constexpr unsigned int HEIGHT = 450, WIDTH = 900;
constexpr unsigned int dW = 8, dH = 16;
class pos{
public:
float
x,
y;
pos(float newX, float newY){
x = newX;
y = newY;
}
};
void moveCursor( short x,short y){
COORD coord = { x, y};
SetConsoleCursorPosition ( GetStdHandle ( STD_OUTPUT_HANDLE ), coord );
}
void drawPoint(char canvas[HEIGHT/dH][WIDTH/dW], pos Pos, char c ){
if(Pos.x < 0 || Pos.y < 0 || Pos.x > WIDTH || Pos.y > HEIGHT) return;
canvas[int(round(Pos.y/dH))][int(round(Pos.x/dW))] = c ;
}
void drawLine(char canvas[HEIGHT/dH][WIDTH/dW], pos Pos1, pos Pos2, char c){
// Pos1 is allways on the left
if(Pos2.x < Pos1.x)
swap(Pos1,Pos2);
bool reversed = false;
float
X1 = Pos1.x,
Y1 = Pos1.y,
X2 = Pos2.x,
Y2 = Pos2.y;
if(abs(Pos1.x - Pos2.x) < abs(Pos1.y - Pos2.y)){
reversed = true;
X1 = Pos1.y;
Y1 = Pos1.x;
X2 = Pos2.y;
Y2 = Pos2.x;
}
pair<float,float> sorted = {min(X1,X2),max(X1,X2)};
float
B1 = X1-X2,
B2 = Y1-Y2;
if(B2 == 0){
if(!reversed){
for (float i = sorted.first; i < sorted.second;i += dW ) drawPoint(canvas,pos(i,Y1),c);
}else{
for (float i = sorted.first; i < sorted.second;i += dH ) drawPoint(canvas,pos(Y1,i),c);
}
}
float
B = B2/B1,
A = -X1*B+Y1;
if(!reversed){
for (float i = sorted.first; i < sorted.second;i += dW ) drawPoint(canvas,pos(i,( i*B + A )),c);
}else{
for (float i = sorted.first; i < sorted.second;i += dH ) drawPoint(canvas,pos(( i*B + A ),i),c);
}
// draw pivit points
drawPoint(canvas,{Pos1.x,Pos1.y},'@');
drawPoint(canvas,{Pos2.x,Pos2.y},'@');
}