-
Notifications
You must be signed in to change notification settings - Fork 0
/
distance.c
108 lines (97 loc) · 2.46 KB
/
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* distance.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rvan-der <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/04 05:34:05 by rvan-der #+# #+# */
/* Updated: 2017/02/27 16:32:48 by rvan-der ### ########.fr */
/* */
/* ************************************************************************** */
#include "filler.h"
double sqroot(double nb)
{
double min;
double max;
double res;
res = (nb < 2 ? nb : nb / 2);
if (nb >= 1)
{
max = res;
min = 1;
while (res * res != nb && (max - min) >= 0.001)
{
if (res * res > nb)
{
max = res;
res = min + ((max - min) / 2);
}
else
{
min = res;
res = min + ((max - min) / 2);
}
}
}
return (res);
}
double distance_2d(t_coord p1, t_coord p2)
{
int dx;
int dy;
dx = p1.x - p2.x;
dx = (dx < 0 ? -dx : dx);
dy = p1.y - p2.y;
dy = (dy < 0 ? -dy : dy);
return (sqroot((double)(dx * dx + dy * dy)));
}
double dmin_to_coord(t_coord pnt, t_coord *grp)
{
double dmin;
double tmp;
dmin = 0;
if (grp != NULL)
{
dmin = distance_2d(*grp, pnt);
grp = grp->next;
while (grp != NULL && (tmp = distance_2d(*grp, pnt)))
{
dmin = (tmp < dmin ? tmp : dmin);
grp = grp->next;
}
dmin = (grp != NULL ? 0 : dmin);
}
return (dmin);
}
double dmin_to_skin(t_coord pnt, t_skin *grp)
{
double dmin;
double tmp;
t_skin *tmps;
dmin = 0;
if ((tmps = grp) != NULL)
{
dmin = distance_2d(tmps->crd, pnt);
tmps = tmps->next;
while (tmps != NULL && tmps != grp && \
(tmp = distance_2d(grp->crd, pnt)))
{
dmin = (tmp < dmin ? tmp : dmin);
tmps = tmps->next;
}
dmin = (tmps != NULL && tmps != grp ? 0 : dmin);
}
return (dmin);
}
int dmin_to_wall(t_coord pnt, t_plateau p)
{
int dmin;
int tmp;
tmp = (p.size).x - pnt.x - 1;
dmin = (pnt.x > tmp ? tmp : pnt.x);
tmp = (p.size).y - pnt.y - 1;
dmin = (dmin < tmp ? dmin : tmp);
dmin = (dmin < pnt.y ? dmin : pnt.y);
return (dmin);
}