Coordinate Problem

I cracked my head about it - and I find it easy, but my geometry / algebra is pretty rubbish and I don’t remember how to do it from my school days!

Editorial: I have a list of coordinates with people standing next to them. I need an algorithm for organizing people from top to bottom in the list (array), and in the second criterion it is required that the coordinates that are closer to the beginning on the left, all the rest - how would you do that?

The code should show the order as:

  • Tom
  • Harry
  • Bob
  • Dave

See the chart below:

alt text

+3
source share
6 answers

, , y , x, - :

if (a.y > b.y)
// a is before b
else if (a.x < b.x)
// a is before b
else
// b is before a

- . Y - X. Y , , , X. , , ArrayList.sort(), , :

public int compareTo(person a, person b) {
    if (a.y == b.y)
       return a.x-b.x
    else
       return b.y-a.y
}

//compareTo(Tom, Harry) == -50 (tom is before harry)
//compareTo(Tom, Bob) == -25 (tom is before bob)
//compareTo(Dave, Bob) == 30 (dave is after bob)
+8

2D-, (0, 100).

EDIT:

, , , 2 , .

, , . , , y-coord. .

: , 2 . , .

+2

X, ( ) 100 * (100 - Y) + X.

+1

:

orderValue = x+(100-y)

orderValue, " " ( , y = 100-x) .

+1

The comparator is as follows:

int d = o2.y - o1.y;
if (d == 0)
    d = o1.x - o2.x;
return d;

First it is sorted by Y, then by X (for all objects that have the same Y).

[EDIT] Fixed sorting order of Y.

0
source

Maybe you could study the Haversin formula, which is used in navigation to calculate the proximity of two points. However, this mainly refers to points on the sphere. http://en.wikipedia.org/wiki/Haversine_formula

-3
source

Source: https://habr.com/ru/post/1709205/


All Articles