forked from buffolu/project_3_cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
View.cpp
123 lines (101 loc) · 3.1 KB
/
View.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
//
// Created by igor on 14/06/2023.
//
#include "View.h"
void View::setScale(double scale) {
if (scale > 0)
_scale = scale;
else
Log("scale must be positive");
}
void View::makeDefault() {
_size = 25;
_pan = {0, 0};
_scale = 2;
setSize(_size);
}
void View::show() {
// firstly , update matrix.
for (auto &i : _matrix) {
i = ". ";
}
for (const auto &obj : *_objects) {
double x = obj->getLocation().x;
double y = obj->getLocation().y;
insert(x, y, obj->getName());
}
// PRINT
double largest_y = _pan.y+(_scale*_size)-_scale;
std::cout << "Display size: " << _size << ", scale: " << _scale
<< " origin: (" << _pan.x << ", " << _pan.y << ")\n";
for (int i = 0; i < _size; i++) {
if(i%3 == 0 ){ std::cout << std::setw(4) << std::setfill(' ')<<
largest_y<<" ";largest_y-=_scale*3;}
else{std::cout<<" ";}
for (int j = 0; j < _size; j++) {
std::cout << _matrix[_size * i + j].substr(0, 2);
}
std::cout << std::endl;
}
int smallest_x = _pan.x;
std::cout<<std::setw(6) << std::setfill(' ') <<smallest_x;
smallest_x+=_scale*3;
for(int i = 0;i<_size;i++)
{
if(i%3 == 0 && i != 0)
{
std::cout << std::setw(6) << std::setfill(' ')<<smallest_x;
smallest_x+=_scale*3;
}
}
std::cout<<std::endl;
/*
std::for_each(_matrix.rbegin(), _matrix.rend(),
[&jump, &scale](std::vector<std::array<char, 2>> &row) {
int runner = 0;
for (const auto &sqaure : row) {
std::cout << sqaure.front() << sqaure.back();
}
std::cout << "\n";
});
*/
}
void View::setSize(int size) {
if (size < 6) {
Log("new size too small");
return;
} else if (size >= 30) {
Log("new size too big");
return;
}
_matrix.resize(size * size);
_size = size;
/*
for_each(_matrix.begin(), _matrix.end(),
[size](std::vector<std::array<char, 2>> &object) {
object.resize(size);
});
*/
}
int View::getSize() { return _size; }
void View::insert(double x, double y, const std::string &name) {
double range = _size * _scale;
double x_range = _pan.x + range -_scale;
double y_range = _pan.y + range- _scale;
// Point top_right{x_range, y_range};
if (x > x_range || y > y_range || x < _pan.x || y < _pan.y)
return; // out of range depending on this scale,pan and size.
int x_cordinate = (x - _pan.x) / _scale;
int y_cordinate = (y - _pan.y) / _scale;
_matrix[_size * (_size - y_cordinate - 1) + x_cordinate] = name;
}
void View::setPan(double x, double y) {
_pan.x = x;
_pan.y = y;
}
View::View() { makeDefault(); }
void View::addObjects(
std::shared_ptr<std::vector<std::shared_ptr<Sim_object>>> &objects) {
_objects = objects;
}
void View::Log(const std::string &str) { std::cout << str << std::endl; }