1
0
mirror of https://github.com/oliverbooth/X10D synced 2024-11-09 23:25:43 +00:00

Add conversions between Size(F)/Point(F)/Vector and Rect/Rectangle(F)

This commit is contained in:
Oliver Booth 2022-05-28 14:19:46 +01:00
parent 7ca206721b
commit f35f398d7f
No known key found for this signature in database
GPG Key ID: 32A00B35503AF634
32 changed files with 801 additions and 0 deletions

View File

@ -3,15 +3,35 @@
## 3.2.0
### Added
- X10D: Added `MathUtility.InverseLerp(float, float, float)` and `MathUtility.InverseLerp(double, double, double)`
- X10D: Added `Point.ToSize()`
- X10D: Added `Point.ToSizeF()`
- X10D: Added `Point.ToVector2()`
- X10D: Added `PointF.ToSizeF()`
- X10D: Added `RoundUpToPowerOf2()` for built-in integer types
- X10D: Added `Size.ToPoint()`
- X10D: Added `Size.ToPointF()`
- X10D: Added `Size.ToVector2()`
- X10D: Added `Vector2.Deconstruct()`
- X10D: Added `Vector2.ToPointF()`
- X10D: Added `Vector2.ToSizeF()`
- X10D: Added `Vector3.Deconstruct()`
- X10D: Added `Vector4.Deconstruct()`
- X10D.Unity: Added `System.Drawing.Color.ToUnityColor()`
- X10D.Unity: Added `System.Drawing.Color.ToUnityColor32()`
- X10D.Unity: Added `Color.ToSystemDrawingColor()`
- X10D.Unity: Added `Color32.ToSystemDrawingColor()`
- X10D.Unity: Added `Point.ToUnityVector2()`
- X10D.Unity: Added `Point.ToUnityVector2Int()`
- X10D.Unity: Added `PointF.ToUnityVector2()`
- X10D.Unity: Added `Rect.ToSystemRectangleF()`
- X10D.Unity: Added `Rectangle.ToUnityRect()`
- X10D.Unity: Added `RectangleF.ToUnityRect()`
- X10D.Unity: Added `Size.ToUnityVector2()`
- X10D.Unity: Added `Size.ToUnityVector2Int()`
- X10D.Unity: Added `SizeF.ToUnityVector2()`
- X10D.Unity: Added `Vector2.Deconstruct()`
- X10D.Unity: Added `Vector2.ToSystemPointF()`
- X10D.Unity: Added `Vector2.ToSystemSizeF()`
- X10D.Unity: Added `Vector3.Deconstruct()`
- X10D.Unity: Added `Vector4.Deconstruct()`

View File

@ -0,0 +1,21 @@
using System.Drawing;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using X10D.Core;
using X10D.Drawing;
namespace X10D.Tests.Drawing;
[TestClass]
public class PointFTests
{
[TestMethod]
public void ToSizeF_ShouldReturnSize_WithEquivalentMembers()
{
var random = new Random();
var point = new PointF(random.NextSingle(), random.NextSingle());
var size = point.ToSizeF();
Assert.AreEqual(point.X, size.Width, 1e-6f);
Assert.AreEqual(point.Y, size.Height, 1e-6f);
}
}

View File

@ -0,0 +1,42 @@
using System.Drawing;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using X10D.Drawing;
namespace X10D.Tests.Drawing;
[TestClass]
public class PointTests
{
[TestMethod]
public void ToSize_ShouldReturnSize_WithEquivalentMembers()
{
var random = new Random();
var point = new Point(random.Next(), random.Next());
var size = point.ToSize();
Assert.AreEqual(point.X, size.Width);
Assert.AreEqual(point.Y, size.Height);
}
[TestMethod]
public void ToSizeF_ShouldReturnSize_WithEquivalentMembers()
{
var random = new Random();
var point = new Point(random.Next(), random.Next());
var size = point.ToSizeF();
Assert.AreEqual(point.X, size.Width, 1e-6f);
Assert.AreEqual(point.Y, size.Height, 1e-6f);
}
[TestMethod]
public void ToVector2_ShouldReturnVector_WithEquivalentMembers()
{
var random = new Random();
var point = new Point(random.Next(), random.Next());
var size = point.ToVector2();
Assert.AreEqual(point.X, size.X, 1e-6f);
Assert.AreEqual(point.Y, size.Y, 1e-6f);
}
}

