-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHelperFile.h
249 lines (221 loc) · 4.52 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
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*
Student: Ejup Hoxha
Semester: Fall 2018 -
Class: Advanced Algorithms - Dr. Peter Brass
University: City University of New York - Groove School of Engineering
*/
#pragma region Segments
struct Seg
{
int _id;
int _x1, _x2, _y1, _y2;
struct Seg *next;
};
int count_seg =0;
struct Seg *current = NULL; /* Top of the stack. */
void AddSegment(int __x1, int __y1, int __x2, int __y2)
{
struct Seg* temp = (struct Seg*)malloc(sizeof(struct Seg));
temp->_id = count_seg;
temp->_x1 = __x1;
temp->_y1 = __y1;
temp->_x2 = __x2;
temp->_y2 = __y2;
temp->next = current;
current = temp;
count_seg ++;
}
struct Seg* GetSegById(int __id)
{
struct Seg* temp =current;
if(temp == NULL)
{ printf("List is empty\n"); }
else
{
while(temp != NULL)
{ if(__id == temp->_id)
{ return temp; }
temp = temp->next;}
}
return temp;
}
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--; }
}
}
struct Pxy
{
int x;
int y;
};
#pragma endregion
#pragma region REQUEST POINTS
struct RequestPoint
{
int rp_id;
int _req_x, _req_y;
struct RequestPoint* next_rp;
};
int request_count =0;
struct RequestPoint *requestP = NULL;
void AddRequestPoint(struct Pxy _pxy)
{
struct RequestPoint* temp_rp = (struct RequestPoint*)malloc(sizeof(struct RequestPoint));
temp_rp->rp_id = request_count;
temp_rp->_req_x = _pxy.x;
temp_rp->_req_y = _pxy.y;
temp_rp->next_rp = requestP;
requestP = temp_rp;
request_count++;
}
struct RequestPoint* GetPoint(int _pid)
{
struct RequestPoint* temp = requestP;
while(temp != NULL)
{ if(_pid == temp->rp_id)
{ return temp;} temp = temp->next_rp;
}
return temp;
}
void DeleteRequestPoints()
{
int iter = 0;
while(requestP != NULL)
{
free(requestP);
request_count--;
requestP = requestP->next_rp;
}
}
#pragma endregion
#pragma region GRAPH GENERATOR
struct Vertice
{
int id;
int px;
int py;
long double neighbourcost[30];
};
struct Vertice graphlist[30];
double calcostnodes(struct Vertice _from, struct Vertice _to)
{
double ax = powl(_from.px-_to.px, 2);
double ay = powl(_from.py-_to.py, 2);
return sqrtl(ax+ay);
}
int nodecount = 0;
void CreateGraph()
{
struct RequestPoint *reqpoint = NULL;
struct RequestPoint *reqpoint2 = NULL;
for(int i = 0; i < request_count; i++)
{
reqpoint = GetPoint(i);
struct Vertice _node;
_node.id = reqpoint->rp_id;
_node.px = reqpoint->_req_x;
_node.py = reqpoint->_req_y;
/* now fill cost matrix of this node*/
for(int j = 0; j < request_count; j++)
{
/* If we found ourselves. */
if(i==j) _node.neighbourcost[j] = 0;
else
{
reqpoint2 = GetPoint(j);
struct Vertice tmpnode;
tmpnode.px = reqpoint2->_req_x;
tmpnode.py = reqpoint2->_req_y;
_node.neighbourcost[j] = calcostnodes(_node, tmpnode);
}
}
graphlist[i] = _node;
nodecount++;
}
}
void PrintGraph()
{
printf("{");
for(int i = 0; i < nodecount; i++)
{
struct Vertice tempnode = graphlist[i];
printf(",{");
for(int j = 0; j < nodecount; j++)
{
//printf("Node[%d]COST[%d] = %f\n",i,j,tempnode.neighbourcost[j]);
printf(",%Lf",tempnode.neighbourcost[j]);
}
printf("}\n");
}
printf("}");
}
void CreateSegmentNodes()
{
for(int i = 0; i < nodecount; i++)
{
struct Vertice nd;
nd = graphlist[i];
for(int j = 0; j < nodecount; j++)
{
if(i!=j)
{
struct Vertice nd2;
nd2 = graphlist[j];
AddSegment(nd.px, nd.py, nd2.px, nd2.py);
}
}
}
}
#pragma endregion
#pragma region PathQueue
struct PathQueue
{
long int setnkey;
double cost;
int position;
int child;
struct PathQueue* nextvertex;
};
long int pathqueue_cnt = 0;
struct PathQueue* pathqueue = NULL;
void pushvertex(long int _setnkey, int _position, int _child, double _val)
{
struct PathQueue *tmp = (struct PathQueue*)malloc(sizeof(struct PathQueue));
tmp->setnkey = _setnkey;
tmp->position = _position;
tmp->child = _child;
tmp->cost = _val;
tmp->nextvertex = pathqueue;
pathqueue_cnt++;
pathqueue = tmp;
}
struct PathQueue* GetCity(int _setnkey, int _position, int _child)
{
struct PathQueue* temp = pathqueue;
while(temp != NULL)
{ if(_setnkey == temp->setnkey)
{
if(_position == temp->position && _child == temp->child)
return temp;
}
temp = temp->nextvertex;
}
return temp;
}
void DeletePathQueue()
{
int iter = 0;
while(pathqueue != NULL)
{
free(pathqueue);
pathqueue_cnt--;
pathqueue = pathqueue->nextvertex;
}
}
#pragma endregion