-
Notifications
You must be signed in to change notification settings - Fork 0
/
calc_distance.c
110 lines (96 loc) · 2.44 KB
/
calc_distance.c
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* calc_distance.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yabdulha <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/10/16 18:00:04 by yabdulha #+# #+# */
/* Updated: 2018/11/20 18:51:15 by yabdulha ### ########.fr */
/* */
/* ************************************************************************** */
#include "filler.h"
#include <math.h>
static void add_dist(t_co *me, t_co *enemy, int dist, t_filler *data)
{
t_dist *new;
if (!(new = ((t_dist*)malloc(sizeof(*new)))))
exit(1);
//TODO error handler
new->me = me;
new->enemy = enemy;
new->dist = dist;
new->next = data->dist;
data->dist = new;
}
int check_sorted(t_dist *head)
{
t_dist *tmp;
tmp = head;
while (tmp && tmp->next)
{
if (tmp->dist > tmp->next->dist)
return (0);
tmp = tmp->next;
}
return (1);
}
void swap(t_filler *data, t_dist *first, t_dist *second)
{
t_dist *tmp;
if (first == data->dist)
data->dist = second;
first->next = second->next;
second->next = first;
}
void sort_distance(t_filler *data)
{
t_dist *tmp;
t_dist *before = NULL;
data->dist = merge_sort_list_recursive(data->dist, compare);
/*
tmp = data->dist;
while (tmp && tmp->next)
{
if (tmp->dist > 0 && tmp->dist > tmp->next->dist)
{
if (before)
before->next = tmp->next;
swap(data, tmp, tmp->next);
tmp = data->dist;
}
else
{
before = tmp;
tmp = tmp->next;
}
}
if (!(check_sorted(data->dist)))
sort_distance(data);
*/
}
static int eucl_distance(t_co *a, t_co *b)
{
return (sqrt(ABS(a->x - b->x) * ABS(a->x - b->x)
+ ABS(a->y - b->y) * ABS(a->y - b->y)));
}
void calc_distance(t_filler *data)
{
t_co *tmp;
t_co *tmp2;
t_dist *d;
tmp = data->square;
tmp2 = data->my;
output("calc distance");
while (tmp)
{
tmp2 = data->my;
while (tmp2)
{
add_dist(tmp2, tmp, eucl_distance(tmp, tmp2), data);
output("added distance");
tmp2 = tmp2->next;
}
tmp = tmp->next;
}
}