Пример #1
0
        public static bool IsNeighbour(LevelRect a, LevelRect b)
        {
            /// # DESCRIPTION
            /// Determine whether rectangles a and b are neighbors
            /// by projecting them onto both axes and comparing their
            /// combined projections ("one-dimensional shadows") to
            /// their actual sizes.
            /// If a projection:
            ///     - is smaller than both rectangles' width/height,
            ///     then the rectangles overlap on the x/ y - axis.
            ///     - is equivalent to both rectangles' width/height,
            ///     then the rectangles are touching on the x / y - axis.
            ///     - is greater than both rectangles' width/height,
            ///     then the rectangles can not be neighbors.
            ///
            /// Return true iff the overlap on one axis is greater than zero
            /// while the overlap on the other axis is equal to zero.
            /// (If both overlaps were greater than zero, the rectangles
            /// would be overlapping. If both overlaps were equal to zero,
            /// the rectangles would be touching on a corner only.)

            int xProjection = Math.Max(a.x2, b.x2) - Math.Min(a.x1, b.x1);
            int xOverlap    = a.Width + b.Width - xProjection;

            int yProjection = Math.Max(a.y2, b.y2) - Math.Min(a.y1, b.y1);
            int yOverlap    = a.Height + b.Height - yProjection;

            return(xOverlap > 0 && yOverlap == 0 ||
                   xOverlap == 0 && yOverlap > 0);
        }
Пример #2
0
 public bool Intersects(LevelRect other)
 {
     return(x1 <= other.x2 && x2 >= other.x1 &&
            y1 <= other.y2 && y2 >= other.y1);
 }