-
Notifications
You must be signed in to change notification settings - Fork 0
/
mapRoom.cpp
177 lines (153 loc) · 5.87 KB
/
mapRoom.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
/*
* The following code reads data written to a file using roomMapping.ino
* and draws the corresponding map from that data using the SFML graphics
* library.
*/
#include <iostream>
#include <fstream>
#include <stdexcept>
#include <SFML/Graphics.hpp>
using namespace sf;
int* readData(std::string filename, int* length) {
std::ifstream infile(filename.c_str());
if (infile.fail()) {
throw std::runtime_error("Invalid file path");
}
infile >> *length; //read length of file from file
int* map_data = new int[*length]; //create array to read data into
int num;
for (int i=0;i<*length;i++) { //read all data from file into array
infile >> num;
map_data[i] = num;
}
return map_data;
}
int* getDimensions(int* map_data, int* length) {
int* dimensions = new int[4];
int direction = 0; //0 for up, 1 for right, 2 for down, 3 for left
// using the direction and map data, we will record the dimesions
// of the map, so we can scale the graphics window accordingly
for(int i=0; i<*length;) {
if (map_data[i] == 0) {//if we go straight, then add 1 to that direction
dimensions[direction] += 1;
}
else if (map_data[i] == 1) { // if we turn right, change the
direction = (direction+1)%4; // direction and add 1
dimensions[direction] +=1;
}
else if (map_data[i] == 2) { // if we turn left, change the
direction = (direction-1)%4; // direction and add 1
dimensions[direction] +=1;
}
}
return dimensions;
}
VertexArray makeLines(int* map_data, int* length, int size) {
int direction = 0; //start direction at 0 (forwards)
unsigned long linenum = (unsigned long)*length;
VertexArray lines(LinesStrip, linenum);
int x = 500;
int y = 500;
for (int i=0; i<*length; i++) {
lines[i].position = Vector2f(x,y);
if (map_data[i] == 0) { //straight
if (direction == 0) { //if going staight
//move position size pixels upwards of previous
y -= size;
}
else if(direction == 1) { // if going right
//move position size pixels right of previous
x += size;
}
else if(direction == 2) { // if going down
// move position size pixels down of previous
y += size;
}
else if(direction == 3) {// if going left
// move position size pixels left of previous
x -= size;
}
//when going straight direction does not change
}
else if (map_data[i] == 1) { //right turn
if (direction == 0) { //if going up
//move position size pixels right of previous
x += size;
}
else if(direction == 1) {// if going right
// move position size pixels down of previous
y += size;
}
else if(direction == 2) {// if going down
// move position size pizels left of previous
x -= size;
}
else if(direction == 3) {// if going left
// move position size pixels up of previous
y -= size;
}
direction = (direction+1)%4; // change direction for turn
}
else if (map_data[i] == 2) { //left turn
if (direction == 0) { //if going up
// move position size pixels left of previous
x -= size;
}
else if(direction == 1) { //if going right
// move position size pixels up of previous
y -= size;
}
else if(direction == 2) { //if going down
// move position size pixels right of previous
x += size;
}
else if(direction == 3) { //if going left
// move position size pixels down of previous
y += size;
}
direction = (direction-1)%4; // change direction for turn
}
}
return lines;
}
int main(int argc,char** argv) {
int* length = NULL;
std::string filename = argv[1]; //make filename into string
int* map_data = readData(filename, length); //get the array of map data
int* dimensions = NULL;
dimensions = getDimensions(map_data, length); //get the dimensions of the map
int width = 0;
int height = 0;
dimensions[0] = height; //get the height of the room in terms of "steps"
dimensions[3] = width; //get the width of the room in terms of "steps"
//each step is one rotation of the robot's wheels
//we are using the left dimension for width as we start on the right side
int size; //declare the variable for size of each segment
if (height>80 || width>80) { //if more than 80 segments in any direction
size = 5; //make segments smaller to fit
}
if (height>160 || width>160) { //if more than 160 segments in any direction
size = 3; //make segments smaller to fit
}
if (height>300 || width> 300) { //if more than 300 segments in any direction
size = 2; //make segments smaller
}
VertexArray lines = makeLines(map_data, length, size);
RenderWindow window(VideoMode(1000,1000), "window");
while(window.isOpen()) {
window.clear() //clear anything from the window
window.draw(lines); //draw our lines to the window
window.display(); //display the lines to the window
Event event;
while(window.pollEvent(event)) { //check for events in the window
if (event.type == Closed) {
window.close(); // if the user closes the window, close it
}
}
}
//delete all heap arrays free up memory
delete[] length;
delete[] map_data;
delete[] dimensions;
return 0;
}