Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Dreamerking007 · Feb 12, 2020 at 01:46 PM · arraysbooleansrectangle

need help checking if any bool in a rotated rectangle is true

let's get right down to business i have a 2d bool array and i need to find out if any bool within a rotated rectangle is true. i don't need to know which bool is true just if any are true.

currently the way i was planning on doing this is to loop through a rectangle that contains the rotated rectangle checking each bool if it is inside(using is left method) and then checking to see if the bool is true. but i was wondering if there is a more efficient way of doing 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 · Feb 12, 2020 at 02:52 PM

No, there's not really a more efficient way. Creating an axis aligned bounding box of your rotated rectangle is the best optimisation you can do. Of course if the rectangle area is large, you might want to think about using a quadtree or simple chunks to minimize the unnecessary tests. However depending on the number of test points this might not be worth it.


For example in my iterative mandelbrot renderer I actually store the exact position(c), the current position(z) and the iteration count for every pixel on the screen (about 500k). Each frame I'm iterating each pixel one step. Of course the performance isn't great if all 500k+ pixels are iterated, but it still runs somewhat "smooth" on my PC at about 15 fps. Of course the speed increases when the number of remaining "black" pixels decrease. Though since I do one iteration per visual frame it never gets faster than the visual fps.


Of course when dealing with pure rectangles one could calculate the min start and max end end index for a scanline based on the slopes of the rectangle edges. However keep in mind the unnecessary overhead of a square rectangle at 45° is just 1.414 times higher than the number of actual points you have to check. So in most cases it's not worth doing much additional calculations and edge cases. Of course if the rectangle could be really long and thin and is also rotated heavily (45° is the worst case), it might be worth it. Though as I mentioned already it highly depends on your actual point count.

Comment
Add comment · Show 2 · 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 Dreamerking007 · Feb 12, 2020 at 05:40 PM 0
Share

ok thanks for the information the rest of my game is pretty fast so i guess it can suffer a little bit from this

avatar image Dreamerking007 · Feb 18, 2020 at 06:04 PM 0
Share

i didn't fully understand all the terms you were using but after making my code i realized i did what you were mentioning in you're third paragraph

avatar image
0

Answer by Dreamerking007 · Feb 15, 2020 at 10:42 AM

so here is what i came up with the first function just uses a bounding box and then checks each pixel if it is inside the rotated rectangle

the second function is my attempt at removing the pixels that aren't in the rotated rectangle while still handling the ones that are in it the second one isn't completely accurate yet i think there is a rounding error somewhere.

