Polygon.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. // -----------------------------------------------------------------------
  2. // <copyright file="Polygon.cs" company="">
  3. // Triangle.NET code by Christian Woltering, http://triangle.codeplex.com/
  4. // </copyright>
  5. // -----------------------------------------------------------------------
  6. namespace UnityEngine.U2D.Animation.TriangleNet
  7. .Geometry
  8. {
  9. using System;
  10. using System.Linq;
  11. using System.Collections.Generic;
  12. /// <summary>
  13. /// A polygon represented as a planar straight line graph.
  14. /// </summary>
  15. internal class Polygon : IPolygon
  16. {
  17. List<Vertex> points;
  18. List<Point> holes;
  19. List<RegionPointer> regions;
  20. List<ISegment> segments;
  21. /// <inheritdoc />
  22. public List<Vertex> Points
  23. {
  24. get { return points; }
  25. }
  26. /// <inheritdoc />
  27. public List<Point> Holes
  28. {
  29. get { return holes; }
  30. }
  31. /// <inheritdoc />
  32. public List<RegionPointer> Regions
  33. {
  34. get { return regions; }
  35. }
  36. /// <inheritdoc />
  37. public List<ISegment> Segments
  38. {
  39. get { return segments; }
  40. }
  41. /// <inheritdoc />
  42. public bool HasPointMarkers { get; set; }
  43. /// <inheritdoc />
  44. public bool HasSegmentMarkers { get; set; }
  45. /// <inheritdoc />
  46. public int Count
  47. {
  48. get { return points.Count; }
  49. }
  50. /// <summary>
  51. /// Initializes a new instance of the <see cref="Polygon" /> class.
  52. /// </summary>
  53. public Polygon()
  54. : this(3, false)
  55. {
  56. }
  57. /// <summary>
  58. /// Initializes a new instance of the <see cref="Polygon" /> class.
  59. /// </summary>
  60. /// <param name="capacity">The default capacity for the points list.</param>
  61. public Polygon(int capacity)
  62. : this(3, false)
  63. {
  64. }
  65. /// <summary>
  66. /// Initializes a new instance of the <see cref="Polygon" /> class.
  67. /// </summary>
  68. /// <param name="capacity">The default capacity for the points list.</param>
  69. /// <param name="markers">Use point and segment markers.</param>
  70. public Polygon(int capacity, bool markers)
  71. {
  72. points = new List<Vertex>(capacity);
  73. holes = new List<Point>();
  74. regions = new List<RegionPointer>();
  75. segments = new List<ISegment>();
  76. HasPointMarkers = markers;
  77. HasSegmentMarkers = markers;
  78. }
  79. [Obsolete("Use polygon.Add(contour) method instead.")]
  80. public void AddContour(IEnumerable<Vertex> points, int marker = 0,
  81. bool hole = false, bool convex = false)
  82. {
  83. this.Add(new Contour(points, marker, convex), hole);
  84. }
  85. [Obsolete("Use polygon.Add(contour) method instead.")]
  86. public void AddContour(IEnumerable<Vertex> points, int marker, Point hole)
  87. {
  88. this.Add(new Contour(points, marker), hole);
  89. }
  90. /// <inheritdoc />
  91. public Rectangle Bounds()
  92. {
  93. var bounds = new Rectangle();
  94. bounds.Expand(this.points.Cast<Point>());
  95. return bounds;
  96. }
  97. /// <summary>
  98. /// Add a vertex to the polygon.
  99. /// </summary>
  100. /// <param name="vertex">The vertex to insert.</param>
  101. public void Add(Vertex vertex)
  102. {
  103. this.points.Add(vertex);
  104. }
  105. /// <summary>
  106. /// Add a segment to the polygon.
  107. /// </summary>
  108. /// <param name="segment">The segment to insert.</param>
  109. /// <param name="insert">If true, both endpoints will be added to the points list.</param>
  110. public void Add(ISegment segment, bool insert = false)
  111. {
  112. this.segments.Add(segment);
  113. if (insert)
  114. {
  115. this.points.Add(segment.GetVertex(0));
  116. this.points.Add(segment.GetVertex(1));
  117. }
  118. }
  119. /// <summary>
  120. /// Add a segment to the polygon.
  121. /// </summary>
  122. /// <param name="segment">The segment to insert.</param>
  123. /// <param name="index">The index of the segment endpoint to add to the points list (must be 0 or 1).</param>
  124. public void Add(ISegment segment, int index)
  125. {
  126. this.segments.Add(segment);
  127. this.points.Add(segment.GetVertex(index));
  128. }
  129. /// <summary>
  130. /// Add a contour to the polygon.
  131. /// </summary>
  132. /// <param name="contour">The contour to insert.</param>
  133. /// <param name="hole">Treat contour as a hole.</param>
  134. public void Add(Contour contour, bool hole = false)
  135. {
  136. if (hole)
  137. {
  138. this.Add(contour, contour.FindInteriorPoint());
  139. }
  140. else
  141. {
  142. this.points.AddRange(contour.Points);
  143. this.segments.AddRange(contour.GetSegments());
  144. }
  145. }
  146. /// <summary>
  147. /// Add a contour to the polygon.
  148. /// </summary>
  149. /// <param name="contour">The contour to insert.</param>
  150. /// <param name="hole">Point inside the contour, making it a hole.</param>
  151. public void Add(Contour contour, Point hole)
  152. {
  153. this.points.AddRange(contour.Points);
  154. this.segments.AddRange(contour.GetSegments());
  155. this.holes.Add(hole);
  156. }
  157. }
  158. }