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