Skip to content

Instantly share code, notes, and snippets.

@imryan
Created October 18, 2013 16:41
Show Gist options
  • Select an option

  • Save imryan/7044256 to your computer and use it in GitHub Desktop.

Select an option

Save imryan/7044256 to your computer and use it in GitHub Desktop.
Distance formula calculator.
import java.util.*;
public class Distance {
public static void main(String[] args) {
// Declare variables
Scanner sc = new Scanner(System.in);
int x1, x2, y1, y2;
// Get input(s)
x1 = sc.nextInt();
y1 = sc.nextInt();
x2 = sc.nextInt();
y2 = sc.nextInt();
// Call distance calculation method
double distance = getDistance(x1, y1, x2, y2);
System.out.println("Distance between (" + x1 + ", " + x2 + ") and (" + y1 + ", " + y2 + ") is: " + distance);
}
public static double getDistance(int x1, int y1, int x2, int y2)
{
return Math.ceil(Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2)));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment