Polygon.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Microsoft.Xna.Framework;
  5. namespace MonoGame.Extended.Shapes
  6. {
  7. public class Polygon : IEquatable<Polygon>
  8. {
  9. public Polygon(IEnumerable<Vector2> vertices)
  10. {
  11. _localVertices = vertices.ToArray();
  12. _transformedVertices = _localVertices;
  13. _offset = Vector2.Zero;
  14. _rotation = 0;
  15. _scale = Vector2.One;
  16. _isDirty = false;
  17. }
  18. private readonly Vector2[] _localVertices;
  19. private Vector2[] _transformedVertices;
  20. private Vector2 _offset;
  21. private float _rotation;
  22. private Vector2 _scale;
  23. private bool _isDirty;
  24. public Vector2[] Vertices
  25. {
  26. get
  27. {
  28. if (_isDirty)
  29. {
  30. _transformedVertices = GetTransformedVertices();
  31. _isDirty = false;
  32. }
  33. return _transformedVertices;
  34. }
  35. }
  36. public float Left
  37. {
  38. get { return Vertices.Min(v => v.X); }
  39. }
  40. public float Right
  41. {
  42. get { return Vertices.Max(v => v.X); }
  43. }
  44. public float Top
  45. {
  46. get { return Vertices.Min(v => v.Y); }
  47. }
  48. public float Bottom
  49. {
  50. get { return Vertices.Max(v => v.Y); }
  51. }
  52. public void Offset(Vector2 amount)
  53. {
  54. _offset += amount;
  55. _isDirty = true;
  56. }
  57. public void Rotate(float amount)
  58. {
  59. _rotation += amount;
  60. _isDirty = true;
  61. }
  62. public void Scale(Vector2 amount)
  63. {
  64. _scale += amount;
  65. _isDirty = true;
  66. }
  67. private Vector2[] GetTransformedVertices()
  68. {
  69. var newVertices = new Vector2[_localVertices.Length];
  70. var isScaled = _scale != Vector2.One;
  71. for (var i = 0; i < _localVertices.Length; i++)
  72. {
  73. var p = _localVertices[i];
  74. if (isScaled)
  75. p *= _scale;
  76. // ReSharper disable once CompareOfFloatsByEqualityOperator
  77. if (_rotation != 0)
  78. {
  79. var cos = (float) Math.Cos(_rotation);
  80. var sin = (float) Math.Sin(_rotation);
  81. p = new Vector2(cos*p.X - sin*p.Y, sin*p.X + cos*p.Y);
  82. }
  83. newVertices[i] = p + _offset;
  84. }
  85. return newVertices;
  86. }
  87. public Polygon TransformedCopy(Vector2 offset, float rotation, Vector2 scale)
  88. {
  89. var polygon = new Polygon(_localVertices);
  90. polygon.Offset(offset);
  91. polygon.Rotate(rotation);
  92. polygon.Scale(scale - Vector2.One);
  93. return new Polygon(polygon.Vertices);
  94. }
  95. public RectangleF BoundingRectangle
  96. {
  97. get
  98. {
  99. var minX = Left;
  100. var minY = Top;
  101. var maxX = Right;
  102. var maxY = Bottom;
  103. return new RectangleF(minX, minY, maxX - minX, maxY - minY);
  104. }
  105. }
  106. public bool Contains(Vector2 point)
  107. {
  108. return Contains(point.X, point.Y);
  109. }
  110. public bool Contains(float x, float y)
  111. {
  112. var intersects = 0;
  113. var vertices = Vertices;
  114. for (var i = 0; i < vertices.Length; i++)
  115. {
  116. var x1 = vertices[i].X;
  117. var y1 = vertices[i].Y;
  118. var x2 = vertices[(i + 1)%vertices.Length].X;
  119. var y2 = vertices[(i + 1)%vertices.Length].Y;
  120. if ((((y1 <= y) && (y < y2)) || ((y2 <= y) && (y < y1))) && (x < (x2 - x1)/(y2 - y1)*(y - y1) + x1))
  121. intersects++;
  122. }
  123. return (intersects & 1) == 1;
  124. }
  125. public static bool operator ==(Polygon a, Polygon b)
  126. {
  127. return a.Equals(b);
  128. }
  129. public static bool operator !=(Polygon a, Polygon b)
  130. {
  131. return !(a == b);
  132. }
  133. public override bool Equals(object obj)
  134. {
  135. if (ReferenceEquals(null, obj)) return false;
  136. return obj is Polygon && Equals((Polygon) obj);
  137. }
  138. public bool Equals(Polygon other)
  139. {
  140. return Vertices.SequenceEqual(other.Vertices);
  141. }
  142. public override int GetHashCode()
  143. {
  144. unchecked
  145. {
  146. return Vertices.Aggregate(27, (current, v) => current + 13*current + v.GetHashCode());
  147. }
  148. }
  149. }
  150. }