Browse Source

Rectangle and RectangleF

af/merge-core
James Jackson-South 9 years ago
parent
commit
83c915b504
  1. 4
      src/ImageSharp.Drawing/Pens/Pen{TPixel}.cs
  2. 3
      src/ImageSharp.Drawing/Processors/DrawPathProcessor.cs
  3. 7
      src/ImageSharp/Numerics/Point.cs
  4. 440
      src/ImageSharp/Numerics/Rectangle.cs
  5. 422
      src/ImageSharp/Numerics/RectangleF.cs
  6. 266
      tests/ImageSharp.Tests/Numerics/RectangleFTests.cs
  7. 318
      tests/ImageSharp.Tests/Numerics/RectangleTests.cs

4
src/ImageSharp.Drawing/Pens/Pen{TPixel}.cs

@ -132,7 +132,7 @@ namespace ImageSharp.Drawing.Pens
{
this.brush = brush.CreateApplicator(sourcePixels, region, options);
this.halfWidth = width / 2;
this.RequiredRegion = RectangleF.Outset(region, width);
this.RequiredRegion = RectangleF.Inflate(region, width, width);
}
public override RectangleF RequiredRegion
@ -185,7 +185,7 @@ namespace ImageSharp.Drawing.Pens
this.pattern[i + 1] = this.totalLength;
}
this.RequiredRegion = RectangleF.Outset(region, width);
this.RequiredRegion = RectangleF.Inflate(region, width, width);
}
public override RectangleF RequiredRegion

3
src/ImageSharp.Drawing/Processors/DrawPathProcessor.cs

