// shrink toward center by a factor public static Section Shrink(Section section, float p) { Section nsection = new Section(); Vector2 c = PolyTool.Centroid(ToPoly(section)); //Debug.Log (c); for (int i = 0; i < section.Count; i++) { Edge nedge = new Edge(); for (int j = 0; j < section[i].Count; j++) { Vector2 pt = section [i] [j]; Vector2 npt = Vector2.Lerp(pt, c, p); nedge.Add(npt); } nsection.Add(nedge); } return(nsection); }
// return a list of small rectangular sections bounded by given section public static List <Section> GridSection(Section section) { float angr = Mathf.PI * 2 * Random.value; Section nsection = Rotate(section, new Vector2(0, 0), angr); nsection = ShrinkFixed(nsection, 3f); Polygon poly = ToPoly(nsection); float[] bd = Bound(nsection); List <Section> units = new List <Section>(); List <List <int> > mtx = new List <List <int> > (); float uw = 2; float mtxsize = 0; for (float y = bd [1]; y < bd [3]; y += uw) { List <int> row = new List <int> (); for (float x = bd [0]; x < bd [2]; x += uw) { if (PolyTool.ContainsPt(poly, new Vector2(x, y)) && PolyTool.ContainsPt(poly, new Vector2(x + uw, y + uw)) && PolyTool.ContainsPt(poly, new Vector2(x, y + uw)) && PolyTool.ContainsPt(poly, new Vector2(x + uw, y))) { row.Add(0); } else { row.Add(-1); } mtxsize += 1; } mtx.Add(row); } List <int[]> bdims = new List <int[]> (); for (int i = 0; i < mtxsize * 3; i++) { int se = (int)Mathf.Floor(0.5f * Mathf.Min((bd[2] - bd[0]) / uw, (bd[3] - bd[1]) / uw)); int w = Random.Range(1, se); int h = Random.Range(Mathf.Max(w - 2, 1), Mathf.Min(w + 2, se)); int x0 = Random.Range(0, mtx [0].Count - 1); int y0 = Random.Range(0, mtx.Count - 1); bool islegal = true; for (int x = x0; x < x0 + w; x++) { for (int y = y0; y < y0 + h; y++) { try{ if (mtx[y][x] != 0) { islegal = false; break; } }catch (System.ArgumentOutOfRangeException e) { System.Action <System.ArgumentOutOfRangeException> f = delegate(System.ArgumentOutOfRangeException e1) { }; f(e); islegal = false; break; } } } if (islegal) { for (int x = x0; x < x0 + w; x++) { for (int y = y0; y < y0 + h; y++) { mtx [y] [x] = 1; } } bdims.Add(new int[] { x0, y0, w, h }); } } for (int i = 0; i < bdims.Count; i++) { float x = bd[0] + bdims [i] [0] * uw; float y = bd[1] + bdims [i] [1] * uw; float w = bdims [i] [2] * uw; float h = bdims [i] [3] * uw; Section nu = Box(x, y, w, h); Section nur = Rotate(nu, new Vector2(0, 0), -angr); units.Add(Shrink(nur, 0.1f)); } return(units); }
// estimate area using polygon area algorithm public static float EstArea(Section section) { return(PolyTool.Area(ToPoly(section))); }