Search Unity

How to Use Rect.Intersect

Discussion in 'Scripting' started by John-B, Jun 23, 2012.

  1. John-B

    John-B

    Joined:
    Nov 14, 2009
    Posts:
    1,262
    Rect.Intersect(Rect, Rect) is a .Net function, so it should work, right? Is there a file I need to include like I do when I use lists? It says Rect.Intersect is in the System.Windows namespace, but doesn't say what class.
     
  2. flaminghairball

    flaminghairball

    Joined:
    Jun 12, 2008
    Posts:
    868
    UnityScript:

    Code (csharp):
    1.  
    2. import System.Drawing;
    3.  
    4. function IntersectRect(){
    5.   var r1 = new Rectangle(make, your, rectangle, here);
    6.   var r2 = new Rectangle(make, another, rectangle, here);
    7.   var intersected = Rectangle.Intersect(r1, r2);  //bob's your uncle!
    8. }
    9.  
    C#:
    Code (csharp):
    1.  
    2. using System.Drawing;
    3.  
    4. void IntersectRect(){
    5.   Rectangle r1 = new Rectangle(make, your, rectangle, here);
    6.   Rectangle r2 = new Rectangle(make, another, rectangle, here);
    7.   Rectangle intersected = Rectangle.Intersect(r1, r2);  //bob's your uncle!
    8. }
    9.  
    Standard typed-in-browser disclaimer. :)
     
  3. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Yeah, there isn't any System.Drawing in Unity. It would be pretty straightforward to make your own function that does the same thing though.

    Unity uses Mono rather than .NET, and a subset of Mono at that. Also the Rect struct in Unity is a custom struct specific to Unity, not the one from .NET. (Among other things, it uses floats rather than doubles.)

    --Eric
     
  4. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    I'm really curious why the Rect class in Unity is so under-featured. Intersection testing, and computation of union/intersection, are basic functions of any rectangle class. Sure, it doesn't take long for us each to write our own, but multiply that by the (roughly) bajillion Unity devs out there, and it adds up to some serious wasted time.
     
  5. AdamCorncrow

    AdamCorncrow

    Joined:
    Jun 17, 2013
    Posts:
    1
    Code (csharp):
    1. using UnityEngine;
    2.  
    3. public static class UnityFix
    4. {
    5.    
    6.     public static bool Intersect(this Rect rectA, Rect rectB)
    7.     {
    8.         return ( Mathf.Abs(rectA.x - rectB.x) < (Mathf.Abs(rectA.width + rectB.width) / 2))
    9.              (Mathf.Abs(rectA.y - rectB.y) < (Mathf.Abs(rectA.height + rectB.height) / 2));
    10.     }
    11.    
    12. }
     
  6. Acegikmo

    Acegikmo

    Joined:
    Jun 23, 2011
    Posts:
    1,294
    I tried the above code, but it didn't seem to work.

    This did:

    Code (csharp):
    1.         public static bool Intersect( Rect a, Rect b ) {
    2.             bool c1 = a.x < b.xMax;
    3.             bool c2 = a.xMax > b.x;
    4.             bool c3 = a.y < b.yMax;
    5.             bool c4 = a.yMax > b.y;
    6.             return c1  c2  c3  c4;
    7.         }
    Note that the above code doesn't work if your Rects have a negative width. If you want one that accepts any rects, you can use this:

    Code (csharp):
    1.         public static bool Intersect( Rect a, Rect b ) {
    2.             FlipNegative( ref a );
    3.             FlipNegative( ref b );
    4.             bool c1 = a.xMin < b.xMax;
    5.             bool c2 = a.xMax > b.xMin;
    6.             bool c3 = a.yMin < b.yMax;
    7.             bool c4 = a.yMax > b.yMin;
    8.             return c1  c2  c3  c4;
    9.         }
    10.  
    11.         public static void FlipNegative(ref Rect r) {
    12.             if( r.width < 0 )
    13.                 r.x -= ( r.width *= -1 );
    14.             if( r.height < 0 )
    15.                 r.y -= ( r.height *= -1 );
    16.         }
     
    Last edited: Sep 27, 2013
  7. rakkarage

    rakkarage

    Joined:
    Feb 3, 2014
    Posts:
    683
  8. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    If we're requesting "functions that should exist in the Rect class but don't", can we also get a Rect.Lerp?
     
  9. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,717
    You are aware of method extension, right?
     
  10. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Has already been answered in this thread.
     
  11. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,717
    That's why sharing code exist.
     
  12. isgoed

    isgoed

    Joined:
    Oct 18, 2019
    Posts:
    4
    Code for Union of 2 Rects:

    Code (CSharp):
    1. public static void RectUnion(ref Rect RA, ref Rect RB, out Rect RUnion)
    2. {
    3.     RUnion = new Rect();
    4.  
    5.     RUnion.min = Vector2.Min(RA.min, RB.min);
    6.     RUnion.max = Vector2.Max(RA.max, RB.max);
    7. }