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:
See the chart below:
, , 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)
2D-, (0, 100).
EDIT:
, , , 2 , .
, , . , , y-coord. .
: , 2 . , .
X, ( ) 100 * (100 - Y) + X.
:
orderValue = x+(100-y)
orderValue, " " ( , y = 100-x) .
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.
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
Source: https://habr.com/ru/post/1709205/More articles:Suppress tlbimp warnings in visual studio - .netHow to handle an exception when Directory.GetFiles () throws an exception when it finds a file name, it doesn't like it? - .netimplementation of the template engine - c #Visual Basic 6 Data Structures - vbaGridview Search Using DataTable Datasource - searchEquivalent to SuspendLayout and ResumeLayout in WPF - layoutHow can I correctly define my build platform? - cDecimal value Check for zero - mathInstrumental (diagnostic) library for C ++ - c ++Стиль конкретных дат в jquery ui datepicker плагин - jquery-uiAll Articles