开发者

How do I compare objects based on their location on a line

开发者 https://www.devze.com 2023-02-25 06:52 出处:网络
i am trying to write a compare to method that will do this. // If a and b lie on the same horizontal 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.

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

class Lexicographical implements Comparator{ //This needs to be rewritten so...

// 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

public int compare(Point a, Point b) { if (a.y == b.y) // y axis are the same(same line) { if(a.x < b.x)// to the left of b(on the x axis) return -1; else return 1;// to the right of b 开发者_C百科 } else if(a.y < b.y)// y axis are not the same(below not same line) { 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. It's easy once you starting thinking more concretely.

What are the types for a and 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). 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.


One way to implement a compare Method in java is the Comparable Interface

It has only one method int compareTo(T o). This method compares this object (the object on which the method is invoked) with the specified object (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.

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;
         }
      }
   }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消