Jump to content

Maior distância entre coordenadas


Recommended Posts

Boa tarde,

Estou a tentar fazer um programa que me calcule a maior distância entre duas coordenadas, e gostava que me ajudassem pois não estou a receber o valor correto.

Se possível gostaria também de saber se há possibilidade de simplificar o tamanho da função.

public static int calculateDist(int x1, int y1, int x2, int y2) {
        int i = x2 - x1;
        int i1 = y2 - y1;
        int a = (int)Math.pow(i,2);
        int b = (int)Math.pow(i1,2);
        double i2 = a + b;
        double c = Math.sqrt(i2);
        return (int)c;

    public static double methodA(int A, int B, int C, int D) {
        List<Integer> integers = Arrays.asList(
                calculateDist(A, B, C, D),
                calculateDist(A, B, D, C),
                calculateDist(A, C, B, D),
                calculateDist(A, C, D, B),
                calculateDist(A, D, B, C),
                calculateDist(A, D, C, B),
                calculateDist(B, A, C, D),
                calculateDist(B, A, D, C),
                calculateDist(B, C, A, D),
                calculateDist(B, C, D, A),
                calculateDist(B, D, A, C),
                calculateDist(B, D, C, A),
                calculateDist(C, A, B, D),
                calculateDist(C, A, D, B),
                calculateDist(C, B, A, D),
                calculateDist(C, B, D, A),
                calculateDist(C, D, A, B),
                calculateDist(C, D, B, A),
                calculateDist(D, A, B, C),
                calculateDist(D, A, C, B),
                calculateDist(D, B, A, C),
                calculateDist(D, B, C, A),
                calculateDist(D, C, A, B),
                calculateDist(D, C, B, A));

        Integer max = integers.get(0);
        for (int i = 0; i < integers.size(); i++) {
            if (integers.get(i) > max) {
                max = integers.get(i);
        return max;
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.