-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
219 lines (188 loc) · 4.53 KB
/
main.cpp
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/**
* @file main.cpp
* @author BugiBugi205
* @date 2022-04-12
*
* @copyright Copyright (c) 2022
*/
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
const uint8_t BG_CHAR = '-'; //
const uint8_t LINE_CHAR = '#'; //
void BresenhamLine(char**, const int, const int, const int, const int);
int main()
{
uint16_t rows = 25, cols = 30, points;
uint16_t A1=24,A2=0;
uint16_t B1=24,B2=29;
char **arr;
int **points_arr;
int colsDigitsNum;
int rowsDigitsNum;
cout<<"How many points: ";
cin>>points;
points_arr = new int *[2];
points_arr[1] = new int [points]; //x
points_arr[0] = new int [points]; //y
for(int i = 0; i < points; i++)
{
cout<<"x"<<i+1<<": ";
cin>>points_arr[1][i];
if(points_arr[1][i]>cols) cols = points_arr[1][i];
cout<<"y"<<i+1<<": ";
cin>>points_arr[0][i];
if(points_arr[0][i]>rows) rows = points_arr[0][i];
}
cout<<endl;
rows+=5;
cols+=5;
colsDigitsNum = to_string(cols).length();
rowsDigitsNum = to_string(rows).length();
arr = new char *[rows];
for(int i=0; i<rows; i++)
{
arr[i] = new char [cols];
}
//fills background array with BG_CHAR
for(int i=0; i<rows; i++)
{
for(int x=0; x<cols; x++)
{
arr[i][x] = BG_CHAR;
}
}
//creates lines between points
for(int i = 0; i < points; i++)
{
if(i+1>=points)
{
BresenhamLine(arr,points_arr[0][i],points_arr[1][i],points_arr[0][0],points_arr[1][0]);
break;
}
BresenhamLine(arr,points_arr[0][i],points_arr[1][i],points_arr[0][i+1],points_arr[1][i+1]);
}
//line of x
for (int i = colsDigitsNum-1; i > 0; i--)
{
int decNum = 1;
int xBlankCount = (pow(10,i)*2)+(rowsDigitsNum+1);
//blank spaces
for (int x = 0; x < xBlankCount; x++) cout<<" ";
//numerals
for (int x = 0; x <= (cols*2-(xBlankCount-rowsDigitsNum))/2; x++)
{
if(x%(int)pow(10,i)==0 && x>0) decNum++;
if(decNum==10) decNum=0;
cout<<decNum<<" ";
}
cout<<endl;
}
for (int i = 0; i < rowsDigitsNum+1; i++) cout<<" ";
for (int i = 0; i < cols; i++) cout<<i%10<<" "; //last line
cout<<endl;
//draws y idx and background array with already connected points
for(int i=0; i<rows; i++)
{
//y indexes
for (int y = 0; y < abs((int)(to_string(i).length()-rowsDigitsNum)); y++) cout<<" ";
cout<<i<<" ";
//array
for(int x=0; x<cols; x++)
{
cout<<arr[i][x]<<" ";
}
cout<<""<<endl;
}
system("pause");
//cleans memory from dynamic elements
for(int i=0; i<cols; i++)
{
delete [] arr[i];
}
delete [] points_arr;
delete [] points_arr[0];
delete [] points_arr[1];
delete [] arr;
return 0;
}
//algorithm source https://pl.wikipedia.org/wiki/Algorytm_Bresenhama#Implementacja
void BresenhamLine(char **arr, const int x1, const int y1, const int x2, const int y2)
{
// auxiliary variables
int d, dx, dy, ai, bi, xi, yi;
int x = x1, y = y1;
// determining the direction of drawing
if (x1 < x2)
{
xi = 1;
dx = x2 - x1;
}
else
{
xi = -1;
dx = x1 - x2;
}
// determining the direction of drawing
if (y1 < y2)
{
yi = 1;
dy = y2 - y1;
}
else
{
yi = -1;
dy = y1 - y2;
}
// first pixel
arr[x][y] = LINE_CHAR;
// leading axis OX
if (dx > dy)
{
ai = (dy - dx) * 2;
bi = dy * 2;
d = bi - dx;
// loop after consecutive x
while (x != x2)
{
// ratio test
if (d >= 0)
{
x += xi;
y += yi;
d += ai;
}
else
{
d += bi;
x += xi;
}
arr[x][y] = LINE_CHAR;
}
}
// leading axis OY
else
{
ai = ( dx - dy ) * 2;
bi = dx * 2;
d = bi - dy;
// loop after consecutive y
while (y != y2)
{
// ratio test
if (d >= 0)
{
x += xi;
y += yi;
d += ai;
}
else
{
d += bi;
y += yi;
}
arr[x][y] = LINE_CHAR;
}
}
}