// -----------------------------------------------------------------------
//
// Triangle.NET code by Christian Woltering, http://triangle.codeplex.com/
//
// -----------------------------------------------------------------------
namespace UnityEngine.U2D.Animation.TriangleNet
.Geometry
{
using System;
using System.Linq;
using System.Collections.Generic;
///
/// A polygon represented as a planar straight line graph.
///
internal class Polygon : IPolygon
{
List points;
List holes;
List regions;
List segments;
///
public List Points
{
get { return points; }
}
///
public List Holes
{
get { return holes; }
}
///
public List Regions
{
get { return regions; }
}
///
public List Segments
{
get { return segments; }
}
///
public bool HasPointMarkers { get; set; }
///
public bool HasSegmentMarkers { get; set; }
///
public int Count
{
get { return points.Count; }
}
///
/// Initializes a new instance of the class.
///
public Polygon()
: this(3, false)
{
}
///
/// Initializes a new instance of the class.
///
/// The default capacity for the points list.
public Polygon(int capacity)
: this(3, false)
{
}
///
/// Initializes a new instance of the class.
///
/// The default capacity for the points list.
/// Use point and segment markers.
public Polygon(int capacity, bool markers)
{
points = new List(capacity);
holes = new List();
regions = new List();
segments = new List();
HasPointMarkers = markers;
HasSegmentMarkers = markers;
}
[Obsolete("Use polygon.Add(contour) method instead.")]
public void AddContour(IEnumerable points, int marker = 0,
bool hole = false, bool convex = false)
{
this.Add(new Contour(points, marker, convex), hole);
}
[Obsolete("Use polygon.Add(contour) method instead.")]
public void AddContour(IEnumerable points, int marker, Point hole)
{
this.Add(new Contour(points, marker), hole);
}
///
public Rectangle Bounds()
{
var bounds = new Rectangle();
bounds.Expand(this.points.Cast());
return bounds;
}
///
/// Add a vertex to the polygon.
///
/// The vertex to insert.
public void Add(Vertex vertex)
{
this.points.Add(vertex);
}
///
/// Add a segment to the polygon.
///
/// The segment to insert.
/// If true, both endpoints will be added to the points list.
public void Add(ISegment segment, bool insert = false)
{
this.segments.Add(segment);
if (insert)
{
this.points.Add(segment.GetVertex(0));
this.points.Add(segment.GetVertex(1));
}
}
///
/// Add a segment to the polygon.
///
/// The segment to insert.
/// The index of the segment endpoint to add to the points list (must be 0 or 1).
public void Add(ISegment segment, int index)
{
this.segments.Add(segment);
this.points.Add(segment.GetVertex(index));
}
///
/// Add a contour to the polygon.
///
/// The contour to insert.
/// Treat contour as a hole.
public void Add(Contour contour, bool hole = false)
{
if (hole)
{
this.Add(contour, contour.FindInteriorPoint());
}
else
{
this.points.AddRange(contour.Points);
this.segments.AddRange(contour.GetSegments());
}
}
///
/// Add a contour to the polygon.
///
/// The contour to insert.
/// Point inside the contour, making it a hole.
public void Add(Contour contour, Point hole)
{
this.points.AddRange(contour.Points);
this.segments.AddRange(contour.GetSegments());
this.holes.Add(hole);
}
}
}