-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeuristic.java
More file actions
86 lines (81 loc) · 1.77 KB
/
Copy pathHeuristic.java
File metadata and controls
86 lines (81 loc) · 1.77 KB
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
public class Heuristic {
private int g1x;
private int g1y;
private int g2x;
private int g2y;
public Heuristic(int g1x,int g1y,int g2x,int g2y){
this.g1x=g1x;
this.g1x=g1y;
this.g2x=g2x;
this.g2y=g2y;
}
public double findCost(int x,int y){
double a=0;
double b=0;
double c=0;
double d=0;
if(x-g1x<0){
if(y-g1y<0){
a=g1x-x;
b=g1y-y;
}
else{
a=g1x-x;
b=y-g1y;
}
}
else{
if(y-g1y<0){
a=x-g1x;
b=g1y-y;
}
else{
a=x-g1x;
b=y-g1y;
}
}
if(x-g2x<0){
if(y-g2y<0){
c=g2x-x;
d=g2y-y;
}
else{
c=g2x-x;
d=y-g2y;
}
}
else{
if(y-g2y<0){
c=x-g2x;
d=g2y-y;
}
else{
c=x-g2x;
d=y-g2y;
}
}
double g1 = calcCost(a,b);
double g2 = calcCost(c,d);
double cost=0;
if(g1>g2){
cost = g2;
}
else{
cost = g1;
}
return cost;
}
public double calcCost(double a , double b){
double straight=0;
double diagonal=0;
if(a>b){
straight=a-b;
diagonal=a-straight;
}
else{
straight=b-a;
diagonal=b-straight;
}
return (diagonal*0.5 + straight);
}
}