@ -6,7 +6,6 @@
namespace ImageSharp.Drawing.Processors
{
using System;
using System.Numerics;
using System.Threading.Tasks;
using ImageSharp.Memory;
@ -58,7 +57,7 @@ namespace ImageSharp.Drawing.Processors
{
using (PenApplicator<TPixel> applicator = this.Pen.CreateApplicator(source, this.Path.Bounds, this.Options))
{
Rectangle rect = RectangleF.Ceiling(applicator.RequiredRegion);
var rect = Rectangle.Ceiling(applicator.RequiredRegion);
int polyStartY = rect.Y - PaddingFactor;
int polyEndY = rect.Bottom + PaddingFactor;

7
src/ImageSharp/Numerics/Point.cs

@ -213,8 +213,11 @@ namespace ImageSharp
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Offset(int dx, int dy)
{
this.X += dx;
this.Y += dy;
unchecked
{
this.X += dx;
this.Y += dy;
}
}
/// <summary>

440
src/ImageSharp/Numerics/Rectangle.cs

@ -8,6 +8,7 @@ namespace ImageSharp
using System;
using System.ComponentModel;
using System.Numerics;
using System.Runtime.CompilerServices;
/// <summary>
/// Stores a set of four integers that represent the location and size of a rectangle.
@ -23,11 +24,6 @@ namespace ImageSharp
/// </summary>
public static readonly Rectangle Empty = default(Rectangle);
/// <summary>
/// The backing vector for SIMD support.
/// </summary>
private Vector4 backingVector;
/// <summary>
/// Initializes a new instance of the <see cref="Rectangle"/> struct.
/// </summary>
@ -37,7 +33,10 @@ namespace ImageSharp
/// <param name="height">The height of the rectangle.</param>
public Rectangle(int x, int y, int width, int height)
{
this.backingVector = new Vector4(x, y, width, height);
this.X = x;
this.Y = y;
this.Width = width;
this.Height = height;
}
/// <summary>
@ -51,197 +50,325 @@ namespace ImageSharp
/// </param>
public Rectangle(Point point, Size size)
{
this.backingVector = new Vector4(point.X, point.Y, size.Width, size.Height);
this.X = point.X;
this.Y = point.Y;
this.Width = size.Width;
this.Height = size.Height;
}
/// <summary>
/// Initializes a new instance of the <see cref="Rectangle"/> struct.
/// Gets or sets the x-coordinate of this <see cref="Rectangle"/>.
/// </summary>
/// <param name="topLeft">
/// The <see cref="Point"/> which specifies the rectangles top left point in a two-dimensional plane.
/// </param>
/// <param name="bottomRight">
/// The <see cref="Point"/>which specifies the rectangles bottom right point in a two-dimensional plane.
/// </param>
public Rectangle(Point topLeft, Point bottomRight)
{
this.backingVector = new Vector4(topLeft.X, topLeft.Y, bottomRight.X - topLeft.X, bottomRight.Y - topLeft.Y);
}
public int X { get; set; }
/// <summary>
/// Initializes a new instance of the <see cref="Rectangle"/> struct.
/// Gets or sets the y-coordinate of this <see cref="Rectangle"/>.
/// </summary>
/// <param name="vector">The vector.</param>
public Rectangle(Vector4 vector)
{
this.backingVector = vector;
}
public int Y { get; set; }
/// <summary>
/// Gets or sets the x-coordinate of this <see cref="Rectangle"/>.
/// Gets or sets the width of this <see cref="Rectangle"/>.
/// </summary>
public int Width { get; set; }
/// <summary>
/// Gets or sets the height of this <see cref="Rectangle"/>.
/// </summary>
public int X
public int Height { get; set; }
/// <summary>
/// Gets or sets the coordinates of the upper-left corner of the rectangular region represented by this <see cref="Rectangle"/>.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public Point Location
{
get
{
return (int)this.backingVector.X;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => new Point(this.X, this.Y);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
set
{
this.backingVector.X = value;
this.X = value.X;
this.Y = value.Y;
}
}
/// <summary>
/// Gets or sets the y-coordinate of this <see cref="Rectangle"/>.
/// Gets or sets the size of this <see cref="Rectangle"/>.
/// </summary>
public int Y
[EditorBrowsable(EditorBrowsableState.Never)]
public Size Size
{
get
{
return (int)this.backingVector.Y;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => new Size(this.Width, this.Height);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
set
{
this.backingVector.Y = value;
this.Width = value.Width;
this.Height = value.Height;
}
}
/// <summary>
/// Gets or sets the width of this <see cref="Rectangle"/>.
/// Gets a value indicating whether this <see cref="Rectangle"/> is empty.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public bool IsEmpty => this.Equals(Empty);
/// <summary>
/// Gets the y-coordinate of the top edge of this <see cref="Rectangle"/>.
/// </summary>
public int Width
public int Top
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
return (int)this.backingVector.Z;
return this.Y;
}
}
set
/// <summary>
/// Gets the x-coordinate of the right edge of this <see cref="Rectangle"/>.
/// </summary>
public int Right
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
this.backingVector.Z = value;
return unchecked(this.X + this.Width);
}
}
/// <summary>
/// Gets or sets the height of this <see cref="Rectangle"/>.
/// Gets the y-coordinate of the bottom edge of this <see cref="Rectangle"/>.
/// </summary>
public int Height
public int Bottom
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
return (int)this.backingVector.W;
return unchecked(this.Y + this.Height);
}
}
set
/// <summary>
/// Gets the x-coordinate of the left edge of this <see cref="Rectangle"/>.
/// </summary>
public int Left
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
this.backingVector.W = value;
return this.X;
}
}
/// <summary>
/// Gets the size of this <see cref="Rectangle"/>.
/// Creates a <see cref="RectangleF"/> with the coordinates of the specified <see cref="Rectangle"/>.
/// </summary>
public Size Size => new Size(this.Width, this.Height);
/// <param name="rectangle">The rectangle</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator RectangleF(Rectangle rectangle) => new RectangleF(rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height);
/// <summary>
/// Gets a value indicating whether this <see cref="Rectangle"/> is empty.
/// Creates a <see cref="Vector4"/> with the coordinates of the specified <see cref="Rectangle"/>.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public bool IsEmpty => this.Equals(Empty);
/// <param name="rectangle">The rectangle</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator Vector4(Rectangle rectangle) => new Vector4(rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height);
/// <summary>
/// Gets the y-coordinate of the top edge of this <see cref="Rectangle"/>.
/// Compares two <see cref="Rectangle"/> objects for equality.
/// </summary>
public int Top => this.Y;
/// <param name="left">The <see cref="Rectangle"/> on the left side of the operand.</param>
/// <param name="right">The <see cref="Rectangle"/> on the right side of the operand.</param>
/// <returns>
/// True if the current left is equal to the <paramref name="right"/> parameter; otherwise, false.
/// </returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator ==(Rectangle left, Rectangle right) => left.Equals(right);
/// <summary>
/// Gets the x-coordinate of the right edge of this <see cref="Rectangle"/>.
/// Compares two <see cref="Rectangle"/> objects for inequality.
/// </summary>
public int Right => this.X + this.Width;
/// <param name="left">The <see cref="Rectangle"/> on the left side of the operand.</param>
/// <param name="right">The <see cref="Rectangle"/> on the right side of the operand.</param>
/// <returns>
/// True if the current left is unequal to the <paramref name="right"/> parameter; otherwise, false.
/// </returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator !=(Rectangle left, Rectangle right) => !left.Equals(right);
/// <summary>
/// Gets the y-coordinate of the bottom edge of this <see cref="Rectangle"/>.
/// Creates a new <see cref="Rectangle"/> with the specified location and size. </summary>
/// <param name="left">The left coordinate of the rectangle</param>
/// <param name="top">The top coordinate of the rectangle</param>
/// <param name="right">The right coordinate of the rectangle</param>
/// <param name="bottom">The bottom coordinate of the rectangle</param>
/// <returns>The <see cref="Rectangle"/></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
// ReSharper disable once InconsistentNaming
public static Rectangle FromLTRB(int left, int top, int right, int bottom) => new Rectangle(left, top, unchecked(right - left), unchecked(bottom - top));
/// <summary>
/// Returns the center point of the given <see cref="Rectangle"/>
/// </summary>
public int Bottom => this.Y + this.Height;
/// <param name="rectangle">The rectangle</param>
/// <returns>The <see cref="Point"/></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Point Center(Rectangle rectangle) => new Point(rectangle.Left + (rectangle.Width / 2), rectangle.Top + (rectangle.Height / 2));
/// <summary>
/// Gets the x-coordinate of the left edge of this <see cref="Rectangle"/>.
/// Creates a rectangle that represents the intersection between <paramref name="a"/> and
/// <paramref name="b"/>. If there is no intersection, an empty rectangle is returned.
/// </summary>
public int Left => this.X;
/// <param name="a">The first rectangle</param>
/// <param name="b">The second rectangle</param>
/// <returns>The <see cref="Rectangle"/></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Rectangle Intersect(Rectangle a, Rectangle b)
{
int x1 = Math.Max(a.X, b.X);
int x2 = Math.Min(a.Right, b.Right);
int y1 = Math.Max(a.Y, b.Y);
int y2 = Math.Min(a.Bottom, b.Bottom);
if (x2 >= x1 && y2 >= y1)
{
return new Rectangle(x1, y1, x2 - x1, y2 - y1);
}
return Empty;
}
/// <summary>
/// Computes the sum of adding two rectangles.
/// Creates a <see cref="Rectangle"/> that is inflated by the specified amount.
/// </summary>
/// <param name="left">The rectangle on the left hand of the operand.</param>
/// <param name="right">The rectangle on the right hand of the operand.</param>
/// <returns>
/// The <see cref="Rectangle"/>
/// </returns>
public static Rectangle operator +(Rectangle left, Rectangle right)
/// <param name="rectangle">The rectangle</param>
/// <param name="x">The amount to inflate the width by</param>
/// <param name="y">The amount to inflate the height by</param>
/// <returns>A new <see cref="Rectangle"/></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Rectangle Inflate(Rectangle rectangle, int x, int y)
{
return new Rectangle(left.backingVector + right.backingVector);
Rectangle r = rectangle;
r.Inflate(x, y);
return r;
}
/// <summary>
/// Computes the difference left by subtracting one rectangle from another.
/// Converts a <see cref="RectangleF"/> to a <see cref="Rectangle"/> by performing a ceiling operation on all the coordinates.
/// </summary>
/// <param name="left">The rectangle on the left hand of the operand.</param>
/// <param name="right">The rectangle on the right hand of the operand.</param>
/// <returns>
/// The <see cref="Rectangle"/>
/// </returns>
public static Rectangle operator -(Rectangle left, Rectangle right)
/// <param name="rectangle">The rectangle</param>
/// <returns>The <see cref="Rectangle"/></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Rectangle Ceiling(RectangleF rectangle)
{
return new Rectangle(left.backingVector - right.backingVector);
unchecked
{
return new Rectangle(
(int)MathF.Ceiling(rectangle.X),
(int)MathF.Ceiling(rectangle.Y),
(int)MathF.Ceiling(rectangle.Width),
(int)MathF.Ceiling(rectangle.Height));
}
}
/// <summary>
/// Compares two <see cref="Rectangle"/> objects for equality.
/// Converts a <see cref="RectangleF"/> to a <see cref="Rectangle"/> by performing a truncate operation on all the coordinates.
/// </summary>
/// <param name="left">
/// The <see cref="Rectangle"/> on the left side of the operand.
/// </param>
/// <param name="right">
/// The <see cref="Rectangle"/> on the right side of the operand.
/// </param>
/// <returns>
/// True if the current left is equal to the <paramref name="right"/> parameter; otherwise, false.
/// </returns>
public static bool operator ==(Rectangle left, Rectangle right)
/// <param name="rectangle">The rectangle</param>
/// <returns>The <see cref="Rectangle"/></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Rectangle Truncate(RectangleF rectangle)
{
return left.Equals(right);
unchecked
{
return new Rectangle(
(int)rectangle.X,
(int)rectangle.Y,
(int)rectangle.Width,
(int)rectangle.Height);
}
}
/// <summary>
/// Compares two <see cref="Rectangle"/> objects for inequality.
/// Converts a <see cref="RectangleF"/> to a <see cref="Rectangle"/> by performing a round operation on all the coordinates.
/// </summary>
/// <param name="left">
/// The <see cref="Rectangle"/> on the left side of the operand.
/// </param>
/// <param name="right">
/// The <see cref="Rectangle"/> on the right side of the operand.
/// </param>
/// <returns>
/// True if the current left is unequal to the <paramref name="right"/> parameter; otherwise, false.
/// </returns>
public static bool operator !=(Rectangle left, Rectangle right)
/// <param name="rectangle">The rectangle</param>
/// <returns>The <see cref="Rectangle"/></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Rectangle Round(RectangleF rectangle)
{
return !left.Equals(right);
unchecked
{
return new Rectangle(
(int)MathF.Round(rectangle.X),
(int)MathF.Round(rectangle.Y),
(int)MathF.Round(rectangle.Width),
(int)MathF.Round(rectangle.Height));
}
}
/// <summary>
/// Returns the center point of the given <see cref="Rectangle"/>
/// Creates a rectangle that represents the union between <paramref name="a"/> and <paramref name="b"/>.
/// </summary>
/// <param name="a">The first rectangle</param>
/// <param name="b">The second rectangle</param>
/// <returns>The <see cref="Rectangle"/></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Rectangle Union(Rectangle a, Rectangle b)
{
int x1 = Math.Min(a.X, b.X);
int x2 = Math.Max(a.Right, b.Right);
int y1 = Math.Min(a.Y, b.Y);
int y2 = Math.Max(a.Bottom, b.Bottom);
return new Rectangle(x1, y1, x2 - x1, y2 - y1);
}
/// <summary>
/// Creates a Rectangle that represents the intersection between this Rectangle and the <paramref name="rectangle"/>.
/// </summary>
/// <param name="rectangle">The rectangle</param>
/// <returns><see cref="Point"/></returns>
public static Point Center(Rectangle rectangle)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Intersect(Rectangle rectangle)
{
return new Point(rectangle.Left + (rectangle.Width / 2), rectangle.Top + (rectangle.Height / 2));
Rectangle result = Intersect(rectangle, this);
this.X = result.X;
this.Y = result.Y;
this.Width = result.Width;
this.Height = result.Height;
}
/// <summary>
/// Inflates this <see cref="Rectangle"/> by the specified amount.
/// </summary>
/// <param name="width">The width</param>
/// <param name="height">The height</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Inflate(int width, int height)
{
unchecked
{
this.X -= width;
this.Y -= height;
this.Width += 2 * width;
this.Height += 2 * height;
}
}
/// <summary>
/// Inflates this <see cref="Rectangle"/> by the specified amount.
/// </summary>
/// <param name="size">The size</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Inflate(Size size) => this.Inflate(size.Width, size.Height);
/// <summary>
/// Determines if the specfied point is contained within the rectangular region defined by
/// this <see cref="Rectangle"/>.
@ -249,33 +376,63 @@ namespace ImageSharp
/// <param name="x">The x-coordinate of the given point.</param>
/// <param name="y">The y-coordinate of the given point.</param>
/// <returns>The <see cref="bool"/></returns>
public bool Contains(int x, int y)
{
// TODO: SIMD?
return this.X <= x
&& x < this.Right
&& this.Y <= y
&& y < this.Bottom;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Contains(int x, int y) => this.X <= x && x < this.Right && this.Y <= y && y < this.Bottom;
/// <summary>
/// Determines if the specified point is contained within the rectangular region defined by this <see cref="Rectangle"/> .
/// </summary>
/// <param name="point">The point</param>
/// <returns>The <see cref="bool"/></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Contains(Point point) => this.Contains(point.X, point.Y);
/// <summary>
/// Determines if the rectangular region represented by <paramref name="rectangle"/> is entirely contained
/// within the rectangular region represented by this <see cref="Rectangle"/> .
/// </summary>
/// <param name="rectangle">The rectangle</param>
/// <returns>The <see cref="bool"/></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Contains(Rectangle rectangle) =>
(this.X <= rectangle.X) && (rectangle.Right <= this.Right) &&
(this.Y <= rectangle.Y) && (rectangle.Bottom <= this.Bottom);
/// <summary>
/// Determines if the specfied <see cref="Rectangle"/> intersects the rectangular region defined by
/// this <see cref="Rectangle"/>.
/// </summary>
/// <param name="rect">The other Rectange </param>
/// <param name="rectangle">The other Rectange </param>
/// <returns>The <see cref="bool"/></returns>
public bool Intersects(Rectangle rect)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool IntersectsWith(Rectangle rectangle) =>
(rectangle.X < this.Right) && (this.X < rectangle.Right) &&
(rectangle.Y < this.Bottom) && (this.Y < rectangle.Bottom);
/// <summary>
/// Adjusts the location of this rectangle by the specified amount.
/// </summary>
/// <param name="point">The point</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Offset(Point point) => this.Offset(point.X, point.Y);
/// <summary>
/// Adjusts the location of this rectangle by the specified amount.
/// </summary>
/// <param name="dx">The amount to offset the x-coordinate.</param>
/// <param name="dy">The amount to offset the y-coordinate.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Offset(int dx, int dy)
{
return rect.Left <= this.Right && rect.Right >= this.Left
&&
rect.Top <= this.Bottom && rect.Bottom >= this.Top;
unchecked
{
this.X += dx;
this.Y += dy;
}
}
/// <inheritdoc/>
public override int GetHashCode()
{
return this.GetHashCode(this);
}
public override int GetHashCode() => this.GetHashCode(this);
/// <inheritdoc/>
public override string ToString()
@ -285,39 +442,26 @@ namespace ImageSharp
return "Rectangle [ Empty ]";
}
return
$"Rectangle [ X={this.X}, Y={this.Y}, Width={this.Width}, Height={this.Height} ]";
return $"Rectangle [ X={this.X}, Y={this.Y}, Width={this.Width}, Height={this.Height} ]";
}
/// <inheritdoc/>
public override bool Equals(object obj)
{
if (obj is Rectangle)
{
return this.Equals((Rectangle)obj);
}
return false;
}
public override bool Equals(object obj) => obj is Rectangle && this.Equals((Rectangle)obj);
/// <inheritdoc/>
public bool Equals(Rectangle other)
{
return this.backingVector.Equals(other.backingVector);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Equals(Rectangle other) => this.X == other.X && this.Y == other.Y && this.Width == other.Width && this.Height == other.Height;
/// <summary>
/// Returns the hash code for this instance.
/// </summary>
/// <param name="rectangle">
/// The instance of <see cref="Rectangle"/> to return the hash code for.
/// </param>
/// <returns>
/// A 32-bit signed integer that is the hash code for this instance.
/// </returns>
private int GetHashCode(Rectangle rectangle)
{
return rectangle.backingVector.GetHashCode();
unchecked
{
int hashCode = rectangle.X;
hashCode = (hashCode * 397) ^ rectangle.Y;
hashCode = (hashCode * 397) ^ rectangle.Width;
hashCode = (hashCode * 397) ^ rectangle.Height;
return hashCode;
}
}
}
}
}

