-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathView.cpp
More file actions
148 lines (129 loc) · 3.52 KB
/
Copy pathView.cpp
File metadata and controls
148 lines (129 loc) · 3.52 KB
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
#include "View.h"
View::View() {
size = 25;
scale = 2;
map.resize(size, std::vector<std::string>(size, "."));
x = -10;
y = -10;
}
View::View(const View &v) {
size = v.size;
scale = v.scale;
map.clear();
map.resize(size, std::vector<std::string>(size, "."));
map = v.map;
x = v.x;
y = v.y;
}
View &View::operator=(const View &v) {
if(this == &v) {
return *this;
}
size = v.size;
scale = v.scale;
map.clear();
map.resize(size, std::vector<std::string>(size, "."));
x = v.x;
y = v.y;
return *this;
}
void View::default_size() {
size = 25;
scale = 2;
x = -10;
y = -10;
}
void View::new_size(int s) {
if(s < 7) {
std::cout << "ERROR: New map size is too small." << std::endl;
return;
}
if(s > 30) {
std::cout << "ERROR: New map size is too big." << std::endl;
return;
}
size = s;
map.resize(size, std::vector<std::string>(size, "."));
}
void View::new_zoom(double z) {
if(z < 0) {
std::cout << "ERROR: New map scale must be positive." << std::endl;
return;
}
scale = z;
}
void View::pan(double xx, double yy) {
this->x = xx;
this->y = yy;
}
void View::show() {
std::cout << "Display size: " << size << ", scale: " << std::fixed << std::setprecision(2) << scale;
std::cout << ", origin: " << std::setprecision(2) << "(" << x << ", " << y << ")" << std::endl;
std::vector<std::vector<std::string>> matrix = rotate_matrix();
double axis = y + (scale * (size - 1));
int tmp = size - 1;
for(int i = 0; i < size; i++) {
for(int j = 0; j < size; j++) {
if(j != 0) {
std::cout << " ";
}
else if(tmp%3 == 0){
std::cout << std::setw(3) << std::left << (int)axis;
}
else {
std::cout << std::setw(3) << std::left << " ";
}
std::cout << std::left << std::setw(2) << matrix[i][j];
}
std::cout << std::endl;
tmp--;
axis -= scale;
}
std::cout << std::setw(3) << std::left << " ";
axis = x;
for(int i = 0; i < size; i++) {
if(i%3 == 0) {
std::cout << std::setw(3) << std::left << (int)axis;
}
else {
std::cout << std::setw(3) << std::left << " ";
}
axis += scale;
}
std::cout << std::endl << std::endl;
}
void View::insert_obj(double worldX, double worldY, std::string name) {
// Convert world coordinates to view grid indices, relative to the
// current origin (x, y) and scale. map is indexed [xIndex][yIndex];
// rotate_matrix() turns that into a north-up display in show().
int xIndex = static_cast<int>((worldX - x) / scale);
int yIndex = static_cast<int>((worldY - y) / scale);
if (xIndex >= 0 && xIndex < size && yIndex >= 0 && yIndex < size) {
map[xIndex][yIndex] = std::move(name);
}
}
void View::clear() {
map.clear();
map.resize(size, std::vector<std::string>(size, "."));
}
std::vector<std::vector<std::string>> View::rotate_matrix() {
std::vector<std::vector<std::string>> tmp(size, std::vector<std::string>(size, "."));
for(int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
tmp[size - j - 1][i] = map[i][j];
}
}
return tmp;
}
int View::getSize() const {
return size;
}
double View::getScale() const {
return scale;
}
double View::getX() const {
return x;
}
double View::getY() const {
return y;
}