2015-06-30 2 views
0

Вот сегмент кода, с которым у меня возникают проблемы. Независимо от того, пересекаются ли круги, программа возвращается, что они не пересекаются. Что мне здесь не хватает?Почему это уравнение не дает правильного возврата?

if(intersectCir(xBlue, yBlue, radBlue, xRed, yRed, radRed) == true) { 
     System.out.println("The blue circle intersects the red circle."); 
    } else if(intersectCir(xBlue, yBlue, radBlue, xRed, yRed, radRed) != true){ 
     System.out.println("The blue circle does not intersect the red circle."); 
    } 

    if(intersectCir(xBlue, yBlue, radBlue, xGreen, yGreen, radGreen) == true) { 
     System.out.println("The blue circle intersects the green circle."); 
    } else if(intersectCir(xBlue, yBlue, radBlue, xGreen, yGreen, radGreen) != true){ 
     System.out.println("The blue circle does not intersect the green circle."); 
    } 

    if(intersectCir(xGreen, yGreen, radGreen, xRed, yRed, radRed) == true) { 
     System.out.println("The green circle intersects the red circle."); 
    } else if(intersectCir(xBlue, yBlue, radBlue, xRed, yRed, radRed) != true){ 
     System.out.println("The green circle does not intersect the red circle."); 
    } 
} 




public static boolean intersectCir(int x1, int y1, int rad1, int x2, int y2, int rad2) { 
    if(Math.sqrt(Math.pow(x1 - x2, 2)) + (Math.sqrt(Math.pow(y1 - y2, 2))) <= (rad1 + rad2)); 
     return true; 
} 

}

+0

почти весь этот код бессмыслен. все, что вам нужно было показать, это метод 'intersectCir()' и объяснять, какие параметры ... нам все равно, откуда поступают данные, если проблема связана с ИСПОЛЬЗОВАНИЕМ данных. –

+0

Вы пытались добавить «else return false»; к функции «if» внутри «intersectCir»? – Wazaaaap

+3

После слова 'if' имеется точка с запятой, поэтому следующая строка всегда выполняется. – Michelle

ответ

0

Ваше расстояние формула была неправильно переводил. Исправлено и прокомментировано ниже:

public static boolean intersectCir(int x1, int y1, int rad1, int x2, int y2, int rad2) { 
    //distance formula, applied: 
    // ((x1-x2)^2+(y1-y2)^2)^0.5 <= rad1+rad2 
    final int xdiff = x1 - x2; 
    final int ydiff = y1 - y2; 
    final int totalRad = rad1 + rad2; 
    // ((xdiff)^2+(ydiff)^2)^0.5 <= totalRad 
    final int distanceSquared = xdiff * xdiff + ydiff * ydiff;//beware overflow! 
    // (distanceSquared)^0.5 <= totalRad 
    //square roots are hard, better to just square both sides: 
    // distanceSquared <= totalRad^2 
    return distanceSquared <= totalRad * totalRad; 
    } 
+1

Спасибо, этот метод кажется яснее формулы квадратного корня и отлично работает. – pocjt9324

1

Это ваш исправленный метод.

public static boolean intersectCir(int x1, int y1, int rad1, int x2, int y2, int rad2) { 
if(Math.sqrt(Math.pow(x1 - x2, 2)) + (Math.sqrt(Math.pow(y1 - y2, 2))) <= (rad1 + rad2)) 
    return true; 
else 
    return false; 

}

+0

Не могли бы вы объяснить, что вы сделали для @ pocjt9324 – Paradox

+0

Спасибо за ввод! – pocjt9324

+0

Даже с изменениями он по-прежнему показывает, что они не пересекаются, когда они это делают. – pocjt9324

Смежные вопросы