简体   繁体   English

如何根据对象在行中的位置比较对象

[英]How do I compare objects based on their location on a line

i am trying to write a compare to method that will do this. 我正在尝试编写一个将与之进行比较的方法。

// If a and b lie on the same horizontal line, 
//    then a < b when a is to the left of b
// Otherwise, a < b when a is below b   

i dnt really know how to do this, usually i just compare if a>b return +ve int and -ve int if less than or 0 for equal. 我真的不知道该怎么做,通常我只是比较a> b是否返回+ ve int和-ve int如果小于或等于0。

My solution from your suggestions......... 根据您的建议我的解决方案.........

i used ideas form Jim Blackler, Ralph and Peter Lawrey and came up with this. 我使用吉姆·布莱克勒,拉尔夫和彼得·劳瑞的想法,提出了这个想法。 it works sorry i was a little confused and did not think of the Cartesian cordinates thanks Aasmund Eldhuset, this is my final compare method.. and it works 它对不起我有点困惑,没有想到笛卡尔坐标,谢谢Aasmund Eldhuset,这是我最后的比较方法。

class Lexicographical implements Comparator{ //This needs to be rewritten so... class Lexicographical实现Comparator {//需要重写,因此...
// If a and b lie on the same horizontal line, // then a < b when a is to the left of b // Otherwise, a < b when a is below b //如果a和b位于同一水平线上,//那么a <b当a在b的左边时// //否则,a <b当a在b以下时
public int compare(Point a, Point b) { if (ay == by) // y axis are the same(same line) { if(ax < bx)// to the left of b(on the x axis) return -1; public int compare(Point a,Point b){if(ay == by)// y轴相同(同一行){if(ax <bx)// b的左侧(在x轴上)return -1; else return 1;// to the right of b } else if(ay < by)// y axis are not the same(below not same line) { return -1; 否则返回1; ///在b的右边}否则if(ay <by)// y轴不相同(在不相同的行下){return -1;

    }
    else 
        return 0;
}

} }

Do you mean like 你是说喜欢

class Point implements Comparable<Point> {
  double x,y;

  public int compareTo(Point b) {
      // If a and b lie on the same horizontal line, 
      if (y == b.y)
      //    then a < b when a is to the left of b
          return x < b.x ? -1 : x > b.x ? +1 : 0;
      // Otherwise, a < b when a is below b
      return y < b.y ? -1 : y > b.y ? +1 : 0;
  }
}

You'll need a class attribute for whatever types a and b are that you can compareTo. 对于a和b可以比较的类型,您将需要一个class属性。 It's easy once you starting thinking more concretely. 一旦开始更具体地思考,这很容易。

What are the types for a and b? a和b的类型是什么?

if (a.y == b.y) {
  return a.x < b.x;
} else {
  return a.y < b.y;
}

From the text, it sounds like a and b are Cartesian coordinates (each with an x and a y value). 从文本中,听起来像ab是笛卡尔坐标(每个都有xy值)。 Do you have a class that represents coordinates? 您是否有一个代表坐标的类? If not, you should make one, and the compare method should take two instances of that class, and you can use their x and y values to determine whether they are on the same horizontal line, or which one is to the left of the other. 如果不是,则应做一个,并且compare方法应采用该类的两个实例,然后可以使用它们的xy值确定它们是否在同一条水平线上,或者哪个在另一条水平线上。

One way to implement a compare Method in java is the Comparable Interface 在Java中实现compare方法的一种方法是Comparable接口

It has only one method int compareTo(T o) . 它只有一个方法int compareTo(T o) This method compares this object ( the object on which the method is invoked ) with the specified object ( b ). 此方法将此对象( 在其上调用了该方法的对象 )与指定的对象( b )进行比较。 Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object. 当此对象小于,等于或大于指定的对象时,返回负整数,零或正整数。

Assume the object is of class Pseudo ( because it is only pseudo code ), then the compare method should looks like this. 假设对象属于Pseudo类( 因为它只是伪代码 ),那么compare方法应如下所示。

class Pseudo implements Comparable<Pseudo> {

   @Override
   int compareTo(Pseudo b) {

     if (this and b lie on the same horizontal line) {
        if (this is to the left of b) {
           return -1;
        } else {
          return 1;
        }
      } else {
         if (this is to the below of b) {
           return -1;
         } else {
            return 1;
         }
      }
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM