我使用javacomparator将位置对象与点(x,y)进行比较。
我需要能够比较两个点,以检索一个正整数或负整数,这将允许我排序(x,y)点,其中x值排序第一,然后y值第二(如果这有意义……)例如:
(3,4) (2,5) (1,1) (1,3) (3,3)
变成这样:
(1,1) (1,3) (2,5) (3,3) (3,4)
我认为有一种方法可以做到这一点,就是给x值一个大的优先级,把它乘以一个大的数字,比如1000。这样:比较(3,3)和(1,1):
int x_multiplier = 1000;
int value1 = (p1.x * x_multiplier ) + p1.y; // = 3 * 1000 + 3 = 3003
int value2 = (p2.x * x_multiplier ) + p2.y; // = 1 * 1000 + 1 = 1001
return value1-value2; // = 2002. Value1 is greater, thus p1 be later in list.
这是可行的,但问题是如果y值应该等于或大于x\u乘数,那么这就失败了(因为y值现在等于1 x值。。。再说一次,如果这有道理的话。)
// Comparing p1 = (2,0) & p2 = (1,18)
int x_multiplier = 10;
int value1 = (p1.x * x_multiplier ) + p1.y; // = 2 * 10 + 0 = 20
int value2 = (p2.x * x_multiplier ) + p2.y; // = 1 * 10 + 18 = 28
return value1-value2; // = -8, value2 is greater, and thus p2 will be later in the list. However, we know by looking at the points that p2 should come first.
我甚至不知道如何去寻找这个,所以如果有答案的话我就找不到了。
1条答案
按热度按时间ckocjqey1#