using System; using UnityEngine; namespace AlicizaX.Runtime { /// /// 数学帮助类 /// public static class MathHelper { /// /// 检查两个矩形是否相交 /// /// /// /// public static bool CheckIntersect(RectInt src, RectInt target) { int minX = Math.Max(src.x, target.x); int minY = Math.Max(src.y, target.y); int maxX = Math.Min(src.x + src.width, target.x + target.width); int maxY = Math.Min(src.y + src.height, target.y + target.height); if (minX >= maxX || minY >= maxY) { return false; } return true; } /// /// 检查两个矩形是否相交 /// /// /// /// /// /// /// /// /// /// public static bool CheckIntersect(int x1, int y1, int w1, int h1, int x2, int y2, int w2, int h2) { int minX = Math.Max(x1, x2); int minY = Math.Max(y1, y2); int maxX = Math.Min(x1 + w1, x2 + w2); int maxY = Math.Min(y1 + h1, y2 + h2); if (minX >= maxX || minY >= maxY) { return false; } return true; } /// /// 检查两个矩形是否相交,并返回相交的区域 /// /// /// /// /// /// /// /// /// /// /// private static bool CheckIntersect(int x1, int y1, int w1, int h1, int x2, int y2, int w2, int h2, out RectInt rect) { rect = default; int minX = Math.Max(x1, x2); int minY = Math.Max(y1, y2); int maxX = Math.Min(x1 + w1, x2 + w2); int maxY = Math.Min(y1 + h1, y2 + h2); if (minX >= maxX || minY >= maxY) { return false; } rect.x = minX; rect.y = minY; rect.width = Math.Abs(maxX - minX); rect.height = Math.Abs(maxY - minY); return true; } /// /// 检查两个矩形相交的点 /// /// A 坐标X /// A 坐标Y /// A 宽度 /// A 高度 /// B 坐标X /// B 坐标Y /// B 宽度 /// B 高度 /// 交叉点列表 /// 返回是否相交 public static bool CheckIntersectPoints(int x1, int y1, int w1, int h1, int x2, int y2, int w2, int h2, int[] intersectPoints) { Vector2Int dPt = new Vector2Int(); if (false == CheckIntersect(x1, y1, w1, h1, x2, y2, w2, h2, out var rectInt)) { return false; } for (var i = 0; i < w1; i++) { for (var n = 0; n < h1; n++) { if (intersectPoints[i * h1 + n] == 1) { dPt.x = x1 + i; dPt.y = y1 + n; if (rectInt.Contains(dPt)) { intersectPoints[i * h1 + n] = 0; } } } } return true; } } }