422
src/ImageSharp/Numerics/RectangleF.cs

@ -8,9 +8,10 @@ namespace ImageSharp
using System;
using System.ComponentModel;
using System.Numerics;
using System.Runtime.CompilerServices;
/// <summary>
/// Stores a set of four integers that represent the location and size of a rectangle.
/// Stores a set of four single precision floating points that represent the location and size of a rectangle.
/// </summary>
/// <remarks>
/// This struct is fully mutable. This is done (against the guidelines) for the sake of performance,
@ -19,15 +20,10 @@ namespace ImageSharp
public struct RectangleF : IEquatable<RectangleF>
{
/// <summary>
/// Represents a <see cref="Rectangle"/> that has X, Y, Width, and Height values set to zero.
/// Represents a <see cref="RectangleF"/> that has X, Y, Width, and Height values set to zero.
/// </summary>
public static readonly RectangleF Empty = default(RectangleF);
/// <summary>
/// The backing vector for SIMD support.
/// </summary>
private Vector4 backingVector;
/// <summary>
/// Initializes a new instance of the <see cref="RectangleF"/> struct.
/// </summary>
@ -37,221 +33,291 @@ namespace ImageSharp
/// <param name="height">The height of the rectangle.</param>
public RectangleF(float x, float y, float width, float height)
{
this.backingVector = new Vector4(x, y, width, height);
this.X = x;
this.Y = y;
this.Width = width;
this.Height = height;
}
/// <summary>
/// Initializes a new instance of the <see cref="RectangleF"/> struct.
/// </summary>
/// <param name="vector">The vector.</param>
public RectangleF(Vector4 vector)
/// <param name="point">
/// The <see cref="Point"/> which specifies the rectangles point in a two-dimensional plane.
/// </param>
/// <param name="size">
/// The <see cref="Size"/> which specifies the rectangles height and width.
/// </param>
public RectangleF(PointF point, SizeF size)
{
this.backingVector = vector;
this.X = point.X;
this.Y = point.Y;
this.Width = size.Width;
this.Height = size.Height;
}
/// <summary>
/// Gets or sets the x-coordinate of this <see cref="RectangleF"/>.
/// </summary>
public float X
public float X { get; set; }
/// <summary>
/// Gets or sets the y-coordinate of this <see cref="RectangleF"/>.
/// </summary>
public float Y { get; set; }
/// <summary>
/// Gets or sets the width of this <see cref="RectangleF"/>.
/// </summary>
public float Width { get; set; }
/// <summary>
/// Gets or sets the height of this <see cref="RectangleF"/>.
/// </summary>
public float Height { get; set; }
/// <summary>
/// Gets or sets the coordinates of the upper-left corner of the rectangular region represented by this <see cref="RectangleF"/>.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public PointF Location
{
get
{
return this.backingVector.X;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => new PointF(this.X, this.Y);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
set
{
this.backingVector.X = value;
this.X = value.X;
this.Y = value.Y;
}
}
/// <summary>
/// Gets or sets the y-coordinate of this <see cref="RectangleF"/>.
/// Gets or sets the size of this <see cref="RectangleF"/>.
/// </summary>
public float Y
[EditorBrowsable(EditorBrowsableState.Never)]
public SizeF Size
{
get
{
return this.backingVector.Y;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => new SizeF(this.Width, this.Height);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
set
{
this.backingVector.Y = value;
this.Width = value.Width;
this.Height = value.Height;
}
}
/// <summary>
/// Gets or sets the width of this <see cref="RectangleF"/>.
/// Gets a value indicating whether this <see cref="RectangleF"/> is empty.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public bool IsEmpty => (this.Width <= 0) || (this.Height <= 0);
/// <summary>
/// Gets the y-coordinate of the top edge of this <see cref="RectangleF"/>.
/// </summary>
public float Width
public float Top
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
return this.backingVector.Z;
}
set
{
this.backingVector.Z = value;
return this.Y;
}
}
/// <summary>
/// Gets or sets the height of this <see cref="RectangleF"/>.
/// Gets the x-coordinate of the right edge of this <see cref="RectangleF"/>.
/// </summary>
public float Height
public float Right
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
return this.backingVector.W;
return this.X + this.Width;
}
}
set
/// <summary>
/// Gets the y-coordinate of the bottom edge of this <see cref="RectangleF"/>.
/// </summary>
public float Bottom
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
this.backingVector.W = value;
return this.Y + this.Height;
}
}
/// <summary>
/// Gets a value indicating whether this <see cref="RectangleF"/> is empty.
/// Gets the x-coordinate of the left edge of this <see cref="RectangleF"/>.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public bool IsEmpty => this.Equals(Empty);
public float Left
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
return this.X;
}
}
/// <summary>
/// Gets the y-coordinate of the top edge of this <see cref="RectangleF"/>.
/// Creates a <see cref="RectangleF"/> with the coordinates of the specified <see cref="Vector4"/>.
/// </summary>
public float Top => this.Y;
/// <param name="vector">The rectangle</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator RectangleF(Vector4 vector) => new RectangleF(vector.X, vector.Y, vector.Z, vector.W);
/// <summary>
/// Gets the x-coordinate of the right edge of this <see cref="RectangleF"/>.
/// Creates a <see cref="Vector4"/> with the coordinates of the specified <see cref="RectangleF"/>.
/// </summary>
public float Right => this.X + this.Width;
/// <param name="rectangle">The rectangle</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator Vector4(RectangleF rectangle) => new Vector4(rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height);
/// <summary>
/// Gets the y-coordinate of the bottom edge of this <see cref="RectangleF"/>.
/// Creates a <see cref="Rectangle"/> with the coordinates of the specified <see cref="RectangleF"/> by truncating each coordinate.
/// </summary>
public float Bottom => this.Y + this.Height;
/// <param name="rectangle">The rectangle</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static explicit operator Rectangle(RectangleF rectangle) => Rectangle.Truncate(rectangle);
/// <summary>
/// Gets the x-coordinate of the left edge of this <see cref="RectangleF"/>.
/// Compares two <see cref="RectangleF"/> objects for equality.
/// </summary>
public float Left => this.X;
/// <param name="left">The <see cref="RectangleF"/> on the left side of the operand.</param>
/// <param name="right">The <see cref="RectangleF"/> on the right side of the operand.</param>
/// <returns>
/// True if the current left is equal to the <paramref name="right"/> parameter; otherwise, false.
/// </returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator ==(RectangleF left, RectangleF right) => left.Equals(right);
/// <summary>
/// Performs an implicit conversion from <see cref="Rectangle"/> to <see cref="RectangleF"/>.
/// Compares two <see cref="RectangleF"/> objects for inequality.
/// </summary>
/// <param name="d">The d.</param>
/// <param name="left">The <see cref="RectangleF"/> on the left side of the operand.</param>
/// <param name="right">The <see cref="RectangleF"/> on the right side of the operand.</param>
/// <returns>
/// The result of the conversion.
/// True if the current left is unequal to the <paramref name="right"/> parameter; otherwise, false.
/// </returns>
public static implicit operator RectangleF(Rectangle d)
{
return new RectangleF(d.Left, d.Top, d.Width, d.Height);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator !=(RectangleF left, RectangleF right) => !left.Equals(right);
/// <summary>
/// Computes the sum of adding two rectangles.
/// Creates a new <see cref="RectangleF"/> with the specified location and size. </summary>
/// <param name="left">The left coordinate of the rectangle</param>
/// <param name="top">The top coordinate of the rectangle</param>
/// <param name="right">The right coordinate of the rectangle</param>
/// <param name="bottom">The bottom coordinate of the rectangle</param>
/// <returns>The <see cref="RectangleF"/></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
// ReSharper disable once InconsistentNaming
public static RectangleF FromLTRB(float left, float top, float right, float bottom) => new RectangleF(left, top, right - left, bottom - top);
/// <summary>
/// Returns the center point of the given <see cref="RectangleF"/>
/// </summary>
/// <param name="left">The rectangle on the left hand of the operand.</param>
/// <param name="right">The rectangle on the right hand of the operand.</param>
/// <returns>
/// The <see cref="RectangleF"/>
/// </returns>
public static RectangleF operator +(RectangleF left, RectangleF right)
{
return new RectangleF(left.backingVector + right.backingVector);
}
/// <param name="rectangle">The rectangle</param>
/// <returns>The <see cref="Point"/></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static PointF Center(RectangleF rectangle) => new PointF(rectangle.Left + (rectangle.Width / 2), rectangle.Top + (rectangle.Height / 2));
/// <summary>
/// Computes the difference left by subtracting one rectangle from another.
/// Creates a rectangle that represents the intersection between <paramref name="a"/> and
/// <paramref name="b"/>. If there is no intersection, an empty rectangle is returned.
/// </summary>
/// <param name="left">The rectangle on the left hand of the operand.</param>
/// <param name="right">The rectangle on the right hand of the operand.</param>
/// <returns>
/// The <see cref="RectangleF"/>
/// </returns>
public static RectangleF operator -(RectangleF left, RectangleF right)
/// <param name="a">The first rectangle</param>
/// <param name="b">The second rectangle</param>
/// <returns>The <see cref="RectangleF"/></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static RectangleF Intersect(RectangleF a, RectangleF b)
{
return new RectangleF(left.backingVector - right.backingVector);
float x1 = MathF.Max(a.X, b.X);
float x2 = MathF.Min(a.Right, b.Right);
float y1 = MathF.Max(a.Y, b.Y);
float y2 = MathF.Min(a.Bottom, b.Bottom);
if (x2 >= x1 && y2 >= y1)
{
return new RectangleF(x1, y1, x2 - x1, y2 - y1);
}
return Empty;
}
/// <summary>
/// Compares two <see cref="RectangleF"/> objects for equality.
/// Creates a <see cref="RectangleF"/> that is inflated by the specified amount.
/// </summary>
/// <param name="left">
/// The <see cref="RectangleF"/> on the left side of the operand.
/// </param>
/// <param name="right">
/// The <see cref="RectangleF"/> on the right side of the operand.
/// </param>
/// <returns>
/// True if the current left is equal to the <paramref name="right"/> parameter; otherwise, false.
/// </returns>
public static bool operator ==(RectangleF left, RectangleF right)
/// <param name="rectangle">The rectangle</param>
/// <param name="x">The amount to inflate the width by</param>
/// <param name="y">The amount to inflate the height by</param>
/// <returns>A new <see cref="RectangleF"/></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static RectangleF Inflate(RectangleF rectangle, float x, float y)
{
return left.Equals(right);
RectangleF r = rectangle;
r.Inflate(x, y);
return r;
}
/// <summary>
/// Compares two <see cref="RectangleF"/> objects for inequality.
/// Creates a rectangle that represents the union between <paramref name="a"/> and <paramref name="b"/>.
/// </summary>
/// <param name="left">
/// The <see cref="RectangleF"/> on the left side of the operand.
/// </param>
/// <param name="right">
/// The <see cref="RectangleF"/> on the right side of the operand.
/// </param>
/// <returns>
/// True if the current left is unequal to the <paramref name="right"/> parameter; otherwise, false.
/// </returns>
public static bool operator !=(RectangleF left, RectangleF right)
/// <param name="a">The first rectangle</param>
/// <param name="b">The second rectangle</param>
/// <returns>The <see cref="RectangleF"/></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static RectangleF Union(RectangleF a, RectangleF b)
{
return !left.Equals(right);
float x1 = MathF.Min(a.X, b.X);
float x2 = MathF.Max(a.Right, b.Right);
float y1 = MathF.Min(a.Y, b.Y);
float y2 = MathF.Max(a.Bottom, b.Bottom);
return new RectangleF(x1, y1, x2 - x1, y2 - y1);
}
/// <summary>
/// Returns the center point of the given <see cref="RectangleF"/>
/// Creates a RectangleF that represents the intersection between this RectangleF and the <paramref name="rectangle"/>.
/// </summary>
/// <param name="rectangle">The rectangle</param>
/// <returns><see cref="Point"/></returns>
public static Vector2 Center(RectangleF rectangle)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Intersect(RectangleF rectangle)
{
return new Vector2(rectangle.Left + (rectangle.Width / 2), rectangle.Top + (rectangle.Height / 2));
RectangleF result = Intersect(rectangle, this);
this.X = result.X;
this.Y = result.Y;
this.Width = result.Width;
this.Height = result.Height;
}
/// <summary>
/// Rounds the points away from the center this into a <see cref="Rectangle"/>
/// by rounding the dimensions to the nerent integer ensuring that the new rectangle is
/// never smaller than the source <see cref="RectangleF"/>
/// Inflates this <see cref="RectangleF"/> by the specified amount.
/// </summary>
/// <param name="source">The source area to round out</param>
/// <returns>
/// The smallest <see cref="Rectangle"/> that the <see cref="RectangleF"/> will fit inside.
/// </returns>
public static Rectangle Ceiling(RectangleF source)
/// <param name="width">The width</param>
/// <param name="height">The height</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Inflate(float width, float height)
{
int y = (int)Math.Floor(source.Y);
int width = (int)Math.Ceiling(source.Width);
int x = (int)Math.Floor(source.X);
int height = (int)Math.Ceiling(source.Height);
return new Rectangle(x, y, width, height);
this.X -= width;
this.Y -= height;
this.Width += 2 * width;
this.Height += 2 * height;
}
/// <summary>
/// Outsets the specified region.
/// Inflates this <see cref="RectangleF"/> by the specified amount.
/// </summary>
/// <param name="region">The region.</param>
/// <param name="width">The width.</param>
/// <returns>
/// The <see cref="RectangleF"/> with all dimensions move away from the center by the offset.
/// </returns>
public static RectangleF Outset(RectangleF region, float width)
{
float dblWidth = width * 2;
return new RectangleF(region.X - width, region.Y - width, region.Width + dblWidth, region.Height + dblWidth);
}
/// <param name="size">The size</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Inflate(SizeF size) => this.Inflate(size.Width, size.Height);
/// <summary>
/// Determines if the specfied point is contained within the rectangular region defined by
@ -260,75 +326,89 @@ namespace ImageSharp
/// <param name="x">The x-coordinate of the given point.</param>
/// <param name="y">The y-coordinate of the given point.</param>
/// <returns>The <see cref="bool"/></returns>
public bool Contains(float x, float y)
{
// TODO: SIMD?
return this.X <= x
&& x < this.Right
&& this.Y <= y
&& y < this.Bottom;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Contains(float x, float y) => this.X <= x && x < this.Right && this.Y <= y && y < this.Bottom;
/// <summary>
/// Determines if the specified point is contained within the rectangular region defined by this <see cref="RectangleF"/> .
/// </summary>
/// <param name="point">The point</param>
/// <returns>The <see cref="bool"/></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Contains(PointF point) => this.Contains(point.X, point.Y);
/// <summary>
/// Determines if the rectangular region represented by <paramref name="rectangle"/> is entirely contained
/// within the rectangular region represented by this <see cref="RectangleF"/> .
/// </summary>
/// <param name="rectangle">The rectangle</param>
/// <returns>The <see cref="bool"/></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Contains(RectangleF rectangle) =>
(this.X <= rectangle.X) && (rectangle.Right <= this.Right) &&
(this.Y <= rectangle.Y) && (rectangle.Bottom <= this.Bottom);
/// <summary>
/// Determines if the specfied <see cref="Rectangle"/> intersects the rectangular region defined by
/// this <see cref="Rectangle"/>.
/// Determines if the specfied <see cref="RectangleF"/> intersects the rectangular region defined by
/// this <see cref="RectangleF"/>.
/// </summary>
/// <param name="rect">The other Rectange </param>
/// <param name="rectangle">The other Rectange </param>
/// <returns>The <see cref="bool"/></returns>
public bool Intersects(RectangleF rect)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool IntersectsWith(RectangleF rectangle) =>
(rectangle.X < this.Right) && (this.X < rectangle.Right) &&
(rectangle.Y < this.Bottom) && (this.Y < rectangle.Bottom);
/// <summary>
/// Adjusts the location of this rectangle by the specified amount.
/// </summary>
/// <param name="point">The point</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Offset(PointF point) => this.Offset(point.X, point.Y);
/// <summary>
/// Adjusts the location of this rectangle by the specified amount.
/// </summary>
/// <param name="dx">The amount to offset the x-coordinate.</param>
/// <param name="dy">The amount to offset the y-coordinate.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Offset(float dx, float dy)
{
return rect.Left <= this.Right && rect.Right >= this.Left
&&
rect.Top <= this.Bottom && rect.Bottom >= this.Top;
this.X += dx;
this.Y += dy;
}
/// <inheritdoc/>
public override int GetHashCode()
{
return this.GetHashCode(this);
}
public override int GetHashCode() => this.GetHashCode(this);
/// <inheritdoc/>
public override string ToString()
{
if (this.IsEmpty)
{
return "Rectangle [ Empty ]";
return "RectangleF [ Empty ]";
}
return
$"Rectangle [ X={this.X}, Y={this.Y}, Width={this.Width}, Height={this.Height} ]";
return $"RectangleF [ X={this.X}, Y={this.Y}, Width={this.Width}, Height={this.Height} ]";
}
/// <inheritdoc/>
public override bool Equals(object obj)
{
if (obj is RectangleF)
{
return this.Equals((RectangleF)obj);
}
return false;
}
public override bool Equals(object obj) => obj is RectangleF && this.Equals((RectangleF)obj);
/// <inheritdoc/>
public bool Equals(RectangleF other)
{
return this.backingVector.Equals(other.backingVector);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Equals(RectangleF other) => this.X.Equals(other.X) && this.Y.Equals(other.Y) && this.Width.Equals(other.Width) && this.Height.Equals(other.Height);
/// <summary>
/// Returns the hash code for this instance.
/// </summary>
/// <param name="rectangle">
/// The instance of <see cref="RectangleF"/> to return the hash code for.
/// </param>
/// <returns>
/// A 32-bit signed integer that is the hash code for this instance.
/// </returns>
private int GetHashCode(RectangleF rectangle)
{
return rectangle.backingVector.GetHashCode();
unchecked
{
int hashCode = rectangle.X.GetHashCode();
hashCode = (hashCode * 397) ^ rectangle.Y.GetHashCode();
hashCode = (hashCode * 397) ^ rectangle.Width.GetHashCode();
hashCode = (hashCode * 397) ^ rectangle.Height.GetHashCode();
return hashCode;
}
}
}
}
}

