- Home /
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.
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.
ok thanks for the information the rest of my game is pretty fast so i guess it can suffer a little bit from this
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
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