ShapeExtensions.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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. /// Provides <see cref="SpriteBatch"/> extension methods for drawing primitive shape outlines.
  10. /// </summary>
  11. /// <remarks>
  12. /// <para>
  13. /// These methods are intended for <b>prototyping and debug visualization only</b>. They rely on
  14. /// <see cref="SpriteBatch"/> and a 1×1 white pixel texture to approximate shapes, which has the following
  15. /// known limitations:
  16. /// </para>
  17. /// <list type="bullet">
  18. /// <item>
  19. /// <description>
  20. /// Outline joints (corners) physically overlap. With semi-transparent colors this causes visible
  21. /// double-blending artifacts at each joint.
  22. /// </description>
  23. /// </item>
  24. /// <item>
  25. /// <description>
  26. /// Filled shape drawing is not supported here. For filled shapes with correct alpha blending use
  27. /// <see cref="MonoGame.Extended.VectorDraw.PrimitiveDrawing"/> with a
  28. /// <see cref="MonoGame.Extended.VectorDraw.PrimitiveBatch"/>.
  29. /// </description>
  30. /// </item>
  31. /// </list>
  32. /// </remarks>
  33. public static class ShapeExtensions
  34. {
  35. private static Texture2D _whitePixelTexture;
  36. private static Texture2D GetTexture(SpriteBatch spriteBatch)
  37. {
  38. if (_whitePixelTexture == null || _whitePixelTexture.IsDisposed)
  39. {
  40. _whitePixelTexture = new Texture2D(spriteBatch.GraphicsDevice, 1, 1, false, SurfaceFormat.Color);
  41. _whitePixelTexture.SetData(new[] { Color.White });
  42. spriteBatch.Disposing += (sender, args) =>
  43. {
  44. _whitePixelTexture?.Dispose();
  45. _whitePixelTexture = null;
  46. };
  47. }
  48. return _whitePixelTexture;
  49. }
  50. /// <summary>
  51. /// Draws a closed polygon from a <see cref="Polygon" /> shape
  52. /// </summary>
  53. /// <param name="spriteBatch">The destination drawing surface</param>
  54. /// ///
  55. /// <param name="position">Where to position the polygon</param>
  56. /// <param name="polygon">The polygon to draw</param>
  57. /// <param name="color">The color to use</param>
  58. /// <param name="thickness">The thickness of the lines</param>
  59. /// /// <param name="layerDepth">The depth of the layer of this shape</param>
  60. public static void DrawPolygon(this SpriteBatch spriteBatch, Vector2 position, Polygon polygon, Color color, float thickness = 1f, float layerDepth = 0)
  61. {
  62. DrawPolygon(spriteBatch, position, polygon.Vertices, color, thickness, layerDepth);
  63. }
  64. /// <summary>
  65. /// Draws a closed polygon from an array of points
  66. /// </summary>
  67. /// <param name="spriteBatch">The destination drawing surface</param>
  68. /// ///
  69. /// <param name="offset">Where to offset the points</param>
  70. /// <param name="points">The points to connect with lines</param>
  71. /// <param name="color">The color to use</param>
  72. /// <param name="thickness">The thickness of the lines</param>
  73. /// <param name="layerDepth">The depth of the layer of this shape</param>
  74. public static void DrawPolygon(this SpriteBatch spriteBatch, Vector2 offset, IReadOnlyList<Vector2> points, Color color, float thickness = 1f, float layerDepth = 0)
  75. {
  76. if (points.Count == 0)
  77. return;
  78. if (points.Count == 1)
  79. {
  80. DrawPoint(spriteBatch, points[0], color, (int)thickness);
  81. return;
  82. }
  83. var texture = GetTexture(spriteBatch);
  84. for (var i = 0; i < points.Count - 1; i++)
  85. DrawPolygonEdge(spriteBatch, texture, points[i] + offset, points[i + 1] + offset, color, thickness, layerDepth);
  86. DrawPolygonEdge(spriteBatch, texture, points[points.Count - 1] + offset, points[0] + offset, color, thickness, layerDepth);
  87. }
  88. private static void DrawPolygonEdge(SpriteBatch spriteBatch, Texture2D texture, Vector2 point1, Vector2 point2, Color color, float thickness, float layerDepth)
  89. {
  90. var length = Vector2.Distance(point1, point2);
  91. var angle = (float)Math.Atan2(point2.Y - point1.Y, point2.X - point1.X);
  92. var scale = new Vector2(length, thickness);
  93. spriteBatch.Draw(texture, point1, null, color, angle, Vector2.Zero, scale, SpriteEffects.None, layerDepth);
  94. }
  95. /// <summary>
  96. /// Draws a filled rectangle
  97. /// </summary>
  98. /// <param name="spriteBatch">The destination drawing surface</param>
  99. /// <param name="rectangle">The rectangle to draw</param>
  100. /// <param name="color">The color to draw the rectangle in</param>
  101. /// <param name="layerDepth">The depth of the layer of this shape</param>
  102. public static void FillRectangle(this SpriteBatch spriteBatch, RectangleF rectangle, Color color, float layerDepth = 0)
  103. {
  104. FillRectangle(spriteBatch, rectangle.Position, rectangle.Size, color, layerDepth);
  105. }
  106. /// <summary>
  107. /// Draws a filled rectangle
  108. /// </summary>
  109. /// <param name="spriteBatch">The destination drawing surface</param>
  110. /// <param name="location">Where to draw</param>
  111. /// <param name="size">The size of the rectangle</param>
  112. /// <param name="color">The color to draw the rectangle in</param>
  113. /// <param name="layerDepth">The depth of the layer of this shape</param>
  114. public static void FillRectangle(this SpriteBatch spriteBatch, Vector2 location, SizeF size, Color color, float layerDepth = 0)
  115. {
  116. spriteBatch.Draw(GetTexture(spriteBatch), location, null, color, 0, Vector2.Zero, size, SpriteEffects.None, layerDepth);
  117. }
  118. /// <summary>
  119. /// Draws a filled rectangle
  120. /// </summary>
  121. /// <param name="spriteBatch">The destination drawing surface</param>
  122. /// <param name="x">The X coord of the left side</param>
  123. /// <param name="y">The Y coord of the upper side</param>
  124. /// <param name="width">Width</param>
  125. /// <param name="height">Height</param>
  126. /// <param name="color">The color to draw the rectangle in</param>
  127. /// <param name="layerDepth">The depth of the layer of this shape</param>
  128. public static void FillRectangle(this SpriteBatch spriteBatch, float x, float y, float width, float height, Color color, float layerDepth = 0)
  129. {
  130. FillRectangle(spriteBatch, new Vector2(x, y), new SizeF(width, height), color, layerDepth);
  131. }
  132. /// <summary>
  133. /// Draws a rectangle with the thickness provided
  134. /// </summary>
  135. /// <param name="spriteBatch">The destination drawing surface</param>
  136. /// <param name="rectangle">The rectangle to draw</param>
  137. /// <param name="color">The color to draw the rectangle in</param>
  138. /// <param name="thickness">The thickness of the lines</param>
  139. /// <param name="layerDepth">The depth of the layer of this shape</param>
  140. public static void DrawRectangle(this SpriteBatch spriteBatch, RectangleF rectangle, Color color, float thickness = 1f, float layerDepth = 0)
  141. {
  142. var texture = GetTexture(spriteBatch);
  143. var topLeft = new Vector2(rectangle.X, rectangle.Y);
  144. var topRight = new Vector2(rectangle.Right - thickness, rectangle.Y);
  145. var bottomLeft = new Vector2(rectangle.X, rectangle.Bottom - thickness);
  146. var horizontalScale = new Vector2(rectangle.Width, thickness);
  147. var verticalScale = new Vector2(thickness, rectangle.Height);
  148. spriteBatch.Draw(texture, topLeft, null, color, 0f, Vector2.Zero, horizontalScale, SpriteEffects.None, layerDepth);
  149. spriteBatch.Draw(texture, topLeft, null, color, 0f, Vector2.Zero, verticalScale, SpriteEffects.None, layerDepth);
  150. spriteBatch.Draw(texture, topRight, null, color, 0f, Vector2.Zero, verticalScale, SpriteEffects.None, layerDepth);
  151. spriteBatch.Draw(texture, bottomLeft, null, color, 0f, Vector2.Zero, horizontalScale, SpriteEffects.None, layerDepth);
  152. }
  153. /// <summary>
  154. /// Draws a rectangle with the thickness provided
  155. /// </summary>
  156. /// <param name="spriteBatch">The destination drawing surface</param>
  157. /// <param name="location">Where to draw</param>
  158. /// <param name="size">The size of the rectangle</param>
  159. /// <param name="color">The color to draw the rectangle in</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 DrawRectangle(this SpriteBatch spriteBatch, Vector2 location, SizeF size, Color color, float thickness = 1f, float layerDepth = 0)
  163. {
  164. DrawRectangle(spriteBatch, new RectangleF(location.X, location.Y, size.Width, size.Height), color, thickness, layerDepth);
  165. }
  166. /// <summary>
  167. /// Draws a rectangle outline.
  168. /// </summary>
  169. public static void DrawRectangle(this SpriteBatch spriteBatch, float x, float y, float width, float height, Color color, float thickness = 1f, float layerDepth = 0)
  170. {
  171. DrawRectangle(spriteBatch, new RectangleF(x, y, width, height), color, thickness, layerDepth);
  172. }
  173. /// <summary>
  174. /// Draws a line from point1 to point2 with an offset
  175. /// </summary>
  176. /// <param name="spriteBatch">The destination drawing surface</param>
  177. /// <param name="x1">The X coord of the first point</param>
  178. /// <param name="y1">The Y coord of the first point</param>
  179. /// <param name="x2">The X coord of the second point</param>
  180. /// <param name="y2">The Y coord of the second point</param>
  181. /// <param name="color">The color to use</param>
  182. /// <param name="thickness">The thickness of the line</param>
  183. /// <param name="layerDepth">The depth of the layer of this shape</param>
  184. public static void DrawLine(this SpriteBatch spriteBatch, float x1, float y1, float x2, float y2, Color color, float thickness = 1f, float layerDepth = 0)
  185. {
  186. DrawLine(spriteBatch, new Vector2(x1, y1), new Vector2(x2, y2), color, thickness, layerDepth);
  187. }
  188. /// <summary>
  189. /// Draws a line from point1 to point2 with an offset
  190. /// </summary>
  191. /// <param name="spriteBatch">The destination drawing surface</param>
  192. /// <param name="point1">The first point</param>
  193. /// <param name="point2">The second point</param>
  194. /// <param name="color">The color to use</param>
  195. /// <param name="thickness">The thickness of the line</param>
  196. /// <param name="layerDepth">The depth of the layer of this shape</param>
  197. public static void DrawLine(this SpriteBatch spriteBatch, Vector2 point1, Vector2 point2, Color color, float thickness = 1f, float layerDepth = 0)
  198. {
  199. // calculate the distance between the two vectors
  200. var distance = Vector2.Distance(point1, point2);
  201. // calculate the angle between the two vectors
  202. var angle = (float)Math.Atan2(point2.Y - point1.Y, point2.X - point1.X);
  203. DrawLine(spriteBatch, point1, distance, angle, color, thickness, layerDepth);
  204. }
  205. /// <summary>
  206. /// Draws a line from point1 to point2 with an offset
  207. /// </summary>
  208. /// <param name="spriteBatch">The destination drawing surface</param>
  209. /// <param name="point">The starting point</param>
  210. /// <param name="length">The length of the line</param>
  211. /// <param name="angle">The angle of this line from the starting point</param>
  212. /// <param name="color">The color to use</param>
  213. /// <param name="thickness">The thickness of the line</param>
  214. /// <param name="layerDepth">The depth of the layer of this shape</param>
  215. public static void DrawLine(this SpriteBatch spriteBatch, Vector2 point, float length, float angle, Color color, float thickness = 1f, float layerDepth = 0)
  216. {
  217. var origin = new Vector2(0f, 0.5f);
  218. var scale = new Vector2(length, thickness);
  219. spriteBatch.Draw(GetTexture(spriteBatch), point, null, color, angle, origin, scale, SpriteEffects.None, layerDepth);
  220. }
  221. /// <summary>
  222. /// Draws a point at the specified x, y position. The center of the point will be at the position.
  223. /// </summary>
  224. public static void DrawPoint(this SpriteBatch spriteBatch, float x, float y, Color color, float size = 1f, float layerDepth = 0)
  225. {
  226. DrawPoint(spriteBatch, new Vector2(x, y), color, size, layerDepth);
  227. }
  228. /// <summary>
  229. /// Draws a point at the specified position. The center of the point will be at the position.
  230. /// </summary>
  231. public static void DrawPoint(this SpriteBatch spriteBatch, Vector2 position, Color color, float size = 1f, float layerDepth = 0)
  232. {
  233. var scale = Vector2.One * size;
  234. var offset = new Vector2(0.5f) - new Vector2(size * 0.5f);
  235. spriteBatch.Draw(GetTexture(spriteBatch), position + offset, null, color, 0f, Vector2.Zero, scale, SpriteEffects.None, layerDepth);
  236. }
  237. /// <summary>
  238. /// Draw a circle from a <see cref="CircleF" /> shape
  239. /// </summary>
  240. /// <param name="spriteBatch">The destination drawing surface</param>
  241. /// <param name="circle">The circle shape to draw</param>
  242. /// <param name="sides">The number of sides to generate</param>
  243. /// <param name="color">The color of the circle</param>
  244. /// <param name="thickness">The thickness of the lines used</param>
  245. /// <param name="layerDepth">The depth of the layer of this shape</param>
  246. public static void DrawCircle(this SpriteBatch spriteBatch, CircleF circle, int sides, Color color, float thickness = 1f, float layerDepth = 0)
  247. {
  248. DrawCircle(spriteBatch, circle.Center, circle.Radius, sides, color, thickness, layerDepth);
  249. }
  250. /// <summary>
  251. /// Draw a circle
  252. /// </summary>
  253. /// <param name="spriteBatch">The destination drawing surface</param>
  254. /// <param name="center">The center of the circle</param>
  255. /// <param name="radius">The radius of the circle</param>
  256. /// <param name="sides">The number of sides to generate</param>
  257. /// <param name="color">The color of the circle</param>
  258. /// <param name="thickness">The thickness of the lines used</param>
  259. /// <param name="layerDepth">The depth of the layer of this shape</param>
  260. public static void DrawCircle(this SpriteBatch spriteBatch, Vector2 center, float radius, int sides, Color color, float thickness = 1f, float layerDepth = 0)
  261. {
  262. DrawPolygon(spriteBatch, center, CreateCircle(radius, sides), color, thickness, layerDepth);
  263. }
  264. /// <summary>
  265. /// Draw a circle
  266. /// </summary>
  267. /// <param name="spriteBatch">The destination drawing surface</param>
  268. /// <param name="x">The center X of the circle</param>
  269. /// <param name="y">The center Y of the circle</param>
  270. /// <param name="radius">The radius of the circle</param>
  271. /// <param name="sides">The number of sides to generate</param>
  272. /// <param name="color">The color of the circle</param>
  273. /// <param name="thickness">The thickness of the line</param>
  274. /// <param name="layerDepth">The depth of the layer of this shape</param>
  275. public static void DrawCircle(this SpriteBatch spriteBatch, float x, float y, float radius, int sides, Color color, float thickness = 1f, float layerDepth = 0)
  276. {
  277. DrawPolygon(spriteBatch, new Vector2(x, y), CreateCircle(radius, sides), color, thickness, layerDepth);
  278. }
  279. /// <summary>
  280. /// Draw an ellipse.
  281. /// </summary>
  282. /// <param name="spriteBatch">The destination drawing surface</param>
  283. /// <param name="center">Center of the ellipse</param>
  284. /// <param name="radius">Radius of the ellipse</param>
  285. /// <param name="sides">The number of sides to generate.</param>
  286. /// <param name="color">The color of the ellipse.</param>
  287. /// <param name="thickness">The thickness of the line around the ellipse.</param>
  288. /// <param name="layerDepth">The depth of the layer of this shape</param>
  289. public static void DrawEllipse(this SpriteBatch spriteBatch, Vector2 center, Vector2 radius, int sides, Color color, float thickness = 1f, float layerDepth = 0)
  290. {
  291. DrawPolygon(spriteBatch, center, CreateEllipse(radius.X, radius.Y, sides), color, thickness, layerDepth);
  292. }
  293. /// <summary>
  294. /// Draws an arc outline.
  295. /// </summary>
  296. /// <param name="spriteBatch">The destination drawing surface.</param>
  297. /// <param name="center">The center point of the arc.</param>
  298. /// <param name="radius">The radius of the arc.</param>
  299. /// <param name="startAngle">The starting angle in radians.</param>
  300. /// <param name="sweepAngle">
  301. /// The sweep angle in radians. Positive values sweep counter-clockwise.
  302. /// Use <c>MathHelper.TwoPi</c> to draw a full circle.
  303. /// </param>
  304. /// <param name="sides">The number of line segments used to approximate the arc.</param>
  305. /// <param name="color">The color of the arc.</param>
  306. /// <param name="thickness">The thickness of the lines used.</param>
  307. /// <param name="layerDepth">The depth of the layer of this shape.</param>
  308. public static void DrawArc(this SpriteBatch spriteBatch, Vector2 center, float radius, float startAngle, float sweepAngle, int sides, Color color, float thickness = 1f, float layerDepth = 0)
  309. {
  310. Texture2D texture = GetTexture(spriteBatch);
  311. Vector2[] arc = CreateArc(radius, sides, startAngle, sweepAngle);
  312. for (int i = 0; i < arc.Length - 1; i++)
  313. {
  314. DrawPolygonEdge(spriteBatch, texture, center + arc[i], center + arc[i + 1], color, thickness, layerDepth);
  315. }
  316. }
  317. /// <summary>
  318. /// Draws an arc outline.
  319. /// </summary>
  320. /// <param name="spriteBatch">The destination drawing surface.</param>
  321. /// <param name="x">The center X of the arc.</param>
  322. /// <param name="y">The center Y of the arc.</param>
  323. /// <param name="radius">The radius of the arc.</param>
  324. /// <param name="startAngle">The starting angle in radians.</param>
  325. /// <param name="sweepAngle">
  326. /// The sweep angle in radians. Positive values sweep counter-clockwise.
  327. /// Use <c>MathHelper.TwoPi</c> to draw a full circle.
  328. /// </param>
  329. /// <param name="sides">The number of line segments used to approximate the arc.</param>
  330. /// <param name="color">The color of the arc.</param>
  331. /// <param name="thickness">The thickness of the lines used.</param>
  332. /// <param name="layerDepth">The depth of the layer of this shape.</param>
  333. public static void DrawArc(this SpriteBatch spriteBatch, float x, float y, float radius, float startAngle, float sweepAngle, int sides, Color color, float thickness = 1f, float layerDepth = 0)
  334. {
  335. DrawArc(spriteBatch, new Vector2(x, y), radius, startAngle, sweepAngle, sides, color, thickness, layerDepth);
  336. }
  337. private static Vector2[] CreateArc(float radius, int sides, float startAngle, float sweepAngle)
  338. {
  339. Vector2[] points = new Vector2[sides + 1];
  340. float step = sweepAngle / sides;
  341. float theta = startAngle;
  342. for (int i = 0; i <= sides; i++, theta += step)
  343. {
  344. points[i] = new Vector2((float)(radius * Math.Cos(theta)), (float)(radius * Math.Sin(theta)));
  345. }
  346. return points;
  347. }
  348. private static Vector2[] CreateCircle(double radius, int sides)
  349. {
  350. const double max = 2.0 * Math.PI;
  351. var points = new Vector2[sides];
  352. var step = max / sides;
  353. var theta = 0.0;
  354. for (var i = 0; i < sides; i++)
  355. {
  356. points[i] = new Vector2((float)(radius * Math.Cos(theta)), (float)(radius * Math.Sin(theta)));
  357. theta += step;
  358. }
  359. return points;
  360. }
  361. private static Vector2[] CreateEllipse(float rx, float ry, int sides)
  362. {
  363. var vertices = new Vector2[sides];
  364. var t = 0.0;
  365. var dt = 2.0 * Math.PI / sides;
  366. for (var i = 0; i < sides; i++, t += dt)
  367. {
  368. var x = (float)(rx * Math.Cos(t));
  369. var y = (float)(ry * Math.Sin(t));
  370. vertices[i] = new Vector2(x, y);
  371. }
  372. return vertices;
  373. }
  374. }
  375. }