Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ragnaros100 · Jul 22, 2012 at 06:02 PM · c#guirectnegativecontinuation

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 :)

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ragnaros100 · Jul 22, 2012 at 06:20 PM 0
Share

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!

avatar image Owen-Reynolds · Jul 22, 2012 at 07:13 PM 1
Share

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.

avatar image ragnaros100 · Jul 22, 2012 at 10:35 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges