- Home /
How do I make rect.Contains() accept a rect with negative width and height?
Contiuation of this...
I basicly want a working way to drag from the down right corner of a rect and select the units inside that drawn rect.
I've used rect.Contains() before. but it seems that it wont accept if the rects width and height are negative. (as they will get when you drag from the down right corner)
-please I need help on this :)
Answer by Bunny83 · Jul 22, 2012 at 11:28 PM
This function will generate a Rect from two arbitrary points:
Rect FromDragPoints(Vector2 P1, Vector2 P2)
{
Vector2 D = P1 - P2;
Rect R;
If (D.x < 0)
R.x = P1.x;
else
R.x = P2.x;
If (D.y < 0)
R.y = P1.y;
else
R.y = P2.y;
R.width = Mathf.Abs(D.x);
R.height = Mathf.Abs(D.y);
return R;
}
Another way would be:
Rect FromDragPoints(Vector2 P1, Vector2 P2)
{
Rect R;
R.x = Mathf.Min(P1.x,P2.x);
R.y = Mathf.Min(P1.y,P2.y);
R.xMax = Mathf.Max(P1.x,P2.x);
R.xMax = Mathf.Max(P1.y,P2.y);
return R;
}
but the first way is faster.
Btw: this is very simple math and everybody was helping you. Help doesn't mean someone else should do it for your (which i did here). Knowledge is useless if you can't adapt it to other problems.
Answer by Eric5h5 · Jul 22, 2012 at 06:17 PM
You don't need negative width/height, not even if you drag from the right corner. Just have the lower left always be the origin, then the width/height will always be positive.
AARGH! .... Im getting a bit fustrated now... It seams no-one want to help me with this problem. I need more guidance! Not just: "do this. " I need to know HOW to do it!
Suppose X is 100 and width is -20. That means you really have a rectangle with X=80 and width=20. Notice how 80 is the original X plus the width (plus -20 gives 80.) And the new width is just flipped.
So, if width is negative, can get the same rect with positive width using: newX = X+width; newWidth=-width;
Do the same for height.
thanks Owen :) ... That really helped me. You got me on the right track :) ... Your sugestion didnt work at all. But I found the solution later. But you got me going :)
Your answer
Follow this Question
Related Questions
Can't destroy an ui element 2 Answers
Need help understanding GUI and Rect.Contains interaction 0 Answers
strange Rect behavior on Y axis 1 Answer
Distribute terrain in zones 3 Answers
How expensive is Rect.Contains(Event.current.mousePosition)? 1 Answer