/// <summary> Check if this space Touches a box /// /// </summary> /// <param name="box">The box to check against /// </param> /// <param name="xp">The x position of the box to check /// </param> /// <param name="yp">The y position of the box to check /// </param> /// <returns> True if the box Touches these space /// </returns> public virtual bool Touches(AABox box, float xp, float yp) { float thisWidth = (this.x2 - this.x1) / 2; float thisHeight = (this.y2 - this.y1) / 2; float thisCx = this.x1 + thisWidth; float thisCy = this.y1 + thisHeight; float x1 = xp - (box.Width / 2); float x2 = xp + (box.Width / 2); float y1 = yp - (box.Height / 2); float y2 = yp + (box.Height / 2); float otherWidth = (x2 - x1) / 2; float otherHeight = (y2 - y1) / 2; float otherCx = xp; float otherCy = yp; float dx = System.Math.Abs(thisCx - otherCx); float dy = System.Math.Abs(thisCy - otherCy); float totalWidth = thisWidth + otherWidth; float totalHeight = thisHeight + otherHeight; return (totalWidth > dx) && (totalHeight > dy); }
/// <summary> Combine this space with another box /// /// </summary> /// <param name="box">The box to include in this space /// </param> /// <param name="xp">The x position of the box /// </param> /// <param name="yp">The y position of the box /// </param> public virtual void AddAABox(AABox box, float xp, float yp) { float x1 = xp - box.Width; float x2 = xp + box.Width; float y1 = yp - box.Height; float y2 = yp + box.Height; this.x1 = System.Math.Min(x1, this.x1); this.y1 = System.Math.Min(y1, this.y1); this.x2 = System.Math.Max(x2, this.x2); this.y2 = System.Math.Max(y2, this.y2); }