-
Notifications
You must be signed in to change notification settings - Fork 0
/
HelperFile.h
185 lines (161 loc) · 3.69 KB
/
HelperFile.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
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
/*
Student: Ejup Hoxha
Semester: Fall 2018 -
Class: Advanced Algorithms - Dr. Peter Brass
University: City University of New York - Groove School of Engineering
*/
/* *************************** */
/* Segment's Stack LinkedList */
/* *************************** */
#pragma region SEGMENT
struct Seg
{
int _id;
int _x1, _x2, _y1, _y2;
struct Seg *next;
};
int count_seg =0;
struct Seg *current = NULL; /* Top of the stack. */
/* We add each segment into the stack by calling this function. */
void AddSegment(int __id, int __x1, int __y1, int __x2, int __y2)
{
struct Seg* temp = (struct Seg*)malloc(sizeof(struct Seg));
temp->_id = __id;
temp->_x1 = __x1;
temp->_y1 = __y1;
temp->_x2 = __x2;
temp->_y2 = __y2;
temp->next = current;
current = temp;
count_seg ++;
}
/* When we need to search for a specific segment by id. */
struct Seg* GetSegById(int __id)
{
struct Seg* temp =current;
if(temp == NULL)
{ printf("List is empty\n"); }
else
{
while(temp != NULL) //lype segmentin me ID
{ if(__id == temp->_id)
{ //printf("Segment found[%d] =(%d,%d)~(%d,%d)\n", temp->_id,temp->_x1, temp->_y1, temp->_x2, temp->_y2);
return temp; }
temp = temp->next;
}}
return temp;
}
/* Delete all segments on stack. */
void DeleteSegments()
{
int iter= count_seg;
for(int i=0; i<count_seg;i++)
{
struct Seg* temp = current;
if(current!=NULL) { current = current->next;
free(current); count_seg--; }
}
}
#pragma endregion
#pragma region ROBOT
/* Robot's struct. */
struct Robot
{
int r_id;
int r_px, r_py;
int r_height;
int r_width;
double distance_trv;
struct Robot* n_rob;
};
#pragma endregion
#pragma region ROBOT ROAD
/*
Robot Road.
This struct keeps logs of robot's
travel route.
*/
struct RobotRoad
{
int line_id;
int rob_id;
int rx1, ry1;
int rx2, ry2;
struct RobotRoad* next_road;
};
int c_r_road =0; /* Number of elements. */
struct RobotRoad *r_line = NULL; /* Top of the stack. */
/* Whenever we move we call this function to add movement to history. */
void AddRoad(int r_id, int __x1, int __y1, int __x2, int __y2)
{
c_r_road ++;
struct RobotRoad* temp = (struct RobotRoad*)malloc(sizeof(struct RobotRoad));
temp->line_id =c_r_road;
temp->rob_id = r_id;
temp->rx1 = __x1;
temp->ry1 = __y1;
temp->rx2 = __x2;
temp->ry2 = __y2;
temp->next_road = r_line;
r_line = temp;
}
/* Get a specific segment of the robot's road by id. */
struct RobotRoad* GetRoad(int l_id)
{
struct RobotRoad* temp =r_line;
if(temp == NULL)
{ printf("List is empty\n");}
else
{ while(temp != NULL)
{ if(l_id == temp->line_id)
{ return temp;}
temp = temp->next_road;
}
}
return temp;
}
/* Delete robot's path. */
void DeleteRoad()
{
struct RobotRoad* tmp = r_line;
int iter= 0;
for(int i=0; i<c_r_road;i++)
{ if(r_line!=NULL)
{
r_line = r_line->next_road;
free(r_line); r_line--;
}
}
}
#pragma endregion
#pragma region REQUEST POINT
/* Request point structure. */
struct req_point
{
int rp_id;
int _req_x, _req_y;
struct req_point* next_rp;
};
int req_id =0;
struct req_point *last_in = NULL;
/* Every time we request a new position it's added to this linked list, so we have complete history of the movements. */
void AddRequestPoint(int _xpos, int _ypos)
{
struct req_point* temp_rp = (struct req_point*)malloc(sizeof(struct req_point)); //Allocate memory;
temp_rp->rp_id = req_id;
temp_rp->_req_x = _xpos;
temp_rp->_req_y = _ypos;
temp_rp->next_rp = last_in;
last_in = temp_rp;
req_id ++;
}
struct req_point* GetPoint(int _pid)
{
struct req_point* temp = last_in;
while(temp != NULL)
{ if(_pid == temp->rp_id)
{ return temp;} temp = temp->next_rp;
}
return temp;
}
#pragma endregion