I have a function that creates a rectangle from two rectangles that overlap, I was wondering if there is a smarter way to do it purely mathematically rather than lots of if
statements to check min
and max
the way I currently have setup.
This is my function
public static bool Overlaps(this Bounds a, Bounds bounds, out Bounds result)
{
result = new Bounds();
//they don't overlap
if (!a.Intersects(bounds)) return false;
float minX;
float maxX;
float minZ;
float maxZ;
if (a.min.x > bounds.min.x)
minX = a.min.x;
else
minX = bounds.min.x;
if (a.max.x < bounds.max.x)
maxX = a.max.x;
else
maxX = bounds.max.x;
if (a.min.z > bounds.min.z)
minZ = a.min.z;
else
minZ = bounds.min.z;
if (a.max.z < bounds.max.z)
maxZ = a.max.z;
else
maxZ = bounds.max.z;
// using XZ plane for debug
Vector3 min = new Vector3(minX,0,minZ);
Vector3 max = new Vector3(maxX,0,maxZ);
result.SetMinMax(min,max);
return true;
}
Here it is in action