Search Unity

Width or Height values of rect change with rotation

Discussion in 'UGUI & TextMesh Pro' started by elpuerco63, Jun 6, 2017.

  1. elpuerco63

    elpuerco63

    Joined:
    Jun 26, 2014
    Posts:
    271
    I am trying to detect if a rect inside a parent rect is 100% inside the parent. The user can drag, scale and rotate the child rect but upon completion of this I set the scale to 1 to ensure it remains square.

    The code I use below gets the rects of both parent and child and then does the comparison which for the most part works, until that is the child rect is rotated.

    I am finding that for example if the rect starts out at 400x400 and the user scales and rotates to say 600 (for ease of explanation) the rect reports 250x600 (again representative only) so the test to see if the child is in the parent keeps going wrong.

    The code I use is below as are two sample images to show the problem.

    Code (csharp):
    1.  
    2. public static Rect GetRect(this RectTransform trans) {
    3.             Vector3[] corners = new Vector3[4];
    4.             trans.GetWorldCorners(corners);
    5.             Rect rect = new Rect(0,0,0,0);
    6.             rect.x = corners[0].x;
    7.             rect.y = corners[0].y;
    8.             rect.width = corners[2].x - corners[0].x;
    9.             rect.height = corners[2].y - corners[0].y;
    10.             return rect;
    11. }
    12.  
    13. // added a lot of bloat to this to try to track issue
    14. //
    15. public static bool Contains (this Rect rect1, Rect rect2) {
    16.  
    17.             float absSizeX = Math.Abs (rect2.size.x);
    18.             float absSizeY = Math.Abs (rect2.size.y);
    19.  
    20.             float rect1PositionX = rect1.position.x;
    21.             float rect1PositionY = rect1.position.y;
    22.             float rect1XSize = rect1.position.x + rect1.size.x;
    23.             float rect1YSize = rect1.position.y + rect1.size.y;
    24.  
    25.             float rect2PositionX = rect2.position.x;
    26.             float rect2PositionY = rect2.position.y;
    27.             float rect2XSize = rect2.position.x + absSizeX;
    28.             float rect2YSize = rect2.position.y + absSizeY;
    29.             if ((rect1.position.x <= rect2.position.x) && (rect1.position.x + rect1.size.x) >= (rect2.position.x + absSizeX) &&
    30.                 (rect1.position.y <= rect2.position.y) && (rect1.position.y + rect1.size.y) >= (rect2.position.y + absSizeY)) {
    31.  
    32.                 return true;
    33.             }
    34.             else {
    35.  
    36.                 return false;
    37.             }
    38.         }
    39.  
    https://ibb.co/kxtNfv

    https://ibb.co/gke67a


    EDIT:

    wood - trees comes to mind...resolved!
     
    Last edited: Jun 6, 2017