From 173f7d0fb46f022dc0e8483638859d47ec117a6e Mon Sep 17 00:00:00 2001 From: Daniel Baetz Date: Fri, 13 Nov 2020 12:29:19 +0100 Subject: [PATCH] added line --- .../Graphics/SpriteBatch.Drawing.cs | 73 +++++++- src/Exomia.Framework/Mathematics/Line2.cs | 177 ++++++++++++++++++ src/Exomia.Framework/VectorI2.cs | 3 +- src/Exomia.Framework/VectorI3.cs | 3 +- 4 files changed, 250 insertions(+), 6 deletions(-) create mode 100644 src/Exomia.Framework/Mathematics/Line2.cs diff --git a/src/Exomia.Framework/Graphics/SpriteBatch.Drawing.cs b/src/Exomia.Framework/Graphics/SpriteBatch.Drawing.cs index d9eed65f..4c240a16 100644 --- a/src/Exomia.Framework/Graphics/SpriteBatch.Drawing.cs +++ b/src/Exomia.Framework/Graphics/SpriteBatch.Drawing.cs @@ -9,8 +9,10 @@ #endregion using System; +using System.Runtime.CompilerServices; using System.Text; using System.Threading; +using Exomia.Framework.Mathematics; using SharpDX; namespace Exomia.Framework.Graphics @@ -28,6 +30,7 @@ public sealed partial class SpriteBatch /// The rotation. /// The opacity. /// Depth of the layer. + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void DrawRectangle(in RectangleF destinationRectangle, in Color color, float lineWidth, @@ -108,6 +111,7 @@ public void DrawRectangle(in RectangleF destinationRectangle, /// Destination rectangle. /// The color. /// Depth of the layer. + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void DrawFillRectangle(in RectangleF destinationRectangle, in Color color, float layerDepth) { DrawSprite( @@ -122,6 +126,7 @@ public void DrawFillRectangle(in RectangleF destinationRectangle, in Color color /// The color. /// The opacity. /// Depth of the layer. + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void DrawFillRectangle(in RectangleF destinationRectangle, in Color color, float opacity, @@ -141,6 +146,7 @@ public void DrawFillRectangle(in RectangleF destinationRectangle, /// The origin. /// The opacity. /// Depth of the layer. + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void DrawFillRectangle(in RectangleF destinationRectangle, in Color color, float rotation, @@ -162,6 +168,7 @@ public void DrawFillRectangle(in RectangleF destinationRectangle, /// Width of the line. /// The opacity. /// Depth of the layer. + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void DrawLine(in Vector2 point1, in Vector2 point2, in Color color, @@ -172,6 +179,24 @@ public void DrawLine(in Vector2 point1, DrawLine(point1, point2, color, lineWidth, opacity, 1.0f, layerDepth); } + /// + /// Draw line. + /// + /// The line. + /// The color. + /// Width of the line. + /// The opacity. + /// Depth of the layer. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void DrawLine(in Line2 line, + in Color color, + float lineWidth, + float opacity, + float layerDepth) + { + DrawLine(in line, color, lineWidth, opacity, 1.0f, layerDepth); + } + /// /// Draw line. /// @@ -182,6 +207,7 @@ public void DrawLine(in Vector2 point1, /// The opacity. /// The length factor. /// Depth of the layer. + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void DrawLine(in Vector2 point1, in Vector2 point2, in Color color, @@ -190,10 +216,32 @@ public void DrawLine(in Vector2 point1, float lengthFactor, float layerDepth) { + DrawLine(new Line2(point1, point2), color, lineWidth, opacity, lengthFactor, layerDepth); + } + + /// + /// Draw line. + /// + /// The line. + /// The color. + /// Width of the line. + /// The opacity. + /// The length factor. + /// Depth of the layer. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void DrawLine(in Line2 line, + in Color color, + float lineWidth, + float opacity, + float lengthFactor, + float layerDepth) + { + float dx = line.X2 - line.X1; + float dy = line.Y2 - line.Y1; DrawSprite( _whiteTexture, new RectangleF( - point1.X, point1.Y, Vector2.Distance(point1, point2) * lengthFactor, lineWidth), false, - s_nullRectangle, color, (float)Math.Atan2(point2.Y - point1.Y, point2.X - point1.X), + line.X1, line.Y1, (float)Math.Sqrt((dx * dx) + (dy * dy)) * lengthFactor, lineWidth), false, + s_nullRectangle, color, (float)Math.Atan2(dy, dx), s_vector2Zero, opacity, SpriteEffects.None, layerDepth); } @@ -228,6 +276,7 @@ public void DrawPolygon(Vector2[] vertex, in Color color, float lineWidth, float /// The opacity. /// The segments. /// Depth of the layer. + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void DrawCircle(in Vector2 center, float radius, in Color color, @@ -286,6 +335,7 @@ public void DrawCircle(in Vector2 center, /// The texture. /// The position. /// The color. + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Draw(Texture texture, in Vector2 position, in Color color) { DrawSprite( @@ -299,6 +349,7 @@ public void Draw(Texture texture, in Vector2 position, in Color color) /// The texture. /// Destination rectangle. /// The color. + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Draw(Texture texture, in RectangleF destinationRectangle, in Color color) { DrawSprite( @@ -313,6 +364,7 @@ public void Draw(Texture texture, in RectangleF destinationRectangle, in Color c /// The position. /// Source rectangle. /// The color. + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Draw(Texture texture, in Vector2 position, in Rectangle? sourceRectangle, in Color color) { DrawSprite( @@ -327,6 +379,7 @@ public void Draw(Texture texture, in Vector2 position, in Rectangle? sourceRecta /// Destination rectangle. /// Source rectangle. /// The color. + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Draw(Texture texture, in RectangleF destinationRectangle, in Rectangle? sourceRectangle, @@ -346,6 +399,7 @@ public void Draw(Texture texture, /// The rotation. /// The origin. /// (Optional) Depth of the layer. + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Draw(Texture texture, in Vector2 position, in Color color, @@ -367,6 +421,7 @@ public void Draw(Texture texture, /// The rotation. /// The origin. /// Depth of the layer. + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Draw(Texture texture, in RectangleF destinationRectangle, in Color color, @@ -391,6 +446,7 @@ public void Draw(Texture texture, /// The opacity. /// The effects. /// Depth of the layer. + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Draw(Texture texture, in RectangleF destinationRectangle, in Rectangle? sourceRectangle, @@ -419,6 +475,7 @@ public void Draw(Texture texture, /// The opacity. /// The effects. /// Depth of the layer. + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Draw(Texture texture, in Vector2 position, in Rectangle? sourceRectangle, @@ -448,6 +505,7 @@ public void Draw(Texture texture, /// The opacity. /// The effects. /// Depth of the layer. + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Draw(Texture texture, in Vector2 position, in Rectangle? sourceRectangle, @@ -601,6 +659,7 @@ private unsafe void DrawSprite(Texture texture, /// The position. /// The color. /// Depth of the layer. + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void DrawText(SpriteFont font, string text, in Vector2 position, in Color color, float layerDepth) { font.Draw( @@ -617,6 +676,7 @@ public void DrawText(SpriteFont font, string text, in Vector2 position, in Color /// The color. /// The rotation. /// Depth of the layer. + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void DrawText(SpriteFont font, string text, in Vector2 position, @@ -641,6 +701,7 @@ public void DrawText(SpriteFont font, /// The opacity. /// The effects. /// Depth of the layer. + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void DrawText(SpriteFont font, string text, in Vector2 position, @@ -670,6 +731,7 @@ public void DrawText(SpriteFont font, /// The opacity. /// The effects. /// Depth of the layer. + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void DrawText(SpriteFont font, string text, int start, @@ -702,6 +764,7 @@ public void DrawText(SpriteFont font, /// The opacity. /// The effects. /// Depth of the layer. + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void DrawText(SpriteFont font, string text, int start, @@ -728,6 +791,7 @@ public void DrawText(SpriteFont font, /// The position. /// The color. /// Depth of the layer. + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void DrawText(SpriteFont font, StringBuilder text, in Vector2 position, in Color color, float layerDepth) { font.Draw( @@ -744,6 +808,7 @@ public void DrawText(SpriteFont font, StringBuilder text, in Vector2 position, i /// The color. /// The rotation. /// Depth of the layer. + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void DrawText(SpriteFont font, StringBuilder text, in Vector2 position, @@ -768,6 +833,7 @@ public void DrawText(SpriteFont font, /// The opacity. /// The effects. /// Depth of the layer. + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void DrawText(SpriteFont font, StringBuilder text, in Vector2 position, @@ -797,6 +863,7 @@ public void DrawText(SpriteFont font, /// The opacity. /// The effects. /// Depth of the layer. + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void DrawText(SpriteFont font, StringBuilder text, int start, @@ -829,6 +896,7 @@ public void DrawText(SpriteFont font, /// The opacity. /// The effects. /// Depth of the layer. + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void DrawText(SpriteFont font, StringBuilder text, int start, @@ -860,6 +928,7 @@ public void DrawText(SpriteFont font, /// The opacity. /// The effects. /// Depth of the layer. + [MethodImpl(MethodImplOptions.AggressiveInlining)] internal void DrawTextInternal(Texture texture, in Vector2 position, in Rectangle? sourceRectangle, diff --git a/src/Exomia.Framework/Mathematics/Line2.cs b/src/Exomia.Framework/Mathematics/Line2.cs new file mode 100644 index 00000000..5d22115a --- /dev/null +++ b/src/Exomia.Framework/Mathematics/Line2.cs @@ -0,0 +1,177 @@ +#region License + +// Copyright (c) 2018-2020, exomia +// All rights reserved. +// +// This source code is licensed under the BSD-style license found in the +// LICENSE file in the root directory of this source tree. + +#endregion + +using System; +using System.Globalization; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using SharpDX; + +namespace Exomia.Framework.Mathematics +{ + /// + /// A 2d line. + /// + /// + [StructLayout(LayoutKind.Sequential, Pack = 4, Size = 16)] + public readonly struct Line2 : IFormattable + { + /// + /// The first x value. + /// + public readonly float X1; + + /// + /// The second x value. + /// + public readonly float X2; + + /// + /// The first y value. + /// + public readonly float Y1; + + /// + /// The second y value. + /// + public readonly float Y2; + + /// + /// Initializes a new instance of the struct. + /// + /// The first x value. + /// The second x value. + /// The first y value. + /// The second y value. + public Line2(float x1, float x2, float y1, float y2) + { + X1 = x1; + X2 = x2; + Y1 = y1; + Y2 = y2; + } + + /// + /// Initializes a new instance of the struct. + /// + /// The VectorI2 to process. + /// The VectorI2 to process. + public Line2(VectorI2 a, VectorI2 b) + : this(a.X, b.X, a.Y, b.Y) { } + + /// + /// Initializes a new instance of the struct. + /// + /// The Vector2 to process. + /// The Vector2 to process. + public Line2(Vector2 a, Vector2 b) + : this(a.X, b.X, a.Y, b.Y) { } + + /// + /// Determines whether the specified is equal to this instance. + /// + /// The to compare with this instance. + /// + /// true if the specified is equal to this instance; false otherwise. + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public bool Equals(in Line2 other) + { + // ReSharper disable CompareOfFloatsByEqualityOperator + return + X1 == other.X1 && + X2 == other.X2 && + Y1 == other.Y1 && + Y2 == other.Y2; + + // ReSharper restore CompareOfFloatsByEqualityOperator + } + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public override bool Equals(object value) + { + if (value is Line2 other) + { + return Equals(in other); + } + return false; + } + + /// + public override int GetHashCode() + { + return (((((X1.GetHashCode() * 307) ^ Y1.GetHashCode()) * 521) ^ X2.GetHashCode()) * 853) ^ + Y2.GetHashCode(); + } + + /// + public override string ToString() + { + return string.Format( + CultureInfo.CurrentCulture, + "X1:{0} Y1:{1} | X2:{2} Y2:{3}", X1, Y1, X2, Y2); + } + + /// + /// Returns a that represents this instance. + /// + /// The format. + /// + /// A that represents this instance. + /// + public string ToString(string? format) + { + if (format == null) + { + return ToString(); + } + + return string.Format( + CultureInfo.CurrentCulture, + "X1:{0} Y1:{1} | X2:{2} Y2:{3}", + X1.ToString(format, CultureInfo.CurrentCulture), + Y1.ToString(format, CultureInfo.CurrentCulture), + X2.ToString(format, CultureInfo.CurrentCulture), + Y2.ToString(format, CultureInfo.CurrentCulture)); + } + + /// + /// Returns a that represents this instance. + /// + /// The format provider. + /// + /// A that represents this instance. + /// + public string ToString(IFormatProvider formatProvider) + { + return string.Format( + formatProvider, + "X1:{0} Y1:{1} | X2:{2} Y2:{3}", X1, Y1, X2, Y2); + } + + /// + public string ToString(string? format, IFormatProvider formatProvider) + { + if (format == null) + { + return ToString(formatProvider); + } + + return string.Format( + formatProvider, + "X1:{0} Y1:{1} | X2:{2} Y2:{3}", + X1.ToString(format, formatProvider), + Y1.ToString(format, formatProvider), + X2.ToString(format, formatProvider), + Y2.ToString(format, formatProvider)); + } + } +} \ No newline at end of file diff --git a/src/Exomia.Framework/VectorI2.cs b/src/Exomia.Framework/VectorI2.cs index 5baa599e..d83c9ed6 100644 --- a/src/Exomia.Framework/VectorI2.cs +++ b/src/Exomia.Framework/VectorI2.cs @@ -79,8 +79,7 @@ public VectorI2(int x, int y) /// /// The to compare with this instance. /// - /// true if the specified is equal to this instance; otherwise, - /// false. + /// true if the specified is equal to this instance; false otherwise. /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public readonly bool Equals(in VectorI2 other) diff --git a/src/Exomia.Framework/VectorI3.cs b/src/Exomia.Framework/VectorI3.cs index 98e1d392..c04f45ad 100644 --- a/src/Exomia.Framework/VectorI3.cs +++ b/src/Exomia.Framework/VectorI3.cs @@ -105,8 +105,7 @@ public VectorI3(in VectorI2 value, int z) /// /// The to compare with this instance. /// - /// true if the specified is equal to this instance; - /// false otherwise. + /// true if the specified is equal to this instance; false otherwise. /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public readonly bool Equals(in VectorI3 other)