I want to calculate the distance between two points in Java

后端 未结 7 1436
花落未央
花落未央 2020-12-29 21:31

OK, so I\'ve written most of a program that will allow me to determine if two circles overlap.

I have no problems whatsoever with my program aside from one issue: t

7条回答
  •  春和景丽
    2020-12-29 22:04

    Unlike maths-on-paper notation, most programming languages (Java included) need a * sign to do multiplication. Your distance calculation should therefore read:

    distance = Math.sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
    

    Or alternatively:

    distance = Math.sqrt(Math.pow((x1-x2), 2) + Math.pow((y1-y2), 2));
    

提交回复
热议问题