both functions have been split in three so if there is a problem somewhere it made it alot easier to find

 using UnityEngine;
      
     public class rectangletest : MonoBehaviour
     {
         Vector2 tempVector;
         Vector2 tempVector2;
         int i = 0;
         float xOffset = 40;
         float yOffset = 30;
         int maxX;
         int maxY;
         float highY;
         float highX;
      
         int minX;
         int minY;
         float riseTop;
         float riseBottom;
         float currentriseTop;
         float currentriseBottom;
         float xToSplit;
         bool FirstVectorMaxY = false;
      
         float heldFloat;
         float testfloat;
      
         int islefttest = 0;
         int jaggedtest = 0;
      
         int islefttest2 = 0;
         int jaggedtest2 = 0;
      
         int islefttest3 = 0;
         int jaggedtest3 = 0;
      
         private void Start()
         {
             //loop through 360 degrees to make sure the function works in all angles
             for (int i = -180; i < 180; i++)
             {
                 islefttest = 0;
                 jaggedtest = 0;
                 islefttest2 = 0;
                 jaggedtest2 = 0;
                 islefttest3 = 0;
                 jaggedtest3 = 0;
                 testfloat = 0;
                 tempVector = Quaternion.AngleAxis(i, Vector3.forward) * new Vector2(xOffset, yOffset);
                 tempVector2 = Quaternion.AngleAxis(i, Vector3.forward) * new Vector2(xOffset, -yOffset);
      
                 maxY = Mathf.FloorToInt(Mathf.Max(Mathf.Abs(tempVector.y), Mathf.Abs(tempVector2.y)));
                 highY = Mathf.Max(Mathf.Abs(tempVector.y), Mathf.Abs(tempVector2.y));
                 minX = -Mathf.FloorToInt(Mathf.Max(Mathf.Abs(tempVector.x), Mathf.Abs(tempVector2.x)));
                 highX = Mathf.Max(Mathf.Abs(tempVector.x), Mathf.Abs(tempVector2.x));
                 minY = -maxY;
                 IsLeft();
                 Jagged();
      
                 if (jaggedtest != islefttest)
                 {
                     Debug.Log("current rotation: " + i + ", jagged: " + jaggedtest + ", isleft: " + islefttest);
                 }
      
                 if (jaggedtest2 != islefttest2)
                 {
                     Debug.Log("current rotation: " + i + ", jagged2: " + jaggedtest2 + ", isleft2: " + islefttest2);
                 }
      
                 if (jaggedtest3 != islefttest3)
                 {
                     Debug.Log("current rotation: " + i + ", jagged3: " + jaggedtest3 + ", isleft3: " + islefttest3);
                 }
             }
         }
      
         void IsLeft()
         {
             //xToSplit is used to split the function in three to make it easier to find problems
             if (Mathf.Abs(tempVector.y) > Mathf.Abs(tempVector2.y))
             {
                 xToSplit = -Mathf.Abs(tempVector.x);
             }
             else
             {
                 xToSplit = -Mathf.Abs(tempVector2.x);
             }
      
      
             for (int x = minX; x < Mathf.FloorToInt(xToSplit); x++)
             {
                 for (int y = minY; y < maxY; y++)
                 {
                     if (((tempVector2.x - tempVector.x) * (y - tempVector.y)) - ((x - tempVector.x) * (tempVector2.y - tempVector.y)) < 0 &&
                     ((-tempVector2.x + tempVector.x) * (y + tempVector.y)) - ((x + tempVector.x) * (-tempVector2.y + tempVector.y)) < 0 &&
                     ((-tempVector.x - tempVector2.x) * (y - tempVector2.y)) - ((x - tempVector2.x) * (-tempVector.y - tempVector2.y)) < 0 &&
                     ((tempVector.x + tempVector2.x) * (y + tempVector2.y)) - ((x + tempVector2.x) * (tempVector.y + tempVector2.y)) < 0)
                     {
                         //do stuff for (x, y) as well as (-x, -y)
                         islefttest++;
                     }
                 }
             }
      
             for (int x = Mathf.FloorToInt(xToSplit); x < 0; x++)
             {
                 for (int y = minY; y < maxY; y++)
                 {
                     if (((tempVector2.x - tempVector.x) * (y - tempVector.y)) - ((x - tempVector.x) * (tempVector2.y - tempVector.y)) < 0 &&
                     ((-tempVector2.x + tempVector.x) * (y + tempVector.y)) - ((x + tempVector.x) * (-tempVector2.y + tempVector.y)) < 0 &&
                     ((-tempVector.x - tempVector2.x) * (y - tempVector2.y)) - ((x - tempVector2.x) * (-tempVector.y - tempVector2.y)) < 0 &&
                     ((tempVector.x + tempVector2.x) * (y + tempVector2.y)) - ((x + tempVector2.x) * (tempVector.y + tempVector2.y)) < 0)
                     {
                         //do stuff for (x, y) as well as (-x, -y)
                         islefttest2++;
                     }
                 }
             }
      
             //x = 0
             for (int y = minY; y < maxY; y++)
             {
                 if (((tempVector2.x - tempVector.x) * (y - tempVector.y)) - ((-tempVector.x) * (tempVector2.y - tempVector.y)) < 0 &&
                 ((-tempVector2.x + tempVector.x) * (y + tempVector.y)) - ((tempVector.x) * (-tempVector2.y + tempVector.y)) < 0 &&
                 ((-tempVector.x - tempVector2.x) * (y - tempVector2.y)) - ((-tempVector2.x) * (-tempVector.y - tempVector2.y)) < 0 &&
                 ((tempVector.x + tempVector2.x) * (y + tempVector2.y)) - ((tempVector2.x) * (tempVector.y + tempVector2.y)) < 0)
                 {
                     //do stuff for (x, y)
                     islefttest3++;
                 }
             }
         }
      
         void Jagged()
         {
             //these if statements are used to find how much i need to go up or down each iteration
             if (Mathf.Abs(tempVector.y) > Mathf.Abs(tempVector2.y))
             {
                 FirstVectorMaxY = true;
                 xToSplit = -Mathf.Abs(tempVector.x);
                 if (tempVector2.x < 0)
                 {
                     //this means riseTop is line(tempvector2, -tempvector)
                     riseTop = Mathf.Abs(-tempVector.y - tempVector2.y) / Mathf.Abs(-tempVector.x - tempVector2.x);
                     //and riseBottom is line(tempvector2, tempvector)
                     riseBottom = Mathf.Abs(tempVector.y - tempVector2.y) / Mathf.Abs(tempVector.x - tempVector2.x);
                     currentriseTop = Mathf.Abs((tempVector2.x - Mathf.CeilToInt(tempVector2.x)) * riseTop) + tempVector2.y;
                     currentriseBottom = tempVector2.y - Mathf.Abs((tempVector2.x - Mathf.CeilToInt(tempVector2.x)) * riseBottom);
                 }
                 else
                 {
                     //this means riseTop is line(-tempvector2, tempvector)
                     riseTop = Mathf.Abs(tempVector.y + tempVector2.y) / Mathf.Abs(tempVector.x + tempVector2.x);
                     //and riseBottom is line(-tempvector2, -tempvector)
                     riseBottom = Mathf.Abs(-tempVector.y + tempVector2.y) / Mathf.Abs(-tempVector.x + tempVector2.x);
                     currentriseTop = Mathf.Abs((-tempVector2.x - Mathf.CeilToInt(-tempVector2.x)) * riseTop) + -tempVector2.y;
                     currentriseBottom = -tempVector2.y - Mathf.Abs((-tempVector2.x - Mathf.CeilToInt(-tempVector2.x)) * riseBottom);
                 }
             }
             else
             {
                 FirstVectorMaxY = false;
                 xToSplit = -Mathf.Abs(tempVector2.x);
                 if (tempVector.x < 0)
                 {
                     //this means riseBottom is line(tempvector, -tempvector2)
                     riseBottom = Mathf.Abs(-tempVector2.y - tempVector.y) / Mathf.Abs(-tempVector2.x - tempVector.x);
                     //and riseTop is line(tempvector, tempvector2)
                     riseTop = Mathf.Abs(tempVector2.y - tempVector.y) / Mathf.Abs(tempVector2.x - tempVector.x);
                     currentriseTop = Mathf.Abs((tempVector.x - Mathf.CeilToInt(tempVector.x)) * riseTop) + tempVector.y;
                     currentriseBottom = tempVector.y - Mathf.Abs((tempVector.x - Mathf.CeilToInt(tempVector.x)) * riseBottom);
                 }
                 else
                 {
                     //this means riseBottom is line(-tempvector, tempvector2)
                     riseBottom = Mathf.Abs(tempVector2.y + tempVector.y) / Mathf.Abs(tempVector2.x + tempVector.x);
                     //and riseTop is line(-tempvector, -tempvector2)
                     riseTop = Mathf.Abs(-tempVector2.y + tempVector.y) / Mathf.Abs(-tempVector2.x + tempVector.x);
                     currentriseTop = Mathf.Abs((-tempVector.x - Mathf.CeilToInt(-tempVector.x)) * riseTop) + -tempVector.y;
                     currentriseBottom = -tempVector.y - Mathf.Abs((-tempVector.x - Mathf.CeilToInt(-tempVector.x)) * riseBottom);
                 }
             }
      
             //i don't need to check if these pixels are inside th rotated rectangle because only the ones that are should be checked
             for (int x = minX; x < Mathf.FloorToInt(xToSplit); x++)
             {
                 for (int y = Mathf.Max(Mathf.CeilToInt(currentriseBottom), -maxY); y <= Mathf.Min(Mathf.FloorToInt(currentriseTop), maxY); y++)
                 {
                     //do stuff for (x, y) as well as (-x, -y)
                     //if (jaggedtest > 10000)
                     //{
                     //    Debug.Log("bottom: " + Mathf.CeilToInt(currentriseBottom) + ", Top: " + Mathf.FloorToInt(currentriseTop) + ", first half");
                     //    break;
                     //}
                     jaggedtest++;
                 }
                 currentriseTop += riseTop;
                 currentriseBottom -= riseBottom;
             }
      
             //this small section is used to switch the rise over run of the corner that hit the top or bottom of the bounding box
             heldFloat = Mathf.Abs(xToSplit - Mathf.FloorToInt(xToSplit));
      
             currentriseTop += riseTop * heldFloat;
             currentriseBottom -= riseBottom * heldFloat;
      
             if (FirstVectorMaxY)
             {
                 riseBottom = -riseTop;
             }
             else
             {
                 riseTop = -riseBottom;
             }
      
             for (int x = Mathf.FloorToInt(xToSplit); x < 0; x++)
             {
                 for (int y = Mathf.Max(Mathf.CeilToInt(currentriseBottom), -maxY); y <= Mathf.Min(Mathf.FloorToInt(currentriseTop), maxY); y++)
                 {
                     //do stuff for (x, y) as well as (-x, -y)
                     //if (jaggedtest > 10000)
                     //{
                     //    Debug.Log("bottom: " + Mathf.CeilToInt(currentriseBottom) + ", Top: " + Mathf.FloorToInt(currentriseTop) + ", second half");
                     //    break;
                     //}
                     jaggedtest2++;
                 }
                 currentriseTop += riseTop;
                 currentriseBottom -= riseBottom;
             }
      
             //for x = 0
             for (int y = Mathf.Max(Mathf.CeilToInt(currentriseBottom), -maxY); y <= Mathf.Min(Mathf.FloorToInt(currentriseTop), maxY); y++)
             {
                 //do stuff
                 //if (jaggedtest > 10000)
                 //{
                 //    Debug.Log("bottom: " + Mathf.CeilToInt(currentriseBottom) + ", Top: " + Mathf.FloorToInt(currentriseTop) + ", second half");
                 //    break;
                 //}
                 jaggedtest3++;
             }
         }
     }
  

if anyone can find where the second function goes wrong it would be greatly appreciated

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

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

123 People are following this question.

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

Related Questions

An array of bools with random true and false elements 2 Answers

Activating Bool Array in sequence. 2 Answers

What are the built-in array syntax rules in this example... 1 Answer

Any way to enabled all GameObjects in an Array List? (UnityScript) 1 Answer

How can I access collider's game objects? 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