using System; using UnityEngine; namespace BrewMonster.Scripts.Extensions { public static class RectExtension { public static bool IsEmpty(this Rect rect) { return rect.width == 0 && rect.height == 0; } public static Rect Intersect(this Rect rc1, Rect rc2) { if ((rc1.width == 0 && rc1.height == 0) || (rc2.width == 0 && rc2.height == 0)) return Rect.zero; if (rc1.xMin >= rc2.xMax || rc2.xMin >= rc1.xMax || rc1.xMin >= rc2.yMax || rc2.yMin >= rc1.yMax) return Rect.zero; return new Rect(Mathf.Max(rc1.x, rc2.x), Mathf.Max(rc1.y, rc2.y), Mathf.Min(rc1.width, rc2.width), Mathf.Min(rc1.height, rc2.height)); } public static Rect Offset(this Rect rect, float x, float y) { rect.xMin += x; rect.yMin += y; rect.xMax += x; rect.yMax += y; return rect; } } public static class RectIntExtension { public static bool IsEmpty(this RectInt rect) { return rect.width == 0 && rect.height == 0; } public static RectInt Intersect(this RectInt rc1, RectInt rc2) { // if ((rc1.width == 0 && rc1.height == 0) || (rc2.width == 0 && rc2.height == 0)) // return RectInt.zero; // if (rc1.xMin >= rc2.xMax || rc2.xMin >= rc1.xMax || // rc1.xMin >= rc2.yMax || rc2.yMin >= rc1.yMax) // return RectInt.zero; // return new RectInt(Mathf.Max(rc1.x, rc2.x), Mathf.Max(rc1.y, rc2.y), Mathf.Min(rc1.width, rc2.width), Mathf.Min(rc1.height, rc2.height)); if (rc1.xMin >= rc2.xMax || rc2.xMin >= rc1.xMax || rc1.yMin >= rc2.yMax || rc2.yMin >= rc1.yMax) return RectInt.zero; var result = new RectInt(); result.xMin = Math.Max(rc1.xMin, rc2.xMax); result.yMin = Math.Max(rc1.yMin, rc2.yMax); result.xMax = Math.Min(rc1.xMax, rc2.xMax); result.yMax = Math.Min(rc1.yMax, rc2.yMax); result.width = result.xMax - result.xMin; result.height = result.yMax - result.yMin; return result; } public static Vector2Int Center(this RectInt rect) { // return new Vector2Int(Mathf.FloorToInt((float)rect.x + (float)rect.width / 2f), Mathf.FloorToInt((float)rect.y + (float)rect.height / 2f)); return new Vector2Int(rect.x + rect.width / 2, rect.y + rect.height / 2); } public static RectInt Offset(this RectInt rect, int x, int y) { rect.xMin += x; rect.yMax += y; rect.xMax += x; rect.yMin += y; rect.width = rect.xMax - rect.xMin; rect.height = rect.yMax - rect.yMin; return rect; } } }