View File

@ -0,0 +1,42 @@
using System.Drawing;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using X10D.Drawing;
namespace X10D.Tests.Drawing;
[TestClass]
public class SizeTests
{
[TestMethod]
public void ToPoint_ShouldReturnPoint_WithEquivalentMembers()
{
var random = new Random();
var size = new Size(random.Next(), random.Next());
var point = size.ToPoint();
Assert.AreEqual(size.Width, point.X);
Assert.AreEqual(size.Height, point.Y);
}
[TestMethod]
public void ToPointF_ShouldReturnPoint_WithEquivalentMembers()
{
var random = new Random();
var size = new Size(random.Next(), random.Next());
var point = size.ToPointF();
Assert.AreEqual(size.Width, point.X, 1e-6f);
Assert.AreEqual(size.Height, point.Y, 1e-6f);
}
[TestMethod]
public void ToVector2_ShouldReturnVector_WithEquivalentMembers()
{
var random = new Random();
var point = new Size(random.Next(), random.Next());
var size = point.ToVector2();
Assert.AreEqual(point.Width, size.X, 1e-6f);
Assert.AreEqual(point.Height, size.Y, 1e-6f);
}
}

View File

@ -1,5 +1,6 @@
using System.Numerics;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using X10D.Core;
using X10D.Numerics;
namespace X10D.Tests.Numerics;
@ -17,6 +18,28 @@ public class Vector2Tests
Assert.AreEqual(2, y);
}
[TestMethod]
public void ToPointF_ShouldReturnPoint_WithEquivalentMembers()
{
var random = new Random();
var vector = new Vector2(random.NextSingle(), random.NextSingle());
var point = vector.ToPointF();
Assert.AreEqual(vector.X, point.X, 1e-6f);
Assert.AreEqual(vector.Y, point.Y, 1e-6f);
}
[TestMethod]
public void ToSizeF_ShouldReturnSize_WithEquivalentMembers()
{
var random = new Random();
var vector = new Vector2(random.NextSingle(), random.NextSingle());
var size = vector.ToSizeF();
Assert.AreEqual(vector.X, size.Width);
Assert.AreEqual(vector.Y, size.Height);
}
[TestMethod]
public void WithX_ShouldReturnVectorWithNewX_GivenVector()
{

View File

@ -0,0 +1,26 @@
using System;
using System.Collections;
using System.Drawing;
using NUnit.Framework;
using UnityEngine.TestTools;
using X10D.Core;
using X10D.Unity.Drawing;
namespace X10D.Unity.Tests.Drawing
{
public class PointFTests
{
[UnityTest]
public IEnumerator ToUnityVector2_ShouldReturnVector_WithEquivalentMembers()
{
var random = new Random();
var point = new PointF(random.NextSingle(), random.NextSingle());
var vector = point.ToUnityVector2();
Assert.AreEqual(point.X, vector.x, 1e-6f);
Assert.AreEqual(point.Y, vector.y, 1e-6f);
yield break;
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: d90695756d1d4760aef2523486b1b41e
timeCreated: 1653743243

View File

@ -0,0 +1,38 @@
using System;
using System.Collections;
using System.Drawing;
using NUnit.Framework;
using UnityEngine.TestTools;
using X10D.Unity.Drawing;
namespace X10D.Unity.Tests.Drawing
{
public class PointTests
{
[UnityTest]
public IEnumerator ToUnityVector2_ShouldReturnVector_WithEquivalentMembers()
{
var random = new Random();
var point = new Point(random.Next(), random.Next());
var vector = point.ToUnityVector2();
Assert.AreEqual(point.X, vector.x);
Assert.AreEqual(point.Y, vector.y);
yield break;
}
[UnityTest]
public IEnumerator ToUnityVector2Int_ShouldReturnVector_WithEquivalentMembers()
{
var random = new Random();
var point = new Point(random.Next(), random.Next());
var vector = point.ToUnityVector2Int();
Assert.AreEqual(point.X, vector.x);
Assert.AreEqual(point.Y, vector.y);
yield break;
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: f465794fdc394d05a34229f34e5199e2
timeCreated: 1653742987

View File

@ -0,0 +1,28 @@
using System.Collections;
using NUnit.Framework;
using UnityEngine;
using UnityEngine.TestTools;
using X10D.Core;
using X10D.Unity.Drawing;
using Random = System.Random;
namespace X10D.Unity.Tests.Drawing
{
public class RectTests
{
[UnityTest]
public IEnumerator ToSystemRectangleF_ShouldReturnRectangleF_WithEquivalentMembers()
{
var random = new Random();
var rect = new Rect(random.NextSingle(), random.NextSingle(), random.NextSingle(), random.NextSingle());
var rectangle = rect.ToSystemRectangleF();
Assert.AreEqual(rect.x, rectangle.X, 1e-6f);
Assert.AreEqual(rect.y, rectangle.Y, 1e-6f);
Assert.AreEqual(rect.width, rectangle.Width, 1e-6f);
Assert.AreEqual(rect.height, rectangle.Height, 1e-6f);
yield break;
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: bb1ec5372c354f06b39e03649b9307db
timeCreated: 1653743583

View File

@ -0,0 +1,28 @@
using System.Collections;
using System.Drawing;
using NUnit.Framework;
using UnityEngine.TestTools;
using X10D.Core;
using X10D.Unity.Drawing;
using Random = System.Random;
namespace X10D.Unity.Tests.Drawing
{
public class RectangleFTests
{
[UnityTest]
public IEnumerator ToUnityRect_ShouldReturnRect_WithEquivalentMembers()
{
var random = new Random();
var rectangle = new RectangleF(random.NextSingle(), random.NextSingle(), random.NextSingle(), random.NextSingle());
var rect = rectangle.ToUnityRect();
Assert.AreEqual(rectangle.X, rect.x, 1e-6f);
Assert.AreEqual(rectangle.Y, rect.y, 1e-6f);
Assert.AreEqual(rectangle.Width, rect.width, 1e-6f);
Assert.AreEqual(rectangle.Height, rect.height, 1e-6f);
yield break;
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: f38cbc892021405cad2b52de1f960a00
timeCreated: 1653743640

View File

@ -0,0 +1,27 @@
using System.Collections;
using System.Drawing;
using NUnit.Framework;
using UnityEngine.TestTools;
using X10D.Unity.Drawing;
using Random = System.Random;
namespace X10D.Unity.Tests.Drawing
{
public class RectangleTests
{
[UnityTest]
public IEnumerator ToUnityRect_ShouldReturnRect_WithEquivalentMembers()
{
var random = new Random();
var rectangle = new Rectangle(random.Next(), random.Next(), random.Next(), random.Next());
var rect = rectangle.ToUnityRect();
Assert.AreEqual(rectangle.X, rect.x);
Assert.AreEqual(rectangle.Y, rect.y);
Assert.AreEqual(rectangle.Width, rect.width);
Assert.AreEqual(rectangle.Height, rect.height);
yield break;
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 9c74177035d1452a8a7ca08c0a27124b
timeCreated: 1653743677

View File

@ -0,0 +1,26 @@
using System;
using System.Collections;
using System.Drawing;
using NUnit.Framework;
using UnityEngine.TestTools;
using X10D.Core;
using X10D.Unity.Drawing;
namespace X10D.Unity.Tests.Drawing
{
public class SizeFTests
{
[UnityTest]
public IEnumerator ToUnityVector2_ShouldReturnVector_WithEquivalentMembers()
{
var random = new Random();
var size = new SizeF(random.NextSingle(), random.NextSingle());
var vector = size.ToUnityVector2();
Assert.AreEqual(size.Width, vector.x, 1e-6f);
Assert.AreEqual(size.Height, vector.y, 1e-6f);
yield break;
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: b93fe56510de4ddcb9354bde7f10c362
timeCreated: 1653743377

View File

@ -0,0 +1,38 @@
using System;
using System.Collections;
using System.Drawing;
using NUnit.Framework;
using UnityEngine.TestTools;
using X10D.Unity.Drawing;
namespace X10D.Unity.Tests.Drawing
{
public class SizeTests
{
[UnityTest]
public IEnumerator ToUnityVector2_ShouldReturnVector_WithEquivalentMembers()
{
var random = new Random();
var size = new Size(random.Next(), random.Next());
var vector = size.ToUnityVector2();
Assert.AreEqual(size.Width, vector.x);
Assert.AreEqual(size.Height, vector.y);
yield break;
}
[UnityTest]
public IEnumerator ToUnityVector2Int_ShouldReturnVector_WithEquivalentMembers()
{
var random = new Random();
var size = new Size(random.Next(), random.Next());
var vector = size.ToUnityVector2Int();
Assert.AreEqual(size.Width, vector.x);
Assert.AreEqual(size.Height, vector.y);
yield break;
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: c748bfe02fce4b459df7ef2779c2a486
timeCreated: 1653743400

View File

@ -22,6 +22,38 @@ namespace X10D.Unity.Tests.Numerics
yield break;
}
[UnityTest]
public IEnumerator ToSystemPointF_ShouldReturnPoint_WithEquivalentMembers()
{
var random = new Random();
float x = random.NextSingle();
float y = random.NextSingle();
var vector = new Vector2(x, y);
var point = vector.ToSystemPointF();
Assert.AreEqual(vector.x, point.X, 1e-6f);
Assert.AreEqual(vector.y, point.Y, 1e-6f);
yield break;
}
[UnityTest]
public IEnumerator ToSystemSizeF_ShouldReturnPoint_WithEquivalentMembers()
{
var random = new Random();
float x = random.NextSingle();
float y = random.NextSingle();
var vector = new Vector2(x, y);
var point = vector.ToSystemSizeF();
Assert.AreEqual(vector.x, point.Width, 1e-6f);
Assert.AreEqual(vector.y, point.Height, 1e-6f);
yield break;
}
[UnityTest]
public IEnumerator ToSystemVector_ShouldReturnVector_WithEqualComponents()
{

View File

@ -0,0 +1,36 @@
using System.Diagnostics.Contracts;
using System.Drawing;
using System.Runtime.CompilerServices;
using UnityEngine;
namespace X10D.Unity.Drawing;
/// <summary>
/// Drawing-related extension methods for <see cref="Point" />.
/// </summary>
public static class PointExtensions
{
/// <summary>
/// Converts the current <see cref="Point" /> to a <see cref="Vector2" />.
/// </summary>
/// <param name="point">The point to convert.</param>
/// <returns>The resulting <see cref="Vector2" />.</returns>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector2 ToUnityVector2(this Point point)
{
return new Vector2(point.X, point.Y);
}
/// <summary>
/// Converts the current <see cref="Point" /> to a <see cref="Vector3" />.
/// </summary>
/// <param name="value">The point to convert.</param>
/// <returns>The resulting <see cref="Vector2Int" />.</returns>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector2Int ToUnityVector2Int(this Point value)
{
return new Vector2Int(value.X, value.Y);
}
}

View File

@ -0,0 +1,24 @@
using System.Diagnostics.Contracts;
using System.Drawing;
using System.Runtime.CompilerServices;
using UnityEngine;
namespace X10D.Unity.Drawing;
/// <summary>
/// Drawing-related extension methods for <see cref="PointF" />.
/// </summary>
public static class PointFExtensions
{
/// <summary>
/// Converts the current <see cref="Point" /> to a <see cref="Vector2" />.
/// </summary>
/// <param name="point">The point to convert.</param>
/// <returns>The resulting <see cref="Vector2" />.</returns>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector2 ToUnityVector2(this PointF point)
{
return new Vector2(point.X, point.Y);
}
}

View File

@ -0,0 +1,24 @@
using System.Diagnostics.Contracts;
using System.Drawing;
using System.Runtime.CompilerServices;
using UnityEngine;
namespace X10D.Unity.Drawing;
/// <summary>
/// Drawing-related extension methods for <see cref="Rect" />.
/// </summary>
public static class RectExtensions
{
/// <summary>
/// Converts the current <see cref="Rect" /> to a <see cref="RectangleF" />.
/// </summary>
/// <param name="rectangle">The rectangle to convert.</param>
/// <returns>The converted rectangle.</returns>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static RectangleF ToSystemRectangleF(this Rect rectangle)
{
return new RectangleF(rectangle.x, rectangle.y, rectangle.width, rectangle.height);
}
}

View File

@ -0,0 +1,24 @@
using System.Diagnostics.Contracts;
using System.Drawing;
using System.Runtime.CompilerServices;
using UnityEngine;
namespace X10D.Unity.Drawing;
/// <summary>
/// Drawing-related extension methods for <see cref="Rectangle" />.
/// </summary>
public static class RectangleExtensions
{
/// <summary>
/// Converts the current <see cref="Rectangle" /> to a <see cref="Rect" />.
/// </summary>
/// <param name="rectangle">The rectangle to convert.</param>
/// <returns>The converted rectangle.</returns>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Rect ToUnityRect(this Rectangle rectangle)
{
return new Rect(rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height);
}
}

View File

@ -0,0 +1,24 @@
using System.Diagnostics.Contracts;
using System.Drawing;
using System.Runtime.CompilerServices;
using UnityEngine;
namespace X10D.Unity.Drawing;
/// <summary>
/// Drawing-related extension methods for <see cref="RectangleF" />.
/// </summary>
public static class RectangleFExtensions
{
/// <summary>
/// Converts the current <see cref="RectangleF" /> to a <see cref="Rect" />.
/// </summary>
/// <param name="rectangle">The rectangle to convert.</param>
/// <returns>The converted rectangle.</returns>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Rect ToUnityRect(this RectangleF rectangle)
{
return new Rect(rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height);
}
}

View File

@ -0,0 +1,36 @@
using System.Diagnostics.Contracts;
using System.Drawing;
using System.Runtime.CompilerServices;
using UnityEngine;
namespace X10D.Unity.Drawing;
/// <summary>
/// Drawing-related extension methods for <see cref="Size" />.
/// </summary>
public static class SizeExtensions
{
/// <summary>
/// Converts the current <see cref="Size" /> to a <see cref="Vector2" />.
/// </summary>
/// <param name="size">The size to convert.</param>
/// <returns>The resulting <see cref="Vector2" />.</returns>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector2 ToUnityVector2(this Size size)
{
return new Vector2(size.Width, size.Height);
}
/// <summary>
/// Converts the current <see cref="Size" /> to a <see cref="Vector2Int" />.
/// </summary>
/// <param name="size">The size to convert.</param>
/// <returns>The resulting <see cref="Vector2Int" />.</returns>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector2Int ToUnityVector2Int(this Size size)
{
return new Vector2Int(size.Width, size.Height);
}
}

View File

@ -0,0 +1,24 @@
using System.Diagnostics.Contracts;
using System.Drawing;
using System.Runtime.CompilerServices;
using UnityEngine;
namespace X10D.Unity.Drawing;
/// <summary>
/// Drawing-related extension methods for <see cref="SizeF" />.
/// </summary>
public static class SizeFExtensions
{
/// <summary>
/// Converts the current <see cref="SizeF" /> to a <see cref="Vector2" />.
/// </summary>
/// <param name="size">The size to convert.</param>
/// <returns>The resulting <see cref="Vector2" />.</returns>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector2 ToUnityVector2(this SizeF size)
{
return new Vector2(size.Width, size.Height);
}
}

View File

@ -1,4 +1,5 @@
using System.Diagnostics.Contracts;
using System.Drawing;
using System.Runtime.CompilerServices;
using UnityEngine;
@ -21,6 +22,30 @@ public static class Vector2Extensions
y = vector.y;
}
/// <summary>
/// Converts the current <see cref="Vector2" /> into a <see cref="PointF" />.
/// </summary>
/// <param name="vector">The vector to convert.</param>
/// <returns>The resulting <see cref="PointF" />.</returns>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static PointF ToSystemPointF(this Vector2 vector)
{
return new PointF(vector.x, vector.y);
}
/// <summary>
/// Converts the current <see cref="Vector2" /> into a <see cref="SizeF" />.
/// </summary>
/// <param name="vector">The vector to convert.</param>
/// <returns>The resulting <see cref="SizeF" />.</returns>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static SizeF ToSystemSizeF(this Vector2 vector)
{
return new SizeF(vector.x, vector.y);
}
/// <summary>
/// Converts the current vector to a <see cref="System.Numerics.Vector2" />.
/// </summary>

View File

@ -0,0 +1,52 @@
using System.Diagnostics.Contracts;
using System.Drawing;
using System.Numerics;
using System.Runtime.CompilerServices;
namespace X10D.Drawing;
/// <summary>
/// Drawing-related extension methods for <see cref="Point" />.
/// </summary>
public static class PointExtensions
{
/// <summary>
/// Converts the current <see cref="Point" /> to a <see cref="Size" />.
/// </summary>
/// <param name="point">The point to convert.</param>
/// <returns>The resulting <see cref="Size" />.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static Size ToSize(this Point point)
{
return new Size(point.X, point.Y);
}
/// <summary>
/// Converts the current <see cref="Point" /> to a <see cref="SizeF" />.
/// </summary>
/// <param name="point">The point to convert.</param>
/// <returns>The resulting <see cref="SizeF" />.</returns>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static SizeF ToSizeF(this Point point)
{
return new SizeF(point.X, point.Y);
}
/// <summary>
/// Converts the current <see cref="Point" /> to a <see cref="Vector2" />.
/// </summary>
/// <param name="point">The point to convert.</param>
/// <returns>The resulting <see cref="Vector2" />.</returns>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector2 ToVector2(this Point point)
{
return new Vector2(point.X, point.Y);
}
}

View File

@ -0,0 +1,27 @@
using System.Diagnostics.Contracts;
using System.Drawing;
using System.Runtime.CompilerServices;
namespace X10D.Drawing;
/// <summary>
/// Drawing-related extension methods for <see cref="PointF" />.
/// </summary>
public static class PointFExtensions
{
/// <summary>
/// Converts the current <see cref="PointF" /> to a <see cref="SizeF" />.
/// </summary>
/// <param name="point">The point to convert.</param>
/// <returns>The resulting <see cref="SizeF" />.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static SizeF ToSizeF(this PointF point)
{
return new SizeF(point.X, point.Y);
}
}

View File

@ -0,0 +1,60 @@
using System.Diagnostics.Contracts;
using System.Drawing;
using System.Numerics;
using System.Runtime.CompilerServices;
namespace X10D.Drawing;
/// <summary>
/// Drawing-related extension methods for <see cref="Size" />.
/// </summary>
public static class SizeExtensions
{
/// <summary>
/// Converts the current <see cref="Size" /> to a <see cref="Point" />.
/// </summary>
/// <param name="size">The size to convert.</param>
/// <returns>The resulting <see cref="Point" />.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static Point ToPoint(this Size size)
{
return new Point(size.Width, size.Height);
}
/// <summary>
/// Converts the current <see cref="Size" /> to a <see cref="PointF" />.
/// </summary>
/// <param name="size">The size to convert.</param>
/// <returns>The resulting <see cref="PointF" />.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static PointF ToPointF(this Size size)
{
return new PointF(size.Width, size.Height);
}
/// <summary>
/// Converts the current <see cref="Size" /> to a <see cref="Vector2" />.
/// </summary>
/// <param name="size">The size to convert.</param>
/// <returns>The resulting <see cref="Vector2" />.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static Vector2 ToVector2(this Size size)
{
return new Vector2(size.Width, size.Height);
}
}

View File

@ -1,4 +1,5 @@
using System.Diagnostics.Contracts;
using System.Drawing;
using System.Numerics;
using System.Runtime.CompilerServices;
@ -21,6 +22,38 @@ public static class Vector2Extensions
y = vector.Y;
}
/// <summary>
/// Converts the current <see cref="Vector2" /> to a <see cref="PointF" />.
/// </summary>
/// <param name="vector">The vector to convert.</param>
/// <returns>The resulting <see cref="PointF" />.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static PointF ToPointF(this Vector2 vector)
{
return new PointF(vector.X, vector.Y);
}
/// <summary>
/// Converts the current <see cref="Vector2" /> to a <see cref="SizeF" />.
/// </summary>
/// <param name="vector">The vector to convert.</param>
/// <returns>The resulting <see cref="SizeF" />.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static SizeF ToSizeF(this Vector2 vector)
{
return new SizeF(vector.X, vector.Y);
}
/// <summary>
/// Returns a vector whose Y component is the same as the specified vector, and whose X component is a new value.
/// </summary>