266
tests/ImageSharp.Tests/Numerics/RectangleFTests.cs

@ -0,0 +1,266 @@
// <copyright file="RectangleFTests.cs" company="James Jackson-South">
// Copyright (c) James Jackson-South and contributors.
// Licensed under the Apache License, Version 2.0.
// </copyright>
namespace ImageSharp.Tests
{
using System;
using System.Globalization;
using System.Reflection;
using Xunit;
/// <summary>
/// Tests the <see cref="RectangleF"/> struct.
/// </summary>
public class RectangleFTests
{
[Fact]
public void DefaultConstructorTest()
{
Assert.Equal(RectangleF.Empty, new RectangleF());
}
[Theory]
[InlineData(0, 0, 0, 0)]
[InlineData(float.MaxValue, float.MinValue, float.MinValue, float.MaxValue)]
[InlineData(float.MaxValue, 0, 0, float.MaxValue)]
[InlineData(0, float.MinValue, float.MaxValue, 0)]
public void NonDefaultConstructorTest(float x, float y, float width, float height)
{
var rect1 = new RectangleF(x, y, width, height);
var p = new PointF(x, y);
var s = new SizeF(width, height);
var rect2 = new RectangleF(p, s);
Assert.Equal(rect1, rect2);
}
[Theory]
[InlineData(0, 0, 0, 0)]
[InlineData(float.MaxValue, float.MinValue, float.MinValue, float.MaxValue)]
[InlineData(float.MaxValue, 0, 0, float.MaxValue)]
[InlineData(0, float.MinValue, float.MaxValue, 0)]
public void FromLTRBTest(float left, float top, float right, float bottom)
{
var expected = new RectangleF(left, top, right - left, bottom - top);
var actual = RectangleF.FromLTRB(left, top, right, bottom);
Assert.Equal(expected, actual);
}
[Theory]
[InlineData(0, 0, 0, 0)]
[InlineData(float.MaxValue, float.MinValue, float.MinValue, float.MaxValue)]
[InlineData(float.MaxValue, 0, 0, float.MaxValue)]
[InlineData(0, float.MinValue, float.MaxValue, 0)]
public void DimensionsTest(float x, float y, float width, float height)
{
var rect = new RectangleF(x, y, width, height);
var p = new PointF(x, y);
var s = new SizeF(width, height);
Assert.Equal(p, rect.Location);
Assert.Equal(s, rect.Size);
Assert.Equal(x, rect.X);
Assert.Equal(y, rect.Y);
Assert.Equal(width, rect.Width);
Assert.Equal(height, rect.Height);
Assert.Equal(x, rect.Left);
Assert.Equal(y, rect.Top);
Assert.Equal(x + width, rect.Right);
Assert.Equal(y + height, rect.Bottom);
}
[Fact]
public void IsEmptyTest()
{
Assert.True(RectangleF.Empty.IsEmpty);
Assert.True(new RectangleF().IsEmpty);
Assert.True(new RectangleF(1, -2, -10, 10).IsEmpty);
Assert.True(new RectangleF(1, -2, 10, -10).IsEmpty);
Assert.True(new RectangleF(1, -2, 0, 0).IsEmpty);
Assert.False(new RectangleF(0, 0, 10, 10).IsEmpty);
}
[Theory]
[InlineData(0, 0)]
[InlineData(float.MaxValue, float.MinValue)]
public static void LocationSetTest(float x, float y)
{
var point = new PointF(x, y);
var rect = new RectangleF(10, 10, 10, 10) { Location = point };
Assert.Equal(point, rect.Location);
Assert.Equal(point.X, rect.X);
Assert.Equal(point.Y, rect.Y);
}
[Theory]
[InlineData(0, 0)]
[InlineData(float.MaxValue, float.MinValue)]
public static void SizeSetTest(float x, float y)
{
var size = new SizeF(x, y);
var rect = new RectangleF(10, 10, 10, 10) { Size = size };
Assert.Equal(size, rect.Size);
Assert.Equal(size.Width, rect.Width);
Assert.Equal(size.Height, rect.Height);
}
[Theory]
[InlineData(float.MaxValue, float.MinValue, float.MinValue, float.MaxValue)]
[InlineData(float.MaxValue, 0, 0, float.MaxValue)]
[InlineData(0, float.MinValue, float.MaxValue, 0)]
public void EqualityTest(float x, float y, float width, float height)
{
var rect1 = new RectangleF(x, y, width, height);
var rect2 = new RectangleF(width, height, x, y);
Assert.True(rect1 != rect2);
Assert.False(rect1 == rect2);
Assert.False(rect1.Equals(rect2));
Assert.False(rect1.Equals((object)rect2));
}
[Fact]
public static void EqualityTestNotRectangleF()
{
var rectangle = new RectangleF(0, 0, 0, 0);
Assert.False(rectangle.Equals(null));
Assert.False(rectangle.Equals(0));
// If RectangleF implements IEquatable<RectangleF> (e.g. in .NET Core), then classes that are implicitly
// convertible to RectangleF can potentially be equal.
// See https://github.com/dotnet/corefx/issues/5255.
bool expectsImplicitCastToRectangleF = typeof(IEquatable<RectangleF>).IsAssignableFrom(rectangle.GetType());
Assert.Equal(expectsImplicitCastToRectangleF, rectangle.Equals(new Rectangle(0, 0, 0, 0)));
Assert.False(rectangle.Equals((object)new Rectangle(0, 0, 0, 0))); // No implicit cast
}
[Fact]
public static void GetHashCodeTest()
{
var rect1 = new RectangleF(10, 10, 10, 10);
var rect2 = new RectangleF(10, 10, 10, 10);
Assert.Equal(rect1.GetHashCode(), rect2.GetHashCode());
Assert.NotEqual(rect1.GetHashCode(), new RectangleF(20, 10, 10, 10).GetHashCode());
Assert.NotEqual(rect1.GetHashCode(), new RectangleF(10, 20, 10, 10).GetHashCode());
Assert.NotEqual(rect1.GetHashCode(), new RectangleF(10, 10, 20, 10).GetHashCode());
Assert.NotEqual(rect1.GetHashCode(), new RectangleF(10, 10, 10, 20).GetHashCode());
}
[Theory]
[InlineData(float.MaxValue, float.MinValue, float.MinValue, float.MaxValue)]
[InlineData(0, float.MinValue, float.MaxValue, 0)]
public void ContainsTest(float x, float y, float width, float height)
{
var rect = new RectangleF(x, y, width, height);
float X = (x + width) / 2;
float Y = (y + height) / 2;
var p = new PointF(X, Y);
var r = new RectangleF(X, Y, width / 2, height / 2);
Assert.False(rect.Contains(X, Y));
Assert.False(rect.Contains(p));
Assert.False(rect.Contains(r));
}
[Theory]
[InlineData(0, 0, 0, 0)]
[InlineData(float.MaxValue / 2, float.MinValue / 2, float.MinValue / 2, float.MaxValue / 2)]
[InlineData(0, float.MinValue, float.MaxValue, 0)]
public void InflateTest(float x, float y, float width, float height)
{
var rect = new RectangleF(x, y, width, height);
var inflatedRect = new RectangleF(x - width, y - height, width + 2 * width, height + 2 * height);
rect.Inflate(width, height);
Assert.Equal(inflatedRect, rect);
var s = new SizeF(x, y);
inflatedRect = RectangleF.Inflate(rect, x, y);
rect.Inflate(s);
Assert.Equal(inflatedRect, rect);
}
[Theory]
[InlineData(float.MaxValue, float.MinValue, float.MaxValue / 2, float.MinValue / 2)]
[InlineData(0, float.MinValue, float.MaxValue, 0)]
public void IntersectTest(float x, float y, float width, float height)
{
var rect1 = new RectangleF(x, y, width, height);
var rect2 = new RectangleF(y, x, width, height);
var expectedRect = RectangleF.Intersect(rect1, rect2);
rect1.Intersect(rect2);
Assert.Equal(expectedRect, rect1);
Assert.False(rect1.IntersectsWith(expectedRect));
}
[Fact]
public static void IntersectIntersectingRectsTest()
{
var rect1 = new RectangleF(0, 0, 5, 5);
var rect2 = new RectangleF(1, 1, 3, 3);
var expected = new RectangleF(1, 1, 3, 3);
Assert.Equal(expected, RectangleF.Intersect(rect1, rect2));
}
[Theory]
[InlineData(0, 0, 0, 0)]
[InlineData(float.MaxValue, float.MinValue, float.MinValue, float.MaxValue)]
[InlineData(float.MaxValue, 0, 0, float.MaxValue)]
[InlineData(0, float.MinValue, float.MaxValue, 0)]
public void UnionTest(float x, float y, float width, float height)
{
var a = new RectangleF(x, y, width, height);
var b = new RectangleF(width, height, x, y);
float x1 = Math.Min(a.X, b.X);
float x2 = Math.Max(a.X + a.Width, b.X + b.Width);
float y1 = Math.Min(a.Y, b.Y);
float y2 = Math.Max(a.Y + a.Height, b.Y + b.Height);
var expectedRectangle = new RectangleF(x1, y1, x2 - x1, y2 - y1);
Assert.Equal(expectedRectangle, RectangleF.Union(a, b));
}
[Theory]
[InlineData(0, 0, 0, 0)]
[InlineData(float.MaxValue, float.MinValue, float.MinValue, float.MaxValue)]
[InlineData(float.MaxValue, 0, 0, float.MaxValue)]
[InlineData(0, float.MinValue, float.MaxValue, 0)]
public void OffsetTest(float x, float y, float width, float height)
{
var r1 = new RectangleF(x, y, width, height);
var expectedRect = new RectangleF(x + width, y + height, width, height);
var p = new PointF(width, height);
r1.Offset(p);
Assert.Equal(expectedRect, r1);
expectedRect.Offset(p);
r1.Offset(width, height);
Assert.Equal(expectedRect, r1);
}
[Fact]
public void ToStringTest()
{
var r = new RectangleF(5, -5, 0, 1);
Assert.Equal(string.Format(CultureInfo.CurrentCulture, "RectangleF [ X={0}, Y={1}, Width={2}, Height={3} ]", r.X, r.Y, r.Width, r.Height), r.ToString());
}
[Fact]
public void ToStringTestEmpty()
{
var r = new RectangleF(0, 0, 0, 0);
Assert.Equal("RectangleF [ Empty ]", r.ToString());
}
}
}

