Modifications and adding som tests

This commit is contained in:
BobLd
2019-12-16 14:36:52 +00:00
parent 1656411fcb
commit 5cf1f6c58c
5 changed files with 28 additions and 9 deletions

View File

@@ -53,5 +53,14 @@ namespace UglyToad.PdfPig.Tests.Geometry
PdfRectangle rectangle4 = new PdfRectangle(5, 7, 10, 25); PdfRectangle rectangle4 = new PdfRectangle(5, 7, 10, 25);
Assert.False(rectangle1.IntersectsWith(rectangle4)); // special case where they share one border Assert.False(rectangle1.IntersectsWith(rectangle4)); // special case where they share one border
} }
public void Contains()
{
PdfRectangle rectangle = new PdfRectangle(10, 10, 20, 20);
Assert.True(rectangle.Contains(new PdfPoint(15, 15)));
Assert.False(rectangle.Contains(new PdfPoint(10, 15)));
Assert.True(rectangle.Contains(new PdfPoint(10, 15), true));
Assert.False(rectangle.Contains(new PdfPoint(100, 100), true));
}
} }
} }

View File

@@ -13,10 +13,20 @@ namespace UglyToad.PdfPig.Geometry
#region PdfRectangle #region PdfRectangle
/// <summary> /// <summary>
/// Whether the rectangle contains the point. /// Whether the rectangle contains the point.
/// <para>Return false if the point belongs to the border.</para>
/// </summary> /// </summary>
public static bool Contains(this PdfRectangle rectangle, PdfPoint point) /// <param name="rectangle">The rectangle that should contain the point.</param>
/// <param name="point">The point that should be contained within the rectangle.</param>
/// <param name="includeBorder">If set to false, will return false if the point belongs to the border.</param>
public static bool Contains(this PdfRectangle rectangle, PdfPoint point, bool includeBorder = false)
{ {
if (includeBorder)
{
return point.X >= rectangle.Left &&
point.X <= rectangle.Right &&
point.Y >= rectangle.Bottom &&
point.Y <= rectangle.Top;
}
return point.X > rectangle.Left && return point.X > rectangle.Left &&
point.X < rectangle.Right && point.X < rectangle.Right &&
point.Y > rectangle.Bottom && point.Y > rectangle.Bottom &&

View File

@@ -21,7 +21,7 @@ namespace UglyToad.PdfPig.Geometry
/// <summary> /// <summary>
/// True if the <see cref="PdfPath"/> was originaly draw as a rectangle. /// True if the <see cref="PdfPath"/> was originaly draw as a rectangle.
/// </summary> /// </summary>
internal bool IsDrawnAsRectangle { get; set; } public bool IsDrawnAsRectangle { get; internal set; }
private PdfPoint? currentPosition; private PdfPoint? currentPosition;
@@ -718,7 +718,7 @@ namespace UglyToad.PdfPig.Geometry
} }
/// <summary> /// <summary>
/// Order matters /// Compares two <see cref="PdfPath"/>s for equality. Paths will only be considered equal if the commands which construct the paths are in the same order.
/// </summary> /// </summary>
public override bool Equals(object obj) public override bool Equals(object obj)
{ {
@@ -736,14 +736,14 @@ namespace UglyToad.PdfPig.Geometry
} }
/// <summary> /// <summary>
/// Order matters /// Get the hash code. Paths will only have the same hash code if the commands which construct the paths are in the same order.
/// </summary> /// </summary>
public override int GetHashCode() public override int GetHashCode()
{ {
var hash = this.Commands.Count + 1; var hash = this.Commands.Count + 1;
for (int i = 0; i < this.Commands.Count; i++) for (int i = 0; i < this.Commands.Count; i++)
{ {
hash = hash * (i + 1) + this.Commands[i].GetHashCode(); hash = hash * (i + 1) * 17 + this.Commands[i].GetHashCode();
} }
return hash; return hash;
} }

View File

@@ -83,7 +83,7 @@
/// <param name="dx">The distance to move the point in the x direction relative to its current location.</param> /// <param name="dx">The distance to move the point in the x direction relative to its current location.</param>
/// <param name="dy">The distance to move the point in the y direction relative to its current location.</param> /// <param name="dy">The distance to move the point in the y direction relative to its current location.</param>
/// <returns>A new point shifted on the y axis by the given delta value.</returns> /// <returns>A new point shifted on the y axis by the given delta value.</returns>
public PdfPoint MoveXY(decimal dx, decimal dy) public PdfPoint Translate(decimal dx, decimal dy)
{ {
return new PdfPoint(X + dx, Y + dy); return new PdfPoint(X + dx, Y + dy);
} }

View File

@@ -132,9 +132,9 @@
/// <param name="dx">The distance to move the rectangle in the x direction relative to its current location.</param> /// <param name="dx">The distance to move the rectangle in the x direction relative to its current location.</param>
/// <param name="dy">The distance to move the rectangle in the y direction relative to its current location.</param> /// <param name="dy">The distance to move the rectangle in the y direction relative to its current location.</param>
/// <returns>A new rectangle shifted on the y axis by the given delta value.</returns> /// <returns>A new rectangle shifted on the y axis by the given delta value.</returns>
public PdfRectangle MoveXY(decimal dx, decimal dy) public PdfRectangle Translate(decimal dx, decimal dy)
{ {
return new PdfRectangle(this.BottomLeft.MoveXY(dx, dy), this.TopRight.MoveXY(dx, dy)); return new PdfRectangle(this.BottomLeft.Translate(dx, dy), this.TopRight.Translate(dx, dy));
} }
/// <summary> /// <summary>