Hey everyone, unity noob with a decent amount of programming and game making experience.
I currently have a grid generator that generates a grid with a random angle within a polygon, in this case a square but could be any polygon that has no opening. I’ve chopped all the lines up but they intersections as you can see here:

So in order to turn each enclosed box into individual boundaries and repeat the process (think fractal) I’ve written code that goes crawls through a list of LineSegment (two Vectors, start and end, other stuff too), goes to one end of the line, finds other LineSegments that share that point, and picks the leftmost one. It repeats that process until it gets back to the original, thus creating an enclosed box. I’ve added ways of determining if the line has been used twice and the other lines that a line has shared boxes with to prevent repeats from happening.
This is my result:

(ignore the blue red and green)
Those are all enclosed boxes, but unfortunately my code is leaving out a lot of them.
After about 12 hours of fixing things that made the code better but didn’t fix this problem i’m thinking that my problem has to do with my DetermineMostLeft function.
I have another function that works well called GetAngle which returns a float that is the angle in radians. I didnt want to use included Unity functions because they’re overly complicated for my 2d grid. Here is that function:
static float GetAngle(Vector3 start, Vector3 end) {
float unfixedSlope = (start.z - end.z) / (start.x - end.x);
float angle = Mathf.Abs(Mathf.Atan(unfixedSlope));
if (angle == Mathf.PI / 2 || angle == 0) {
if (start.x == end.x) {
if (start.z > end.z) {
angle = Mathf.PI * 1.5f;
} else {
angle = Mathf.PI / 2;
}
} else if (start.z == end.z) {
if (start.x < end.x) {
angle = 0;
} else {
angle = Mathf.PI;
}
}
} else {
if ((start.x > end.x && start.z < end.z)) {
angle += Mathf.PI / 2;
} else if ((start.x > end.x && start.z > end.z)) {
angle += Mathf.PI;
} else if ((start.x < end.x && start.z > end.z)) {
angle += Mathf.PI * 1.5f;
} else {
}
}
//Debug.Log (angle * 57.2958f);
return angle;
}
So I’m confident that the GetAngle function works correctly as bad/verbose as it may be.
Here’s the DetermineMostLeft function that I’m not confident about. It returns the index of the linesegment in the iLines list. Here it is:
static int[] TempMostLeft(List<LineSegment> iLines, int[,] range, LineSegment currentLine, int startOrEnd)
{
List<IntFloat> angles = new List<IntFloat>();
Vector3 endPos;
float angle;
if (startOrEnd == 1) {
angle = GetAngle(currentLine.Start, currentLine.End);
endPos = currentLine.End;
} else
{
angle = GetAngle(currentLine.End, currentLine.Start);
endPos = currentLine.Start;
}
for (int i = 0; i < range.GetLength(0); i++)
{
float testAngle;
bool boo;
if (range[i, 1] == 0)
{
testAngle = GetAngle(iLines[range[i, 0]].Start, iLines[range[i, 0]].End);
boo = false;
} else
{
testAngle = GetAngle(iLines[range[i, 0]].End, iLines[range[i, 0]].Start);
boo = true;
}
float newAngle = testAngle - angle;
if (newAngle < 0f)
{
newAngle += Mathf.PI * 2;
}
angles.Add(new IntFloat(range[i, 0], newAngle, boo));
}
angles.Sort((x, y) => x.floating.CompareTo(y.floating));
Debug.Log("brrr");
int[] left = new int[2];
if (angles.Count - 1 < 0)
{
Debug.Log("test");
}
if (angles[angles.Count - 1].floating < Mathf.PI)
{
left[0] = angles[angles.Count - 1].integer;
if (angles[angles.Count - 1].boo == true)
{
left[1] = 0;
} else
{
left[1] = 1;
}
return left;
} else
{
for (int i = 0; i < angles.Count - 1; i++)
{
if (angles[i].floating < Mathf.PI && angles[i + 1].floating >= Mathf.PI && angles[i].floating != 0f)
{
left[0] = angles[i].integer;
if (endPos == iLines[angles[i].integer].Start)
{
left[1] = 1;
} else
{
left[1] = 0;
}
return left;
}
}
}
Debug.Log("no left turn");
left[0] = -1;
left[1] = -1;
return left;
}
Any thoughts? I know this is a very loose question but I’m so stumped and having spent like 60 hours writing this fractal generator I really want it to work!
PS: I use Vector3s to represent 2d because I like to use the y variable to encode other information for failures etc. like .y == 1f means something is wrong.
PPS: I know I should have put the two points for a line in an array as opposed to .start or .end. I screwed that up awhile ago and its too late lol