ShapeExtensions.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. using System;
  2. using System.Collections.Generic;
  3. using Microsoft.Xna.Framework;
  4. using Microsoft.Xna.Framework.Graphics;
  5. using MonoGame.Extended.Shapes;
  6. namespace MonoGame.Extended
  7. {
  8. /// <summary>
  9. /// Sprite batch extensions for drawing primitive shapes
  10. /// </summary>
  11. public static class ShapeExtensions
  12. {
  13. private static Texture2D _whitePixelTexture;
  14. private static Texture2D GetTexture(SpriteBatch spriteBatch)
  15. {
  16. if (_whitePixelTexture == null || _whitePixelTexture.IsDisposed)
  17. {
  18. _whitePixelTexture = new Texture2D(spriteBatch.GraphicsDevice, 1, 1, false, SurfaceFormat.Color);
  19. _whitePixelTexture.SetData(new[] { Color.White });
  20. spriteBatch.Disposing += (sender, args) =>
  21. {
  22. _whitePixelTexture?.Dispose();
  23. _whitePixelTexture = null;
  24. };
  25. }
  26. return _whitePixelTexture;
  27. }
  28. /// <summary>
  29. /// Draws a closed polygon from a <see cref="Polygon" /> shape
  30. /// </summary>
  31. /// <param name="spriteBatch">The destination drawing surface</param>
  32. /// ///
  33. /// <param name="position">Where to position the polygon</param>
  34. /// <param name="polygon">The polygon to draw</param>
  35. /// <param name="color">The color to use</param>
  36. /// <param name="thickness">The thickness of the lines</param>
  37. /// /// <param name="layerDepth">The depth of the layer of this shape</param>
  38. public static void DrawPolygon(this SpriteBatch spriteBatch, Vector2 position, Polygon polygon, Color color, float thickness = 1f, float layerDepth = 0)
  39. {
  40. DrawPolygon(spriteBatch, position, polygon.Vertices, color, thickness, layerDepth);
  41. }
  42. /// <summary>
  43. /// Draws a closed polygon from an array of points
  44. /// </summary>
  45. /// <param name="spriteBatch">The destination drawing surface</param>
  46. /// ///
  47. /// <param name="offset">Where to offset the points</param>
  48. /// <param name="points">The points to connect with lines</param>
  49. /// <param name="color">The color to use</param>
  50. /// <param name="thickness">The thickness of the lines</param>
  51. /// <param name="layerDepth">The depth of the layer of this shape</param>
  52. public static void DrawPolygon(this SpriteBatch spriteBatch, Vector2 offset, IReadOnlyList<Vector2> points, Color color, float thickness = 1f, float layerDepth = 0)
  53. {
  54. if (points.Count == 0)
  55. return;
  56. if (points.Count == 1)
  57. {
  58. DrawPoint(spriteBatch, points[0], color, (int)thickness);
  59. return;
  60. }
  61. var texture = GetTexture(spriteBatch);
  62. for (var i = 0; i < points.Count - 1; i++)
  63. DrawPolygonEdge(spriteBatch, texture, points[i] + offset, points[i + 1] + offset, color, thickness, layerDepth);
  64. DrawPolygonEdge(spriteBatch, texture, points[points.Count - 1] + offset, points[0] + offset, color, thickness, layerDepth);
  65. }
  66. private static void DrawPolygonEdge(SpriteBatch spriteBatch, Texture2D texture, Vector2 point1, Vector2 point2, Color color, float thickness, float layerDepth)
  67. {
  68. var length = Vector2.Distance(point1, point2);
  69. var angle = (float)Math.Atan2(point2.Y - point1.Y, point2.X - point1.X);
  70. var scale = new Vector2(length, thickness);
  71. spriteBatch.Draw(texture, point1, null, color, angle, Vector2.Zero, scale, SpriteEffects.None, layerDepth);
  72. }
  73. /// <summary>
  74. /// Draws a filled rectangle
  75. /// </summary>
  76. /// <param name="spriteBatch">The destination drawing surface</param>
  77. /// <param name="rectangle">The rectangle to draw</param>
  78. /// <param name="color">The color to draw the rectangle in</param>
  79. /// <param name="layerDepth">The depth of the layer of this shape</param>
  80. public static void FillRectangle(this SpriteBatch spriteBatch, RectangleF rectangle, Color color, float layerDepth = 0)
  81. {
  82. FillRectangle(spriteBatch, rectangle.Position, rectangle.Size, color, layerDepth);
  83. }
  84. /// <summary>
  85. /// Draws a filled rectangle
  86. /// </summary>
  87. /// <param name="spriteBatch">The destination drawing surface</param>
  88. /// <param name="location">Where to draw</param>
  89. /// <param name="size">The size of the rectangle</param>
  90. /// <param name="color">The color to draw the rectangle in</param>
  91. /// <param name="layerDepth">The depth of the layer of this shape</param>
  92. public static void FillRectangle(this SpriteBatch spriteBatch, Vector2 location, SizeF size, Color color, float layerDepth = 0)
  93. {
  94. spriteBatch.Draw(GetTexture(spriteBatch), location, null, color, 0, Vector2.Zero, size, SpriteEffects.None, layerDepth);
  95. }
  96. /// <summary>
  97. /// Draws a filled rectangle
  98. /// </summary>
  99. /// <param name="spriteBatch">The destination drawing surface</param>
  100. /// <param name="x">The X coord of the left side</param>
  101. /// <param name="y">The Y coord of the upper side</param>
  102. /// <param name="width">Width</param>
  103. /// <param name="height">Height</param>
  104. /// <param name="color">The color to draw the rectangle in</param>
  105. /// <param name="layerDepth">The depth of the layer of this shape</param>
  106. public static void FillRectangle(this SpriteBatch spriteBatch, float x, float y, float width, float height, Color color, float layerDepth = 0)
  107. {
  108. FillRectangle(spriteBatch, new Vector2(x, y), new SizeF(width, height), color, layerDepth);
  109. }
  110. /// <summary>
  111. /// Draws a rectangle with the thickness provided
  112. /// </summary>
  113. /// <param name="spriteBatch">The destination drawing surface</param>
  114. /// <param name="rectangle">The rectangle to draw</param>
  115. /// <param name="color">The color to draw the rectangle in</param>
  116. /// <param name="thickness">The thickness of the lines</param>
  117. /// <param name="layerDepth">The depth of the layer of this shape</param>
  118. public static void DrawRectangle(this SpriteBatch spriteBatch, RectangleF rectangle, Color color, float thickness = 1f, float layerDepth = 0)
  119. {
  120. var texture = GetTexture(spriteBatch);
  121. var topLeft = new Vector2(rectangle.X, rectangle.Y);
  122. var topRight = new Vector2(rectangle.Right - thickness, rectangle.Y);
  123. var bottomLeft = new Vector2(rectangle.X, rectangle.Bottom - thickness);
  124. var horizontalScale = new Vector2(rectangle.Width, thickness);
  125. var verticalScale = new Vector2(thickness, rectangle.Height);
  126. spriteBatch.Draw(texture, topLeft, null, color, 0f, Vector2.Zero, horizontalScale, SpriteEffects.None, layerDepth);
  127. spriteBatch.Draw(texture, topLeft, null, color, 0f, Vector2.Zero, verticalScale, SpriteEffects.None, layerDepth);
  128. spriteBatch.Draw(texture, topRight, null, color, 0f, Vector2.Zero, verticalScale, SpriteEffects.None, layerDepth);
  129. spriteBatch.Draw(texture, bottomLeft, null, color, 0f, Vector2.Zero, horizontalScale, SpriteEffects.None, layerDepth);
  130. }
  131. /// <summary>
  132. /// Draws a rectangle with the thickness provided
  133. /// </summary>
  134. /// <param name="spriteBatch">The destination drawing surface</param>
  135. /// <param name="location">Where to draw</param>
  136. /// <param name="size">The size of the rectangle</param>
  137. /// <param name="color">The color to draw the rectangle in</param>
  138. /// <param name="thickness">The thickness of the line</param>
  139. /// <param name="layerDepth">The depth of the layer of this shape</param>
  140. public static void DrawRectangle(this SpriteBatch spriteBatch, Vector2 location, SizeF size, Color color, float thickness = 1f, float layerDepth = 0)
  141. {
  142. DrawRectangle(spriteBatch, new RectangleF(location.X, location.Y, size.Width, size.Height), color, thickness, layerDepth);
  143. }
  144. /// <summary>
  145. /// Draws a rectangle outline.
  146. /// </summary>
  147. public static void DrawRectangle(this SpriteBatch spriteBatch, float x, float y, float width, float height, Color color, float thickness = 1f, float layerDepth = 0)
  148. {
  149. DrawRectangle(spriteBatch, new RectangleF(x, y, width, height), color, thickness, layerDepth);
  150. }
  151. /// <summary>
  152. /// Draws a line from point1 to point2 with an offset
  153. /// </summary>
  154. /// <param name="spriteBatch">The destination drawing surface</param>
  155. /// <param name="x1">The X coord of the first point</param>
  156. /// <param name="y1">The Y coord of the first point</param>
  157. /// <param name="x2">The X coord of the second point</param>
  158. /// <param name="y2">The Y coord of the second point</param>
  159. /// <param name="color">The color to use</param>
  160. /// <param name="thickness">The thickness of the line</param>
  161. /// <param name="layerDepth">The depth of the layer of this shape</param>
  162. public static void DrawLine(this SpriteBatch spriteBatch, float x1, float y1, float x2, float y2, Color color, float thickness = 1f, float layerDepth = 0)
  163. {
  164. DrawLine(spriteBatch, new Vector2(x1, y1), new Vector2(x2, y2), color, thickness, layerDepth);
  165. }
  166. /// <summary>
  167. /// Draws a line from point1 to point2 with an offset
  168. /// </summary>
  169. /// <param name="spriteBatch">The destination drawing surface</param>
  170. /// <param name="point1">The first point</param>
  171. /// <param name="point2">The second point</param>
  172. /// <param name="color">The color to use</param>
  173. /// <param name="thickness">The thickness of the line</param>
  174. /// <param name="layerDepth">The depth of the layer of this shape</param>
  175. public static void DrawLine(this SpriteBatch spriteBatch, Vector2 point1, Vector2 point2, Color color, float thickness = 1f, float layerDepth = 0)
  176. {
  177. // calculate the distance between the two vectors
  178. var distance = Vector2.Distance(point1, point2);
  179. // calculate the angle between the two vectors
  180. var angle = (float)Math.Atan2(point2.Y - point1.Y, point2.X - point1.X);
  181. DrawLine(spriteBatch, point1, distance, angle, color, thickness, layerDepth);
  182. }
  183. /// <summary>
  184. /// Draws a line from point1 to point2 with an offset
  185. /// </summary>
  186. /// <param name="spriteBatch">The destination drawing surface</param>
  187. /// <param name="point">The starting point</param>
  188. /// <param name="length">The length of the line</param>
  189. /// <param name="angle">The angle of this line from the starting point</param>
  190. /// <param name="color">The color to use</param>
  191. /// <param name="thickness">The thickness of the line</param>
  192. /// <param name="layerDepth">The depth of the layer of this shape</param>
  193. public static void DrawLine(this SpriteBatch spriteBatch, Vector2 point, float length, float angle, Color color, float thickness = 1f, float layerDepth = 0)
  194. {
  195. var origin = new Vector2(0f, 0.5f);
  196. var scale = new Vector2(length, thickness);
  197. spriteBatch.Draw(GetTexture(spriteBatch), point, null, color, angle, origin, scale, SpriteEffects.None, layerDepth);
  198. }
  199. /// <summary>
  200. /// Draws a point at the specified x, y position. The center of the point will be at the position.
  201. /// </summary>
  202. public static void DrawPoint(this SpriteBatch spriteBatch, float x, float y, Color color, float size = 1f, float layerDepth = 0)
  203. {
  204. DrawPoint(spriteBatch, new Vector2(x, y), color, size, layerDepth);
  205. }
  206. /// <summary>
  207. /// Draws a point at the specified position. The center of the point will be at the position.
  208. /// </summary>
  209. public static void DrawPoint(this SpriteBatch spriteBatch, Vector2 position, Color color, float size = 1f, float layerDepth = 0)
  210. {
  211. var scale = Vector2.One * size;
  212. var offset = new Vector2(0.5f) - new Vector2(size * 0.5f);
  213. spriteBatch.Draw(GetTexture(spriteBatch), position + offset, null, color, 0f, Vector2.Zero, scale, SpriteEffects.None, layerDepth);
  214. }
  215. /// <summary>
  216. /// Draw a circle from a <see cref="CircleF" /> shape
  217. /// </summary>
  218. /// <param name="spriteBatch">The destination drawing surface</param>
  219. /// <param name="circle">The circle shape to draw</param>
  220. /// <param name="sides">The number of sides to generate</param>
  221. /// <param name="color">The color of the circle</param>
  222. /// <param name="thickness">The thickness of the lines used</param>
  223. /// <param name="layerDepth">The depth of the layer of this shape</param>
  224. public static void DrawCircle(this SpriteBatch spriteBatch, CircleF circle, int sides, Color color, float thickness = 1f, float layerDepth = 0)
  225. {
  226. DrawCircle(spriteBatch, circle.Center, circle.Radius, sides, color, thickness, layerDepth);
  227. }
  228. /// <summary>
  229. /// Draw a circle
  230. /// </summary>
  231. /// <param name="spriteBatch">The destination drawing surface</param>
  232. /// <param name="center">The center of the circle</param>
  233. /// <param name="radius">The radius of the circle</param>
  234. /// <param name="sides">The number of sides to generate</param>
  235. /// <param name="color">The color of the circle</param>
  236. /// <param name="thickness">The thickness of the lines used</param>
  237. /// <param name="layerDepth">The depth of the layer of this shape</param>
  238. public static void DrawCircle(this SpriteBatch spriteBatch, Vector2 center, float radius, int sides, Color color, float thickness = 1f, float layerDepth = 0)
  239. {
  240. DrawPolygon(spriteBatch, center, CreateCircle(radius, sides), color, thickness, layerDepth);
  241. }
  242. /// <summary>
  243. /// Draw a circle
  244. /// </summary>
  245. /// <param name="spriteBatch">The destination drawing surface</param>
  246. /// <param name="x">The center X of the circle</param>
  247. /// <param name="y">The center Y of the circle</param>
  248. /// <param name="radius">The radius of the circle</param>
  249. /// <param name="sides">The number of sides to generate</param>
  250. /// <param name="color">The color of the circle</param>
  251. /// <param name="thickness">The thickness of the line</param>
  252. /// <param name="layerDepth">The depth of the layer of this shape</param>
  253. public static void DrawCircle(this SpriteBatch spriteBatch, float x, float y, float radius, int sides, Color color, float thickness = 1f, float layerDepth = 0)
  254. {
  255. DrawPolygon(spriteBatch, new Vector2(x, y), CreateCircle(radius, sides), color, thickness, layerDepth);
  256. }
  257. /// <summary>
  258. /// Draw an ellipse.
  259. /// </summary>
  260. /// <param name="spriteBatch">The destination drawing surface</param>
  261. /// <param name="center">Center of the ellipse</param>
  262. /// <param name="radius">Radius of the ellipse</param>
  263. /// <param name="sides">The number of sides to generate.</param>
  264. /// <param name="color">The color of the ellipse.</param>
  265. /// <param name="thickness">The thickness of the line around the ellipse.</param>
  266. /// <param name="layerDepth">The depth of the layer of this shape</param>
  267. public static void DrawEllipse(this SpriteBatch spriteBatch, Vector2 center, Vector2 radius, int sides, Color color, float thickness = 1f, float layerDepth = 0)
  268. {
  269. DrawPolygon(spriteBatch, center, CreateEllipse(radius.X, radius.Y, sides), color, thickness, layerDepth);
  270. }
  271. private static Vector2[] CreateCircle(double radius, int sides)
  272. {
  273. const double max = 2.0 * Math.PI;
  274. var points = new Vector2[sides];
  275. var step = max / sides;
  276. var theta = 0.0;
  277. for (var i = 0; i < sides; i++)
  278. {
  279. points[i] = new Vector2((float)(radius * Math.Cos(theta)), (float)(radius * Math.Sin(theta)));
  280. theta += step;
  281. }
  282. return points;
  283. }
  284. private static Vector2[] CreateEllipse(float rx, float ry, int sides)
  285. {
  286. var vertices = new Vector2[sides];
  287. var t = 0.0;
  288. var dt = 2.0 * Math.PI / sides;
  289. for (var i = 0; i < sides; i++, t += dt)
  290. {
  291. var x = (float)(rx * Math.Cos(t));
  292. var y = (float)(ry * Math.Sin(t));
  293. vertices[i] = new Vector2(x, y);
  294. }
  295. return vertices;
  296. }
  297. }
  298. }