SHARE
TWEET

Untitled

a guest Jul 18th, 2019 86 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System.Linq;
  5.  
  6. public class Workingstuffout : MonoBehaviour
  7. {
  8.     public List<Vector2> shape1coords = new List<Vector2>();
  9.     public List<Vector2> shape2coords = new List<Vector2>();
  10.     public List<List<Vector2>> shape1rects = new List<List<Vector2>>();
  11.     public List<List<Vector2>> shape2rects = new List<List<Vector2>>();
  12.  
  13.     void Start()
  14.     {
  15.         shape1rects = DivideIntoRects(shape1coords);
  16.         shape2rects = DivideIntoRects(shape2coords);
  17.     }
  18.  
  19.     public List<List<Vector2>> DivideIntoRects(List<Vector2> shapecoords)
  20.     {
  21.         List<Vector2> coords = new List<Vector2>(shapecoords);
  22.         List<List<Vector2>> listofrects = new List<List<Vector2>>();
  23.  
  24.         while (coords.Count > 0)
  25.         {
  26.             Vector2[] sortingcoords = coords.ToArray();
  27.             sortingcoords.OrderBy(g => g.x);
  28.             sortingcoords.OrderByDescending(g => g.y);
  29.             coords = new List<Vector2>(sortingcoords.ToList());
  30.  
  31.             int lowerpoint = 0;
  32.             for (int i = 0; i < coords.Count; i += 1)
  33.             {
  34.                 if (i > 1)
  35.                 {
  36.                     if (coords[i].x == coords[0].x) { lowerpoint = i; break; }
  37.                     else if (coords[i].x == coords[1].x) { lowerpoint = i; break; }
  38.                 }
  39.             }
  40.             Debug.Log("Testing the rectangle: (" + coords[0].x + ", " + coords[0].y + "), (" + coords[1].x + ", " + coords[lowerpoint].y + ")");
  41.  
  42.             Vector2 highestoverlappingpoint = new Vector2(-1000, -1000);
  43.             for (int i = 0; i < coords.Count; i += 1)
  44.             {
  45.                 if (i != 0 && i != 1 && i != lowerpoint)
  46.                 {
  47.                     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)
  48.                     {
  49.                         if (highestoverlappingpoint.y == -1000 || coords[i].y > highestoverlappingpoint.y) { highestoverlappingpoint = new Vector2(coords[i].x, coords[i].y); }
  50.                     }
  51.                 }
  52.             }
  53.  
  54.             List<Vector2> newrect = new List<Vector2>();
  55.             if (highestoverlappingpoint.y == -1000)
  56.             {
  57.                 newrect.Add(coords[0]);
  58.                 newrect.Add(coords[1]);
  59.                 newrect.Add(new Vector2(coords[0].x, coords[lowerpoint].y));
  60.                 newrect.Add(new Vector2(coords[1].x, coords[lowerpoint].y));
  61.             }
  62.             else
  63.             {
  64.                 newrect.Add(coords[0]);
  65.                 newrect.Add(coords[1]);
  66.                 newrect.Add(new Vector2(coords[0].x, highestoverlappingpoint.y));
  67.                 newrect.Add(new Vector2(coords[1].x, highestoverlappingpoint.y));
  68.             }
  69.  
  70.             listofrects.Add(newrect);
  71.  
  72.             List<int> removethesecoords = new List<int>();
  73.             for (int i = 0; i < 4; i += 1)
  74.             {
  75.                 int matched = 0;
  76.                 for (int j = 0; j < coords.Count; j += 1)
  77.                 {
  78.                     if (newrect[i] == coords[j]) { removethesecoords.Add(j); matched += 1; }
  79.                 }
  80.                 if (matched > 0) { coords.Add(newrect[i]); }
  81.             }
  82.  
  83.             if (removethesecoords.Count > 0)
  84.             {
  85.                 for (int i = removethesecoords.Count - 1; i > -1; i -= 1) { coords.RemoveAt(removethesecoords[i]); }
  86.             }
  87.         }
  88.         return listofrects;
  89.     }
  90. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top