-3

The user has to select two different points in two JComboBoxes, after selecting them, the program needs to get the distance between the two points selected.I can get the input from the ComboBoxes, but i need to calculate the distance between them, i tried using latitude and longitude, but i need to do that for every possible scenario( say 15 different points ). Any ideas ?

Commongrate
  • 123
  • 1
  • 12

1 Answers1

1

How about some simple geometry using the pythagorean theorem, then you can just loop through the 15 different points and run the math for them all.

distance = sqrt(x*x+y*y)

In your case x and y are the delta between your 2 points (x1-x2, y1-y2)

double dx = pt1.x-pt2.x;
double dy = pt1.y-pt2.y;

double distance = Math.sqrt(dx*dx+dy*dy);

http://www.mathwarehouse.com/algebra/distance_formula/index.php

ug_
  • 11,267
  • 2
  • 35
  • 52
  • Sorry, I understand the math portions, the thing that i don't understand is how to compare one point with the other 15 different times. – Commongrate Dec 11 '16 at 08:03
  • @Commongrate if you can post some of the code you are strugging with it will be easier to help. ATM im not sure and i think most people dont know what your points look like or what your doing with the comparing – ug_ Dec 11 '16 at 08:09
  • @ ug_ Ok nvm what i said, can you tell me about the formula above and if it gives a value in radians or degrees? – Commongrate Dec 11 '16 at 11:43
  • @Commongrate Neither radians or degrees, it gives you a length. You should read about the formula and how to use it. – ug_ Dec 11 '16 at 21:08
  • @ ug_ will do, thanks for the help. – Commongrate Dec 12 '16 at 05:36