318
tests/ImageSharp.Tests/Numerics/RectangleTests.cs

@ -5,6 +5,9 @@
namespace ImageSharp.Tests
{
using System;
using System.Globalization;
using Xunit;
/// <summary>
@ -12,55 +15,294 @@ namespace ImageSharp.Tests
/// </summary>
public class RectangleTests
{
/// <summary>
/// Tests the equality operators for equality.
/// </summary>
[Fact]
public void AreEqual()
public void DefaultConstructorTest()
{
Assert.Equal(Rectangle.Empty, new Rectangle());
}
[Theory]
[InlineData(int.MaxValue, int.MinValue, int.MaxValue, int.MinValue)]
[InlineData(int.MaxValue, 0, int.MinValue, 0)]
[InlineData(0, 0, 0, 0)]
[InlineData(0, int.MinValue, 0, int.MaxValue)]
public void NonDefaultConstructorTest(int x, int y, int width, int height)
{
var rect1 = new Rectangle(x, y, width, height);
var rect2 = new Rectangle(new Point(x, y), new Size(width, height));
Assert.Equal(rect1, rect2);
}
[Theory]
[InlineData(int.MaxValue, int.MinValue, int.MaxValue, int.MinValue)]
[InlineData(int.MaxValue, 0, int.MinValue, 0)]
[InlineData(0, 0, 0, 0)]
[InlineData(0, int.MinValue, 0, int.MaxValue)]
public void FromLTRBTest(int left, int top, int right, int bottom)
{
Rectangle first = new Rectangle(1, 1, 100, 100);
Rectangle second = new Rectangle(1, 1, 100, 100);
var rect1 = new Rectangle(left, top, unchecked(right - left), unchecked(bottom - top));
var rect2 = Rectangle.FromLTRB(left, top, right, bottom);
Assert.Equal(first, second);
Assert.Equal(rect1, rect2);
}
/// <summary>
/// Tests the equality operators for inequality.
/// </summary>
[Fact]
public void AreNotEqual()
public void EmptyTest()
{
Assert.True(Rectangle.Empty.IsEmpty);
Assert.True(new Rectangle(0, 0, 0, 0).IsEmpty);
Assert.True(new Rectangle().IsEmpty);
}
[Theory]
[InlineData(int.MaxValue, int.MinValue, int.MaxValue, int.MinValue)]
[InlineData(int.MaxValue, 0, int.MinValue, 0)]
[InlineData(int.MinValue, int.MaxValue, int.MinValue, int.MaxValue)]
[InlineData(0, int.MinValue, 0, int.MaxValue)]
public void NonEmptyTest(int x, int y, int width, int height)
{
Assert.False(new Rectangle(x, y, width, height).IsEmpty);
}
[Theory]
[InlineData(int.MaxValue, int.MinValue, int.MaxValue, int.MinValue)]
[InlineData(int.MaxValue, 0, int.MinValue, 0)]
[InlineData(0, 0, 0, 0)]
[InlineData(0, int.MinValue, 0, int.MaxValue)]
[InlineData(int.MinValue, int.MaxValue, int.MinValue, int.MaxValue)]
public void DimensionsTest(int x, int y, int width, int height)
{
var rect = new Rectangle(x, y, width, height);
Assert.Equal(new Point(x, y), rect.Location);
Assert.Equal(new Size(width, height), rect.Size);
Assert.Equal(x, rect.X);
Assert.Equal(y, rect.Y);
Assert.Equal(width, rect.Width);
Assert.Equal(height, rect.Height);
Assert.Equal(x, rect.Left);
Assert.Equal(y, rect.Top);
Assert.Equal(unchecked(x + width), rect.Right);
Assert.Equal(unchecked(y + height), rect.Bottom);
var p = new Point(width, height);
var s = new Size(x, y);
rect.Location = p;
rect.Size = s;
Assert.Equal(p, rect.Location);
Assert.Equal(s, rect.Size);
Assert.Equal(width, rect.X);
Assert.Equal(height, rect.Y);
Assert.Equal(x, rect.Width);
Assert.Equal(y, rect.Height);
Assert.Equal(width, rect.Left);
Assert.Equal(height, rect.Top);
Assert.Equal(unchecked(x + width), rect.Right);
Assert.Equal(unchecked(y + height), rect.Bottom);
}
[Theory]
[InlineData(0, 0)]
[InlineData(int.MaxValue, int.MinValue)]
public static void LocationSetTest(int x, int y)
{
var point = new Point(x, y);
var rect = new Rectangle(10, 10, 10, 10) { Location = point };
Assert.Equal(point, rect.Location);
Assert.Equal(point.X, rect.X);
Assert.Equal(point.Y, rect.Y);
}
[Theory]
[InlineData(0, 0)]
[InlineData(int.MaxValue, int.MinValue)]
public static void SizeSetTest(int x, int y)
{
Rectangle first = new Rectangle(1, 1, 0, 100);
Rectangle second = new Rectangle(1, 1, 100, 100);
var size = new Size(x, y);
var rect = new Rectangle(10, 10, 10, 10) { Size = size };
Assert.Equal(size, rect.Size);
Assert.Equal(size.Width, rect.Width);
Assert.Equal(size.Height, rect.Height);
}
[Theory]
[InlineData(int.MaxValue, int.MinValue, int.MaxValue, int.MinValue)]
[InlineData(int.MaxValue, 0, int.MinValue, 0)]
[InlineData(0, int.MinValue, 0, int.MaxValue)]
[InlineData(int.MinValue, int.MaxValue, int.MinValue, int.MaxValue)]
public void EqualityTest(int x, int y, int width, int height)
{
var rect1 = new Rectangle(x, y, width, height);
var rect2 = new Rectangle(width / 2, height / 2, x, y);
Assert.NotEqual(first, second);
Assert.True(rect1 != rect2);
Assert.False(rect1 == rect2);
Assert.False(rect1.Equals(rect2));
Assert.False(rect1.Equals((object)rect2));
}
/// <summary>
/// Tests whether the rectangle constructors correctly assign properties.
/// </summary>
[Fact]
public void ConstructorAssignsProperties()
{
Rectangle first = new Rectangle(1, 1, 50, 100);
Assert.Equal(1, first.X);
Assert.Equal(1, first.Y);
Assert.Equal(50, first.Width);
Assert.Equal(100, first.Height);
Assert.Equal(1, first.Top);
Assert.Equal(51, first.Right);
Assert.Equal(101, first.Bottom);
Assert.Equal(1, first.Left);
Rectangle second = new Rectangle(new Point(1, 1), new Size(50, 100));
Assert.Equal(1, second.X);
Assert.Equal(1, second.Y);
Assert.Equal(50, second.Width);
Assert.Equal(100, second.Height);
Assert.Equal(1, second.Top);
Assert.Equal(51, second.Right);
Assert.Equal(101, second.Bottom);
Assert.Equal(1, second.Left);
public static void EqualityTestNotRectangle()
{
var rectangle = new Rectangle(0, 0, 0, 0);
Assert.False(rectangle.Equals(null));
Assert.False(rectangle.Equals(0));
Assert.False(rectangle.Equals(new RectangleF(0, 0, 0, 0)));
}
[Fact]
public static void GetHashCodeTest()
{
var rect1 = new Rectangle(10, 10, 10, 10);
var rect2 = new Rectangle(10, 10, 10, 10);
Assert.Equal(rect1.GetHashCode(), rect2.GetHashCode());
Assert.NotEqual(rect1.GetHashCode(), new Rectangle(20, 10, 10, 10).GetHashCode());
Assert.NotEqual(rect1.GetHashCode(), new Rectangle(10, 20, 10, 10).GetHashCode());
Assert.NotEqual(rect1.GetHashCode(), new Rectangle(10, 10, 20, 10).GetHashCode());
Assert.NotEqual(rect1.GetHashCode(), new Rectangle(10, 10, 10, 20).GetHashCode());
}
[Theory]
[InlineData(float.MaxValue, float.MinValue, float.MaxValue, float.MinValue)]
[InlineData(float.MinValue, float.MaxValue, float.MinValue, float.MaxValue)]
[InlineData(0, 0, 0, 0)]
public void RectangleFConversionTest(float x, float y, float width, float height)
{
var rect = new RectangleF(x, y, width, height);
Rectangle rCeiling, rTruncate, rRound;
unchecked
{
rCeiling = new Rectangle((int)Math.Ceiling(x), (int)Math.Ceiling(y),
(int)Math.Ceiling(width), (int)Math.Ceiling(height));
rTruncate = new Rectangle((int)x, (int)y, (int)width, (int)height);
rRound = new Rectangle((int)Math.Round(x), (int)Math.Round(y),
(int)Math.Round(width), (int)Math.Round(height));
}
Assert.Equal(rCeiling, Rectangle.Ceiling(rect));
Assert.Equal(rTruncate, Rectangle.Truncate(rect));
Assert.Equal(rRound, Rectangle.Round(rect));
}
[Theory]
[InlineData(int.MaxValue, int.MinValue, int.MinValue, int.MaxValue)]
[InlineData(0, int.MinValue, int.MaxValue, 0)]
public void ContainsTest(int x, int y, int width, int height)
{
var rect = new Rectangle(unchecked(2 * x - width), unchecked(2 * y - height), width, height);
var p = new Point(x, y);
var r = new Rectangle(x, y, width / 2, height / 2);
Assert.False(rect.Contains(x, y));
Assert.False(rect.Contains(p));
Assert.False(rect.Contains(r));
}
[Theory]
[InlineData(0, 0, 0, 0)]
[InlineData(int.MaxValue, int.MinValue, int.MinValue, int.MaxValue)]
[InlineData(0, int.MinValue, int.MaxValue, 0)]
public void InflateTest(int x, int y, int width, int height)
{
Rectangle inflatedRect, rect = new Rectangle(x, y, width, height);
unchecked
{
inflatedRect = new Rectangle(x - width, y - height, width + 2 * width, height + 2 * height);
}
Assert.Equal(inflatedRect, Rectangle.Inflate(rect, width, height));
rect.Inflate(width, height);
Assert.Equal(inflatedRect, rect);
var s = new Size(x, y);
unchecked
{
inflatedRect = new Rectangle(rect.X - x, rect.Y - y, rect.Width + 2 * x, rect.Height + 2 * y);
}
rect.Inflate(s);
Assert.Equal(inflatedRect, rect);
}
[Theory]
[InlineData(0, 0, 0, 0)]
[InlineData(int.MaxValue, int.MinValue, int.MinValue, int.MaxValue)]
[InlineData(0, int.MinValue, int.MaxValue, 0)]
public void IntersectTest(int x, int y, int width, int height)
{
var rect = new Rectangle(x, y, width, height);
var expectedRect = Rectangle.Intersect(rect, rect);
rect.Intersect(rect);
Assert.Equal(expectedRect, rect);
Assert.False(rect.IntersectsWith(expectedRect));
}
[Fact]
public static void IntersectIntersectingRectsTest()
{
var rect1 = new Rectangle(0, 0, 5, 5);
var rect2 = new Rectangle(1, 1, 3, 3);
var expected = new Rectangle(1, 1, 3, 3);
Assert.Equal(expected, Rectangle.Intersect(rect1, rect2));
}
[Theory]
[InlineData(0, 0, 0, 0)]
[InlineData(int.MaxValue, int.MinValue, int.MinValue, int.MaxValue)]
[InlineData(int.MaxValue, 0, 0, int.MaxValue)]
[InlineData(0, int.MinValue, int.MaxValue, 0)]
public void UnionTest(int x, int y, int width, int height)
{
var a = new Rectangle(x, y, width, height);
var b = new Rectangle(width, height, x, y);
int x1 = Math.Min(a.X, b.X);
int x2 = Math.Max(a.X + a.Width, b.X + b.Width);
int y1 = Math.Min(a.Y, b.Y);
int y2 = Math.Max(a.Y + a.Height, b.Y + b.Height);
var expectedRectangle = new Rectangle(x1, y1, x2 - x1, y2 - y1);
Assert.Equal(expectedRectangle, Rectangle.Union(a, b));
}
[Theory]
[InlineData(0, 0, 0, 0)]
[InlineData(int.MaxValue, int.MinValue, int.MinValue, int.MaxValue)]
[InlineData(int.MaxValue, 0, 0, int.MaxValue)]
[InlineData(0, int.MinValue, int.MaxValue, 0)]
public void OffsetTest(int x, int y, int width, int height)
{
var r1 = new Rectangle(x, y, width, height);
var expectedRect = new Rectangle(x + width, y + height, width, height);
var p = new Point(width, height);
r1.Offset(p);
Assert.Equal(expectedRect, r1);
expectedRect.Offset(p);
r1.Offset(width, height);
Assert.Equal(expectedRect, r1);
}
[Fact]
public void ToStringTest()
{
var r = new Rectangle(5, -5, 0, 1);
Assert.Equal(string.Format(CultureInfo.CurrentCulture, "Rectangle [ X={0}, Y={1}, Width={2}, Height={3} ]", r.X, r.Y, r.Width, r.Height), r.ToString());
}
[Fact]
public void ToStringTestEmpty()
{
var r = new Rectangle(0, 0, 0, 0);
Assert.Equal("Rectangle [ Empty ]", r.ToString());
}
}
}
Loading…
Cancel
Save