Last active
April 20, 2016 06:42
-
-
Save amrishodiq/01480f0e5a6c3591215ab595a074f60a to your computer and use it in GitHub Desktop.
Solving distance of two cities
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
double perfectCity(double[] departure, double[] destination) { | |
double minorVert = 0; | |
double minorHor = 0; | |
minorVert = minorDistance(departure[1], destination[1]); | |
minorHor = minorDistance(departure[0], destination[0]); | |
return minorVert + minorHor; | |
} | |
double minorDistance(double a, double b) { | |
if (a == b) return 0; | |
if (!isInBetween(a, b)) { | |
double left = floor(a); | |
left = (a - left) + (b - left); | |
double right = ceil(a); | |
right = (right - a) + (right - b); | |
double result = left < right?left:right; | |
//result = result % 1.0; | |
return result; | |
} else { | |
double min = min(a, b); | |
double max = max(a,b); | |
double minRight = ceil(min); | |
double result = (minRight - min) + (max - minRight); | |
//result = result = result % 1.0; | |
return result; | |
} | |
} | |
double min(double a, double b) { | |
return a < b?a:b; | |
} | |
double max(double a, double b) { | |
return a > b?a:b; | |
} | |
/** | |
* Check wether there is road between them. | |
* @return true if there is road(s) between a and b. | |
*/ | |
boolean isInBetween(double a, double b) { | |
if (a < 0.0 && b > 0.0) return true; | |
if (a > 0.0 && b < 0.0) return true; | |
if (floor(a) != floor(b)) return true; | |
return false; | |
} | |
double abs(double input) { | |
if (input > 0.0) return input; | |
return 0.0 - input; | |
} | |
double floor(double input) { | |
double result = input - (input % 1.0); | |
return result; | |
} | |
double ceil(double input) { | |
if (input % 1.0 == 0.0) { | |
return input; | |
} | |
return floor(input) + 1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment