- Home /
rect.Contains cant accept rects with a negative width and height?
To make this short, I have a classic rectangular RTS selection system. And I use the rect.Contains() function. But for some reason the rect.Contains() function does'nt work when the width or height of the rect is negative.
EDIT: here's an example code:
Rect rect3 = new Rect(Screen.width / 2, Screen.height / 2, -200, -200); // this rect doesnt work
GUI.Box(rect3, "");
if(rect3.Contains(-Input.mousePosition))//still doesnt work
{
Debug.Log("inside");
}
Rect rect4 = new Rect(Screen.width / 2, Screen.height / 2, 200, 200); // this rect works
GUI.Box(rect4, "");
if(rect4.Contains(Input.mousePosition))
{
Debug.Log("inside");
}
Howcome? Is there any way to fix this?
EDIT: my point of inverting the rect is because i have a RTS like selection system. And I use the function rect.Contains() to determine what units that are selected. And if you drag from the down right corner. The width and height of the drawn rectangle will get negative.
-Thanks :)
Answer by alexfeature · Dec 31, 2012 at 07:20 AM
Hey,
You can't have a rectangle with negative dimensions.
What you need to do is create a rectangle that has positive dimensions starting from the point where your mouse is to the point where the drag operation started.
Basically, when your mouse X and / or Y are negative you will be redrawing the rectangle FROM CURRENT location of your mouse pointer to the START OF DRAG location.
In cases when they are positive then the logic stays as you have it since your are drawing the rectangle FROM START OF DRAG location TO CURRENT mouse position.
I hope this helps, Alex
Answer by Seth-Bergman · Jul 22, 2012 at 11:34 AM
if you are using mouse position, as I suspect, as in:
if (rect.Contains(Input.mousePosition))
the x and y of Input.mousePosition go from 0 to Screen width/height
Using GUITextures would be better, as they too use the same screen coordinates
EDIT : yup, as I figured..
just keep in mind Input.mousePosition goes from (0,0) in the lower left of screen to (Screen.width,Screen.height) in the upper right
this is just an example. $$anonymous$$y question is: Is there any way to make a rect with a negative width and height. AND get the funciton rect.Contains() to work with it?
to the edit:
Yea. I already knew that. thanks. But this did'nt answer my question
then you should realize that you need to convert the ARGU$$anonymous$$ENT you are passing to something that represents the actual position of your Rect.
for example: if (rect.Contains(-Input.mousePosition))
boom. negative.
but then your Rect has to have negative values not less than -Screen.width and -Screen.height,
or you can't reach it.. but say
if (rect.Contains(-Input.mousePosition * 2))
now it could be twice as negative, get it?
you don't need to use Input.mousePosition, it could be any point. (Vector2 or Vector3)
p.s. mousePosition is a Vector3, and rect.Contains takes a point so the above code is not literal.. just making the point..
in other words, just pass in a negative Vector3/Vector2
Nope. still doesnt work. (try and look at the example code.. the rect called rect3 doesnt work because the width and height of it are negative numbers)
Ah ok my mistake after all.. Assumed you were using a negative position.. What possible reason could you have for declaring a rectangle with a negative width/height?
even if it's technically possible somehow, what's the point?
EDIT: ok , I see that making width and height negative invert the Rect. Still don't see much point though..
good luck anyway, sorry for the confusion
Answer by Seth-Bergman · Jul 22, 2012 at 12:30 PM
Now that I understand the question, here's your answer:
http://docs.oracle.com/javase/6/docs/api/java/awt/Rectangle.html
A Rectangle whose width or height is negative has neither location nor dimension along those axes with negative dimensions. Such a Rectangle is treated as non-existant along those axes. Such a Rectangle is also empty with respect to containment calculations and methods which test if it contains or intersects a point or rectangle will always return false.
etc...
Answer by Ash-Blue · Jul 20, 2019 at 09:13 PM
You're completely correct. Unity's Rect
class has no math to handle negative sizes. It jacks up any rectangle helper methods. It's a minor annoyance you can easily fix with this helper script I've written.
using UnityEngine;
namespace CleverCrow.Fluid.Dialogues.Editors {
public static class RectCleaner {
public static Rect FixNegativeSize (this Rect rectOld) {
var rect = new Rect(rectOld);
if (rect.width < 0) {
rect.x += rect.width;
rect.width = Mathf.Abs(rect.width);
}
if (rect.height < 0) {
rect.y += rect.height;
rect.height = Mathf.Abs(rect.height);
}
return rect;
}
}
}
To use it you'll need to call it like this. It's non-destructive so it returns a value.
var myRect = new Rect(0, 0, -20, -20);
var cleanedRect = myRect.FixNegativeSize();
// cleanedRect can now be used normally
Your answer
Follow this Question
Related Questions
Unity not dividing correctly C# camera pixel width & height 1 Answer
Having an issue with setting the minimum size for a field size 0 Answers
How to change the Width and Height in the script C#? (New Gui Unity 4.6 beta) 2 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers