Browse Source

Add shift operators to Point struct

pull/2633/head
Ynse Hoornenborg 1 year ago
parent
commit
81c727679f
  1. 36
      src/ImageSharp/Primitives/Point.cs
  2. 23
      tests/ImageSharp.Tests/Primitives/PointTests.cs

36
src/ImageSharp/Primitives/Point.cs

@ -144,6 +144,24 @@ public struct Point : IEquatable<Point>
public static Point operator /(Point left, int right)
=> new(left.X / right, left.Y / right);
/// <summary>
/// Shift <see cref="Point"/> to the right by a <see cref="int"/> amount producing <see cref="Point"/>.
/// </summary>
/// <param name="left">Shifted value of type <see cref="Point"/>.</param>
/// <param name="right">Shifted amount of type <see cref="int"/>.</param>
/// <returns>Result of type <see cref="Point"/>.</returns>
public static Point operator >>(Point left, int right)
=> new(left.X >> right, left.Y >> right);
/// <summary>
/// Shift <see cref="Point"/> to the left by a <see cref="int"/> amount producing <see cref="Point"/>.
/// </summary>
/// <param name="left">Shifted value of type <see cref="Point"/>.</param>
/// <param name="right">Shifted amount of type <see cref="int"/>.</param>
/// <returns>Result of type <see cref="Point"/>.</returns>
public static Point operator <<(Point left, int right)
=> new(left.X << right, left.Y << right);
/// <summary>
/// Compares two <see cref="Point"/> objects for equality.
/// </summary>
@ -267,6 +285,24 @@ public struct Point : IEquatable<Point>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Offset(Point point) => this.Offset(point.X, point.Y);
/// <summary>
/// Shifts the coordinate value of this <see cref="Point"/> to the right with the specified amount.
/// </summary>
/// <param name="point">The point to shift.</param>
/// <param name="bitCount">The number of bits to shift to the right.</param>
/// <returns>The <see cref="Point"/>.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Point ShiftRight(Point point, int bitCount) => new(unchecked(point.X >> bitCount), unchecked(point.Y >> bitCount));
/// <summary>
/// Shifts the coordinate value of this <see cref="Point"/> to the left with the specified amount.
/// </summary>
/// <param name="point">The point to shift.</param>
/// <param name="bitCount">The number of bits to shift to the left.</param>
/// <returns>The <see cref="Point"/>.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Point ShiftLeft(Point point, int bitCount) => new(unchecked(point.X << bitCount), unchecked(point.Y << bitCount));
/// <inheritdoc/>
public override int GetHashCode() => HashCode.Combine(this.X, this.Y);

23
tests/ImageSharp.Tests/Primitives/PointTests.cs

@ -4,7 +4,7 @@
using System.Globalization;
using System.Numerics;
namespace SixLabors.ImageSharp.Tests;
namespace SixLabors.ImageSharp.Tests.Primitives;
public class PointTests
{
@ -112,6 +112,27 @@ public class PointTests
Assert.Equal(subExpected, Point.Subtract(p, s));
}
[Theory]
[InlineData(int.MaxValue, int.MaxValue, 5)]
[InlineData(int.MinValue, int.MinValue, 4)]
[InlineData(int.MaxValue, int.MaxValue, 2)]
[InlineData(0, 0, 3)]
public void ShiftTest(int x, int y, int s)
{
Point rightExpected, leftExpected, p = new Point(x, y);
unchecked
{
rightExpected = new Point(x >> s, y >> s);
leftExpected = new Point(x << s, y << s);
}
Assert.Equal(rightExpected, p >> s);
Assert.Equal(leftExpected, p << s);
Assert.Equal(rightExpected, Point.ShiftRight(p, s));
Assert.Equal(leftExpected, Point.ShiftLeft(p, s));
}
[Theory]
[InlineData(float.MaxValue, float.MinValue)]
[InlineData(float.MinValue, float.MinValue)]

Loading…
Cancel
Save