public int XcompareTo(Object obj) { DepthSegment other = (DepthSegment) obj; // if segments are collinear and vertical compare endpoints if (isVertical() && other.isVertical() && upwardSeg.p0.x == other.upwardSeg.p0.x) return compareX(this.upwardSeg, other.upwardSeg); // check if segments are trivially ordered along X if (upwardSeg.maxX() <= other.upwardSeg.minX()) return -1; if (upwardSeg.minX() >= other.upwardSeg.maxX()) return 1; /** * try and compute a determinate orientation for the segments. Test returns 1 if other is left * of this (i.e. this > other) */ int orientIndex = upwardSeg.orientationIndex(other.upwardSeg); // if orientation is determinate, return it if (orientIndex != 0) return orientIndex; /** * If comparison between this and other is indeterminate, try the opposite call order. * orientationIndex value is 1 if this is left of other, so have to flip sign to get proper * comparison value of -1 if this is leftmost */ if (orientIndex == 0) orientIndex = -1 * other.upwardSeg.orientationIndex(upwardSeg); // if orientation is determinate, return it if (orientIndex != 0) return orientIndex; // otherwise, segs must be collinear - sort based on minimum X value return compareX(this.upwardSeg, other.upwardSeg); }
/** * Defines a comparison operation on DepthSegments which orders them left to right * * <pre> * DS1 < DS2 if DS1.seg is left of DS2.seg * DS1 > DS2 if DS1.seg is right of DS2.seg * </pre> * * @param obj * @return the comparison value */ public int compareTo(Object obj) { DepthSegment other = (DepthSegment) obj; if (!envelopesOverlap(upwardSeg, other.upwardSeg)) return upwardSeg.compareTo(other.upwardSeg); // check orientations int orientIndex = upwardSeg.orientationIndex(other.upwardSeg); if (orientIndex != 0) return orientIndex; orientIndex = -other.upwardSeg.orientationIndex(upwardSeg); if (orientIndex != 0) return orientIndex; // segments cross or are collinear. Use segment ordering return upwardSeg.compareTo(other.upwardSeg); }