Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using System.Linq;
- public class Workingstuffout : MonoBehaviour
- {
- public List<Vector2> shape1coords = new List<Vector2>();
- public List<Vector2> shape2coords = new List<Vector2>();
- public List<List<Vector2>> shape1rects = new List<List<Vector2>>();
- public List<List<Vector2>> shape2rects = new List<List<Vector2>>();
- void Start()
- {
- shape1rects = DivideIntoRects(shape1coords);
- shape2rects = DivideIntoRects(shape2coords);
- }
- public List<List<Vector2>> DivideIntoRects(List<Vector2> shapecoords)
- {
- List<Vector2> coords = new List<Vector2>(shapecoords);
- List<List<Vector2>> listofrects = new List<List<Vector2>>();
- while (coords.Count > 0)
- {
- Vector2[] sortingcoords = coords.ToArray();
- sortingcoords.OrderBy(g => g.x);
- sortingcoords.OrderByDescending(g => g.y);
- coords = new List<Vector2>(sortingcoords.ToList());
- int lowerpoint = 0;
- for (int i = 0; i < coords.Count; i += 1)
- {
- if (i > 1)
- {
- if (coords[i].x == coords[0].x) { lowerpoint = i; break; }
- else if (coords[i].x == coords[1].x) { lowerpoint = i; break; }
- }
- }
- Debug.Log("Testing the rectangle: (" + coords[0].x + ", " + coords[0].y + "), (" + coords[1].x + ", " + coords[lowerpoint].y + ")");
- Vector2 highestoverlappingpoint = new Vector2(-1000, -1000);
- for (int i = 0; i < coords.Count; i += 1)
- {
- if (i != 0 && i != 1 && i != lowerpoint)
- {
- if (coords[i].x > coords[0].x && coords[i].x < coords[1].x && coords[i].y > coords[lowerpoint].y && coords[i].y < coords[0].y)
- {
- if (highestoverlappingpoint.y == -1000 || coords[i].y > highestoverlappingpoint.y) { highestoverlappingpoint = new Vector2(coords[i].x, coords[i].y); }
- }
- }
- }
- List<Vector2> newrect = new List<Vector2>();
- if (highestoverlappingpoint.y == -1000)
- {
- newrect.Add(coords[0]);
- newrect.Add(coords[1]);
- newrect.Add(new Vector2(coords[0].x, coords[lowerpoint].y));
- newrect.Add(new Vector2(coords[1].x, coords[lowerpoint].y));
- }
- else
- {
- newrect.Add(coords[0]);
- newrect.Add(coords[1]);
- newrect.Add(new Vector2(coords[0].x, highestoverlappingpoint.y));
- newrect.Add(new Vector2(coords[1].x, highestoverlappingpoint.y));
- }
- listofrects.Add(newrect);
- List<int> removethesecoords = new List<int>();
- for (int i = 0; i < 4; i += 1)
- {
- int matched = 0;
- for (int j = 0; j < coords.Count; j += 1)
- {
- if (newrect[i] == coords[j]) { removethesecoords.Add(j); matched += 1; }
- }
- if (matched > 0) { coords.Add(newrect[i]); }
- }
- if (removethesecoords.Count > 0)
- {
- for (int i = removethesecoords.Count - 1; i > -1; i -= 1) { coords.RemoveAt(removethesecoords[i]); }
- }
- }
- return listofrects;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement