-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathInt.java
52 lines (42 loc) · 923 Bytes
/
Int.java
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
public class Int
{
public static int max(int a, int b)
{
return a > b ? a : b;
}
public static int min(int a, int b)
{
return a < b ? a : b;
}
public static int abs(int a)
{
return a < 0 ? -a : a;
}
public static int sign(int a)
{
return a < 0 ? -1 : 1;
}
// distance
public static int dist(int x1, int y1, int x2, int y2)
{
int dx = x1 - x2;
int dy = y1 - y2;
return (int)Math.sqrt(dx * dx + dy * dy);
}
// fast distance approximation
public static int fdist(int x1, int y1, int x2, int y2)
{
int dx = x1 - x2;
int dy = y1 - y2;
dx = (dx < 0) ? -dx : dx;
dy = (dy < 0) ? -dy : dy;
return (dx + (dx << 1) + dy + (dy << 1)) >> 2;
}
// skips sqrt, for distance comparisions only
public static int distcmp(int x1, int y1, int x2, int y2)
{
int dx = x1 - x2;
int dy = y1 - y2;
return (dx * dx + dy * dy);
}
}